#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}

#define MM 15151
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

int x[5001];
int p[5001], c[5001];

#define NN 10202

PQ q;
vector<int> G[NN];
int to[MM], cap[MM], cost[MM], V, E;
INT dis[NN], work[NN];
int pr_v[NN], pr_e[NN];

inline void add(int u, int v, int f, int rf, int c, int rc) {
	to[E]=v, cap[E]=f, cost[E]=c, G[u].pb(E++);
	to[E]=u, cap[E]=rf, cost[E]=rc, G[v].pb(E++);
}

inline void minCost(int s, int t, int m_x) {
	int u, v, e, i, flow;
	INT m_n, Cost=0;
	while(m_x) {
		for(i=0; i<=V; i++) dis[i]=inf;
		q=PQ(), q.push({0, s}), dis[s]=0;
		while(!q.empty()) {
			u=q.top().y, m_n=q.top().x, q.pop();
			if(dis[u]<m_n) continue;
			for(i=G[u].size(); i--; ) {
				e=G[u][i], v=to[e];
				m_n=dis[u]+cost[e]+work[u]-work[v];
				if(cap[e]>0 && dis[v]>m_n) {
					dis[v]=m_n;
					pr_v[v]=u, pr_e[v]=e;
					q.push({dis[v], v});
				}
			}
		}
		if(dis[t]==inf) break;
		for(i=0; i<=V; i++) work[i]+=dis[i];
		flow=m_x;
		for(u=t; u!=s; u=pr_v[u]) smin(flow, cap[pr_e[u]]);
		m_x-=flow, Cost+=flow*work[t];
		for(u=t; u!=s; u=pr_v[u]) {
			cap[pr_e[u]]-=flow, cap[pr_e[u]^1]+=flow;
		}
	}
	if(m_x) puts("-1");
	else printf("%I64d", Cost);
}

int arr[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n, m; scanf("%d %d", &n, &m);
	int nn=0;
	for(int i=1; i<=n; i++) {
	   	scanf("%d", x+i), arr[nn++]=x[i];
	}
	for(int i=1; i<=m; i++) {
	   	scanf("%d %d", p+i, c+i);
	   	arr[nn++]=p[i];
	}
	sort(arr, arr+nn);
	nn=unique(arr, arr+nn)-arr;

	int s=nn+1, t=s+1; V=t;

	for(int i=1; i<nn; i++) {
		int d=arr[i]-arr[i-1];
		add(i-1, i, n, n, d, d);
	}
	for(int i=1; i<=n; i++) {
		int id=lower_bound(arr, arr+nn, x[i])-arr;
		add(s, id, 1, 0, 0, 0);
	}
	for(int i=1; i<=m; i++) {
		int id=lower_bound(arr, arr+nn, p[i])-arr;
		add(id, t, c[i], 0, 0, 0);
	}
	minCost(s, t, n);
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}

#define MM 22010000
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

int x[5001];
int p[5001], c[5001];

#define NN 10002

PQ q;
vector<int> G[NN];
int to[MM], cap[MM], cost[MM], V, E;
INT dis[NN], work[NN];
int pr_v[NN], pr_e[NN];

inline void add(int u, int v, int f, int c) {
	to[E]=v, cap[E]=f, cost[E]=c, G[u].pb(E++);
	to[E]=u, cap[E]=0, cost[E]=c, G[u].pb(E++);
}

inline void minCost(int s, int t, int m_x) {
	int u, v, e, i=0, flow;
	INT m_n, Cost=0;
	while(m_x) {
		for(i=0; i<=V; i++) dis[i]=inf;
		q=PQ(), q.push({0, s}), dis[s]=0;
		while(!q.empty()) {
			u=q.top().y, m_n=q.top().x, q.pop();
			if(dis[u]<m_n) continue;
			for(i=G[u].size(); i--; ) {
				e=G[u][i], v=to[e];
				m_n=dis[u]+cost[e]+work[u]-work[v];
				if(cap[e]>0 && dis[v]>m_n) {
					dis[v]=m_n;
					pr_v[v]=u, pr_e[v]=e;
					q.push({dis[v], v});
				}
			}
		}
		if(dis[t]==inf) break;
		for(i=0; i<=V; i++) work[i]+=dis[i];
		flow=m_x;
		for(u=t; u!=s; u=pr_v[u]) smin(flow, cap[pr_e[u]]);
		m_x-=flow, Cost+=flow*work[t];
		for(u=t; u!=s; u=pr_v[u]) {
			cap[pr_e[u]]-=flow, cap[pr_e[u]^1]+=flow;
		}
	}
	if(m_x) puts("-1");
	else printf("%I64d", Cost);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n, m; scanf("%d %d", &n, &m);
	for(int i=1; i<=n; i++) scanf("%d", x+i);
	for(int i=1; i<=m; i++) scanf("%d %d", p+i, c+i);

	int s=0, t=n+m+1; V=t;
	for(int i=1; i<=n; i++) {
		add(s, i, 1, 0);
		for(int j=1; j<=m; j++) {
			add(i, n+j, 1, abs(x[i]-p[j]));
		}
	}
	for(int i=1; i<=m; i++) add(n+i, t, c[i], 0);
	minCost(s, t, n);
}


#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
using namespace std;
using DO = double;
using LL = long long;
using VI = vector<int>;
using pii = pair<int, int>;

struct data{
  LL val;
  int L, R, id;
  data(int L=0, int R=0, int id=0, LL val=0) : L(L), R(R), id(id), val(val) {}
  void pr() {
    printf("%d --> %d   val = %d,   id = %d\n", L, R, val, id);
  }
};

const int N = 5050, M = 1e5;
data d[M], f[M];
int x[N];
pii p[N];
int s[N];

int main() {
#ifdef TRUE
  freopen("F.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i=1; i<=n; i++) {
    scanf("%d", x+i);
  }
  sort(x+1, x+n+1);
  for(int i=1; i<=m; i++) {
    scanf("%d %d", &p[i].x, &p[i].y);
  }
  sort(p+1, p+m+1);
  int dn=0, fn;
  for(int i=1; i<=m; i++) {
    s[i] = s[i-1]+p[i].y;
  }
  for(int i=1; i<=m; i++) {
    d[i-1] = data(s[i-1]+1, s[i], i, abs(x[1]-p[i].x));
    if(i>1) {
      d[i-1].val = min(d[i-1].val, d[i-2].val);
    }
  }
  dn=m;

  #ifdef TRUE
  for(int i=0; i<dn; i++) {
    d[i].pr();
  }
  puts("");
  #endif

  for(int i=2; i<=n; i++) {
    fn=0;
    for(int j=0, id; j<dn; j++) {
      id = d[j].id;
      if(d[j].L < s[id]) {
        f[fn++]=data(d[j].L+1, min(s[id], d[j].R+1), id, d[j].val+abs(x[i]-p[id].x));
      }
      if(s[id] == d[j].R && id<m) {
        f[fn++]=data(s[id]+1, s[id]+1, id+1, d[j].val+abs(x[i]-p[id+1].x));
      }
    }
    #ifdef TRUE
    for(int i=0; i<fn; i++) {
      f[i].pr();
    }
    puts("");
    #endif
    dn=0;
    for(int j=0; j<fn; j++) {
      if(dn) {
        f[j].val = min(f[j].val, d[dn-1].val);
        if(f[j].id != d[dn-1].id || f[j].val < d[dn-1].val) {
          d[dn++]=f[j];
        }else {
          d[dn-1].R = f[j].R;
        }
      }else {
        d[dn++]=f[j];
      }
    }
    #ifdef TRUE
    for(int i=0; i<dn; i++) {
      d[i].pr();
    }
    puts("");
    puts("");
    #endif
  }
  cout<<d[dn-1].val<<endl;
  return 0;
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}

#define MM 20010000
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

int x[5001];
int p[5001], c[5001];

#define NN 10002

PQ q;
vector<int> G[NN];
int to[MM], cap[MM], cost[MM], V, E;
INT dis[NN], work[NN];
int pr_v[NN], pr_e[NN];

inline void add(int u, int v, int f, int c) {
	to[E]=v, cap[E]=f, cost[E]=c, G[u].pb(E++);
	to[E]=u, cap[E]=0, cost[E]=c, G[u].pb(E++);
}

inline void minCost(int s, int t, int m_x) {
	int u, v, e, i=0, flow;
	INT m_n, Cost=0;
	while(m_x) {
		for(i=0; i<=V; i++) dis[i]=inf;
		q=PQ(), q.push({0, s}), dis[s]=0;
		while(!q.empty()) {
			u=q.top().y, m_n=q.top().x, q.pop();
			if(dis[u]<m_n) continue;
			for(i=G[u].size(); i--; ) {
				e=G[u][i], v=to[e];
				m_n=dis[u]+cost[e]+work[u]-work[v];
				if(cap[e]>0 && dis[v]>m_n) {
					dis[v]=m_n;
					pr_v[v]=u, pr_e[v]=e;
					q.push({dis[v], v});
				}
			}
		}
		if(dis[t]==inf) break;
		for(i=0; i<=V; i++) work[i]+=dis[i];
		flow=m_x;
		for(u=t; u!=s; u=pr_v[u]) smin(flow, cap[pr_e[u]]);
		m_x-=flow, Cost+=flow*work[t];
		for(u=t; u!=s; u=pr_v[u]) {
			cap[pr_e[u]]-=flow, cap[pr_e[u]^1]+=flow;
		}
	}
	if(m_x) puts("-1");
	else printf("%I64d", Cost);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n, m; scanf("%d %d", &n, &m);
	for(int i=1; i<=n; i++) scanf("%d", x+i);
	for(int i=1; i<=m; i++) scanf("%d %d", p+i, c+i);

	int s=0, t=n+m+1; V=t;
	for(int i=1; i<=n; i++) {
		add(s, i, 1, 0);
		for(int j=1; j<=m; j++) {
			add(i, n+j, 1, abs(x[i]-p[j]));
		}
	}
	for(int i=1; i<=m; i++) add(n+i, t, c[i], 0);
	minCost(s, t, n);
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 10010
#define inf 2e9+1
using namespace std;

typedef long long INT;

int pos[NN], sum[NN];

map <int, int> mp;

int nn = 1;

void go_left(int u){
	for(int i = u - 1; i; i--){
		if(sum[i]) sum[i]--;
		if(mp[pos[i]] > 0) {mp[pos[i]]--; return ;}
	}
}

void go_right(int u){
	for(int i = u + 1; i <= nn; i++){
		if(mp[pos[i]] > 0) {mp[pos[i]]--; return ;}
		sum[i]++;
	}
}

int calc_left(int u){
	int ans = 0;
	for(int i = u - 1; i >= 0; i--){
		if(sum[i] == 0) ans += pos[i + 1] - pos[i];
		else ans -= pos[i + 1] - pos[i];
		if(mp[pos[i]] > 0) return ans;
	}
	return inf;
}

int calc_right(int u){
	int ans = 0;
	for(int i = u + 1; i <= nn; i++){
		ans += pos[i + 1] - pos[i];
		if(mp[pos[i]] > 0) return ans;
	}
	return inf;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	INT n, m;
	scanf("%I64d%I64d", &n, &m);
	for(int i = 1; i <= n; i++){
		int u;
		scanf("%d", &u);
		pos[nn++] = u;
		mp[u]--;
	}
	for(int i = 1; i <= m; i++){
		int p, val;
		scanf("%d%d", &p, &val);
		pos[nn++] = p, mp[p] += val;
	}
	sort(pos + 1, pos + nn);
	nn = unique(pos + 1, pos + nn) - pos - 1;
	for(int i = 1; i <= nn; i++) cerr<<pos[i]<<" ";
	INT ans = 0;
	for(int i = 1; i <= nn; i++){
		while(mp[pos[i]] < 0){
			int dl = calc_left(i);
			int dr = calc_right(i);
			if(dl <= dr){
				ans += dl;
				go_left(i);
			} else{
				ans += dr;
				go_right(i);
			}
			mp[pos[i]]++;
		}
	}

	printf("%I64d\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=11111;
const int MM=11111111;
const int inf=0x3f3f3f3f;
const INT INF=0x3f3f3f3f3f3f3f3fll;

INT dis[NN],src,tar;
int E, head[NN], to[MM], nxt[MM], pre[NN];
int a[NN],b[NN],c[NN];
INT flow[MM],cost[MM];

void add_edge(int u, int v, int f, int c){
	nxt[E]=head[u]; to[E]=v; flow[E]=f; cost[E]=c; head[u]=E++;
	nxt[E]=head[v]; to[E]=u; flow[E]=0; cost[E]=-c; head[v]=E++;
}

queue <int> q;

INT C;

INT maxflow(int s, int t){
	INT ans=0, u, v, bot;
	while(1) {
		q=queue<int> ();
		q.push(s);
		memset(dis, inf, sizeof(dis));
		dis[s]=0;
		while(!q.empty()){
			u=q.front();
			q.pop();
			for(int e=head[u]; ~e; e=nxt[e]){
				v=to[e];
				if(flow[e]==0) continue;
				if(dis[v]>dis[u]+cost[e]) {
					dis[v]=dis[u]+cost[e];
					q.push(v);
					pre[v]=e;
				}
			}
		}

		if(dis[t]==INF) return ans;
		bot=INF;
		for(u=t; u!=s; u=to[pre[u]^1]) bot=min(bot, flow[pre[u]]);
		for(u=t; u!=s; u=to[pre[u]^1]) {
			flow[pre[u]]-=bot;
			flow[pre[u]^1]+=bot;
		}
		C+=dis[t];
		ans+=bot;
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n,m; cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)scanf("%d%d",&b[i],&c[i]);

	memset(head,-1,sizeof head);
	src=0; tar=n+m+1;

	for(int i=1;i<=n;i++){
		add_edge(src,i,1,0);
		for(int j=1;j<=m;j++){
			add_edge(i,n+j,1,abs(a[i]-b[j]));
		}
	}
	for(int j=1;j<=m;j++){
		add_edge(n+j,tar,c[j],0);
	}

	if(maxflow(src,tar)<n){
		puts("-1");
		return 0;
	}
	cout<<C<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 10101;
const int inf = 0x3f3f3f3f;
int x[N], xn, mouse[N], y[N], c[N];
int hole[N];
int rev[N];

int L(int p) {
  for(int ans = 0; p>=0; p--) {
    if(hole[p]) {
      return ans;
    }
    if(p) {
      if(rev[p]) {
        ans-=x[p]-x[p-1];
      }else {
        ans+=x[p]-x[p-1];
      }
    }
  }
  return inf;
}

int R(int p) {
  for(int ans = 0; p<xn; p++) {
    if(hole[p]) {
      return ans;
    }
    ans+=x[p+1]-x[p];
  }
  return inf;
}

void goL(int p) {
  for(; p>=0; p--) {
    if(hole[p]) {
      hole[p]--;
      return;
    }
    if(rev[p]) {
      rev[p]--;
    }
  }
}

void goR(int p) {
  for(; p<xn; p++) {
    if(hole[p]) {
      hole[p]--;
      return;
    }
    rev[p+1]++;
  }
}

int main() {
#ifdef TRUE
  freopen("F.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i=1; i<=n; i++) {
    scanf("%d", mouse+i);
    x[xn++]=mouse[i];
  }
  for(int i=1; i<=m; i++) {
    scanf("%d %d", y+i, c+i);
    x[xn++]=y[i];
  }
  sort(x, x+xn);
  xn = unique(x, x+xn)-x;
  sort(mouse+1, mouse+n+1);
  for(int i=1; i<=m; i++) {
    y[i] = lower_bound(x, x+xn, y[i])-x;
    hole[y[i]]+=c[i];
  }
  LL ans=0;
  for(int i=1, L, R, pos; i<=n; i++) {
    pos = lower_bound(x, x+xn, mouse[i])-x;
    L = ::L(pos);
    R = ::R(pos);
    if(L == inf && R == inf) {
      puts("-1");
      return 0;
    }
    if(L<R) {
      ans+=L;
      goL(pos);
    }else {
      ans+=R;
      goR(pos);
    }
  }
  cout<<ans<<endl;
  return 0;
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

struct edge {
	int to, capacity, cost, rev;
	edge() {}
	edge(int to,int _capacity,int _cost,int _rev):
		to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};

#define NN 10101

int x[5001], arr[NN];
int p[5001], c[5001];
PQ q;

struct Min_Cost_Max_Flow {
	int V, PreV[NN], PreE[NN];
	INT H[NN], dis[NN];
	vector<edge> G[NN];
   	inline void solve() {
   		int n, m; scanf("%d %d", &n, &m);
   		int nn=0;
   		for(int i=1; i<=n; i++) {
		   	scanf("%d", x+i), arr[nn++]=x[i];
		}
   		for(int i=1; i<=m; i++) {
		   	scanf("%d %d", p+i, c+i);
		   	arr[nn++]=p[i];
		}
		int s=nn, t=s+1; V=t;

   		sort(arr, arr+nn);
   		nn=unique(arr, arr+nn)-arr;

   		for(int i=1; i<nn; i++) {
   			int d=arr[i]-arr[i-1];
   			add(i-1, i, n, n, d, d);
   		}
   		for(int i=1; i<=n; i++) {
   			int id=lower_bound(arr, arr+nn, x[i])-arr;
   			add(s, id, 1, 0, 0, 0);
   		}
   		for(int i=1; i<=m; i++) {
   			int id=lower_bound(arr, arr+nn, p[i])-arr;
   			add(id, t, c[i], 0, 0, 0);
   		}
   		minCost(s, t, n);
   	}
	void add(int from, int to, int cap, int rev, int cost, int Rev) {
		G[from].push_back(edge(to, cap, cost, G[to].size()));
		G[to].push_back(edge(from, rev, Rev, G[from].size() - 1));
	}
	void minCost(int s, int t, int f) {
		INT res = 0;
		while (f) {
			q=PQ();
   			fill(dis, dis + 1 + V, inf);
   			dis[s] = 0; q.push(pii(0, s));
			while (!q.empty()) {
   				pii now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] < now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
   					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
   						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
   						PreV[e.to] = v; PreE[e.to] = i;
   						q.push(pii(dis[e.to], e.to));
	   				}
	   			}
	   		}
			if (dis[t] == inf)   break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
   			f -= d; res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
   				edge& e = G[PreV[v]][PreE[v]];
   				e.capacity -= d;
   				G[v][e.rev].capacity += d;
			}
   		}
   		if(f) puts("-1");
   		else printf("%I64d\n", res);
   	}
} mcmf;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	mcmf.solve();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=11111;
const int MM=17700000;
const int inf=0x3f3f3f3f;
const INT INF=0x3f3f3f3f3f3f3f3fll;

INT dis[NN],src,tar;
int E, head[NN], to[MM], nxt[MM], pre[NN];
int a[NN],b[NN],c[NN];
INT flow[MM];
int cost[MM];

void add_edge(int u, int v, int f, int c){
	nxt[E]=head[u]; to[E]=v; flow[E]=f; cost[E]=c; head[u]=E++;
	nxt[E]=head[v]; to[E]=u; flow[E]=0; cost[E]=-c; head[v]=E++;
}

queue <int> q;

INT C;

INT maxflow(int s, int t){
	INT ans=0, u, v, bot;
	while(1) {
		q=queue<int> ();
		q.push(s);
		memset(dis, inf, sizeof(dis));
		dis[s]=0;
		while(!q.empty()){
			u=q.front();
			q.pop();
			for(int e=head[u]; ~e; e=nxt[e]){
				v=to[e];
				if(flow[e]==0) continue;
				if(dis[v]>dis[u]+cost[e]) {
					dis[v]=dis[u]+cost[e];
					q.push(v);
					pre[v]=e;
				}
			}
		}

		if(dis[t]==INF) return ans;
		bot=INF;
		for(u=t; u!=s; u=to[pre[u]^1]) bot=min(bot, flow[pre[u]]);
		for(u=t; u!=s; u=to[pre[u]^1]) {
			flow[pre[u]]-=bot;
			flow[pre[u]^1]+=bot;
		}
		C+=dis[t];
		ans+=bot;
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n,m; cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)scanf("%d%d",&b[i],&c[i]);

	memset(head,-1,sizeof head);
	src=0; tar=n+m+1;

	for(int i=1;i<=n;i++){
		add_edge(src,i,1,0);
		for(int j=1;j<=m;j++){
			add_edge(i,n+j,1,abs(a[i]-b[j]));
		}
	}
	for(int j=1;j<=m;j++){
		add_edge(n+j,tar,c[j],0);
	}

	if(maxflow(src,tar)<n){
		puts("-1");
		return 0;
	}
	cout<<C<<endl;

	return 0;
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}

#define MM 22010000
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

int x[5001];
int p[5001], c[5001];

#define NN 10202

PQ q;
vector<int> G[NN];
int to[MM], cap[MM], cost[MM], V, E;
INT dis[NN], work[NN];
int pr_v[NN], pr_e[NN];

inline void add(int u, int v, int f, int rf, int c, int rc) {
	to[E]=v, cap[E]=f, cost[E]=c, G[u].pb(E++);
	to[E]=u, cap[E]=rf, cost[E]=rc, G[v].pb(E++);
}

inline void minCost(int s, int t, int m_x) {
	int u, v, e, i, flow;
	INT m_n, Cost=0;
	while(m_x) {
		for(i=0; i<=V; i++) dis[i]=inf;
		q=PQ(), q.push({0, s}), dis[s]=0;
		while(!q.empty()) {
			u=q.top().y, m_n=q.top().x, q.pop();
			if(dis[u]<m_n) continue;
			for(i=G[u].size(); i--; ) {
				e=G[u][i], v=to[e];
				m_n=dis[u]+cost[e]+work[u]-work[v];
				if(cap[e]>0 && dis[v]>m_n) {
					dis[v]=m_n;
					pr_v[v]=u, pr_e[v]=e;
					q.push({dis[v], v});
				}
			}
		}
		if(dis[t]==inf) break;
		for(i=0; i<=V; i++) work[i]+=dis[i];
		flow=m_x;
		for(u=t; u!=s; u=pr_v[u]) smin(flow, cap[pr_e[u]]);
		m_x-=flow, Cost+=flow*work[t];
		for(u=t; u!=s; u=pr_v[u]) {
			cap[pr_e[u]]-=flow, cap[pr_e[u]^1]+=flow;
		}
	}
	if(m_x) puts("-1");
	else printf("%I64d", Cost);
}

int arr[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n, m; scanf("%d %d", &n, &m);
	int nn=0;
	for(int i=1; i<=n; i++) {
	   	scanf("%d", x+i), arr[nn++]=x[i];
	}
	for(int i=1; i<=m; i++) {
	   	scanf("%d %d", p+i, c+i);
	   	arr[nn++]=p[i];
	}
	sort(arr, arr+nn);
	nn=unique(arr, arr+nn)-arr;

	int s=nn+1, t=s+1; V=t;

	for(int i=1; i<nn; i++) {
		int d=arr[i]-arr[i-1];
		add(i-1, i, n, n, d, d);
	}
	for(int i=1; i<=n; i++) {
		int id=lower_bound(arr, arr+nn, x[i])-arr;
		add(s, id, 1, 0, 0, 0);
	}
	for(int i=1; i<=m; i++) {
		int id=lower_bound(arr, arr+nn, p[i])-arr;
		add(id, t, c[i], 0, 0, 0);
	}
	minCost(s, t, n);
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

struct edge {
	int to, capacity, cost, rev;
	edge() {}
	edge(int to,int _capacity,int _cost,int _rev):
		to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};

#define NN 10002

int x[5001];
int p[5001], c[5001];
PQ q;

struct Min_Cost_Max_Flow {
	int V, PreV[NN], PreE[NN];
	INT H[NN], dis[NN];
	vector<edge> G[NN];
   	inline void solve() {
   		int n, m; scanf("%d %d", &n, &m);
   		for(int i=1; i<=n; i++) scanf("%d", x+i);
   		for(int i=1; i<=m; i++) scanf("%d %d", p+i, c+i);

   		int s=0, t=n+m+1; V=t;
   		for(int i=1; i<=n; i++) {
   			add(s, i, 1, 0);
   			for(int j=1; j<=m; j++) {
   				add(i, n+j, 1, abs(x[i]-p[j]));
   			}
   		}
   		for(int i=1; i<=m; i++) add(n+i, t, c[i], 0);
   		minCost(s, t, n);
   	}
	void add(int from, int to, int cap, int cost) {
		G[from].push_back(edge(to, cap, cost, G[to].size()));
		G[to].push_back(edge(from,0, -cost, G[from].size() - 1));
	}
	void minCost(int s, int t, int f) {
		INT res = 0;
		while (f) {
			q=PQ();
   			fill(dis, dis + 1 + V, inf);
   			dis[s] = 0; q.push(pii(0, s));
			while (!q.empty()) {
   				pii now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] < now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
   					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
   						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
   						PreV[e.to] = v; PreE[e.to] = i;
   						q.push(pii(dis[e.to], e.to));
	   				}
	   			}
	   		}
			if (dis[t] == inf)   break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
   			f -= d; res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
   				edge& e = G[PreV[v]][PreE[v]];
   				e.capacity -= d;
   				G[v][e.rev].capacity += d;
			}
   		}
   		if(f) puts("-1");
   		else printf("%I64d\n", res);
   	}
} mcmf;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	mcmf.solve();
}


#include<bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 10101;
const int inf = 2100000000;
int x[N], xn, mouse[N], y[N], c[N];
int hole[N];
int rev[N];

int L(int p) {
  for(int ans = 0; p>=0; p--) {
    if(hole[p]>0) {
      return ans;
    }
    if(p) {
      if(rev[p]) {
        ans-=x[p]-x[p-1];
      }else {
        ans+=x[p]-x[p-1];
      }
    }
  }
  return inf;
}

int R(int p) {
  for(int ans = 0; p<xn; p++) {
    if(hole[p]>0) {
      return ans;
    }
    ans+=x[p+1]-x[p];
  }
  return inf;
}

void goL(int p) {
  for(; p>=0; p--) {
    if(hole[p]>0) {
      hole[p]--;
      return;
    }
    if(rev[p]) {
      rev[p]--;
    }
  }
}

void goR(int p) {
  for(; p<xn; p++) {
    if(hole[p]>0) {
      hole[p]--;
      return;
    }
    rev[p+1]++;
  }
}

int main() {
#ifdef TRUE
  freopen("F.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i=1; i<=n; i++) {
    scanf("%d", mouse+i);
    x[xn++]=mouse[i];
  }
  for(int i=1; i<=m; i++) {
    scanf("%d %d", y+i, c+i);
    x[xn++]=y[i];
  }
  sort(x, x+xn);
  xn = unique(x, x+xn)-x;
  for(int i=1; i<=m; i++) {
    y[i] = lower_bound(x, x+xn, y[i])-x;
    hole[y[i]]+=c[i];
  }
  for(int i=1, pos; i<=n; i++) {
    pos = lower_bound(x, x+xn, mouse[i])-x;
    hole[pos]--;
  }
  LL ans=0;
  for(int i=0; i<xn; i++) {
    for(int L, R; hole[i]<0;) {
      L = ::L(i);
      R = ::R(i);
      if(L == inf && R == inf) {
        puts("-1");
        return 0;
      }
      if(L<R) {
        ans+=L;
        goL(i);
      }else {
        ans+=R;
        goR(i);
      }
      hole[i]++;
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=11111;
const int MM=22222222;
const int inf=0x3f3f3f3f;
const INT INF=0x3f3f3f3f3f3f3f3fll;

INT dis[NN],src,tar;
int E, head[NN], to[MM], nxt[MM], pre[NN];
int a[NN],b[NN],c[NN];
INT flow[MM],cost[MM];

void add_edge(int u, int v, int f, int c){
	nxt[E]=head[u]; to[E]=v; flow[E]=f; cost[E]=c; head[u]=E++;
	nxt[E]=head[v]; to[E]=u; flow[E]=0; cost[E]=-c; head[v]=E++;
}

queue <int> q;

INT C;

INT maxflow(int s, int t){
	INT ans=0, u, v, bot;
	while(1) {
		q=queue<int> ();
		q.push(s);
		memset(dis, inf, sizeof(dis));
		dis[s]=0;
		while(!q.empty()){
			u=q.front();
			q.pop();
			for(int e=head[u]; ~e; e=nxt[e]){
				v=to[e];
				if(flow[e]==0) continue;
				if(dis[v]>dis[u]+cost[e]) {
					dis[v]=dis[u]+cost[e];
					q.push(v);
					pre[v]=e;
				}
			}
		}

		if(dis[t]==INF) return ans;
		bot=INF;
		for(u=t; u!=s; u=to[pre[u]^1]) bot=min(bot, flow[pre[u]]);
		for(u=t; u!=s; u=to[pre[u]^1]) {
			flow[pre[u]]-=bot;
			flow[pre[u]^1]+=bot;
		}
		C+=dis[t];
		ans+=bot;
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n,m; cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)scanf("%d%d",&b[i],&c[i]);

	memset(head,-1,sizeof head);
	src=0; tar=n+m+1;

	for(int i=1;i<=n;i++){
		add_edge(src,i,1,0);
		for(int j=1;j<=m;j++){
			add_edge(i,n+j,1,abs(a[i]-b[j]));
		}
	}
	for(int j=1;j<=m;j++){
		add_edge(n+j,tar,c[j],0);
	}

	if(maxflow(src,tar)<n){
		puts("-1");
		return 0;
	}
	cout<<C<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 10101;
const int inf = 0x3f3f3f3f;
int x[N], xn, mouse[N], y[N], c[N];
int hole[N];
int revL[N], revR[N];

int L(int p) {
  for(int ans = 0; p>=0; p--) {
    if(hole[p]) {
      return ans;
    }
    if(p) {
      if(revL[p]) {
        ans-=x[p]-x[p-1];
      }else {
        ans+=x[p]-x[p-1];
      }
    }
  }
  return inf;
}

int R(int p) {
  for(int ans = 0; p<xn; p++) {
    if(hole[p]) {
      return ans;
    }
    if(!revR[p]) {
      ans+=x[p+1]-x[p];
    }
  }
  return inf;
}

void goL(int p) {
  for(; p>=0; p--) {
    if(hole[p]) {
      hole[p]--;
      return;
    }
    if(revL[p]) {
      revL[p]--;
    }else {
      revR[p-1]++;
    }
  }
}

void goR(int p) {
  for(; p<xn; p++) {
    if(hole[p]) {
      hole[p]--;
      return;
    }
    if(revR[p]) {
      revR[p]--;
    }else {
      revL[p+1]++;
    }
  }
}

int main() {
#ifdef TRUE
  freopen("F.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i=1; i<=n; i++) {
    scanf("%d", mouse+i);
    x[xn++]=mouse[i];
  }
  for(int i=1; i<=m; i++) {
    scanf("%d %d", y+i, c+i);
    x[xn++]=y[i];
  }
  sort(x, x+xn);
  xn = unique(x, x+xn)-x;
  sort(mouse+1, mouse+n+1);
  for(int i=1; i<=m; i++) {
    y[i] = lower_bound(x, x+xn, y[i])-x;
    hole[y[i]]+=c[i];
  }
  LL ans=0;
  for(int i=1, L, R, pos; i<=n; i++) {
    pos = lower_bound(x, x+xn, mouse[i])-x;
    L = ::L(pos);
    R = ::R(pos);
    if(L == inf && R == inf) {
      puts("-1");
      return 0;
    }
    if(L<R) {
      ans+=L;
      goL(pos);
    }else {
      ans+=R;
      goR(pos);
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

using INT = long long;

#define NN 5050
#define inf 2100000000

map<int, int> mp;
int arr[NN*2], rev[NN*2];
int tot[NN*2], sum[NN*2];
int nn;

int calc_left(int u) {
	int ans = 0;
	for(int i=u-1; i; i--) {
		if(sum[i] == 0) ans += rev[i+1] - rev[i];
		else ans -= rev[i+1] - rev[i];
		if(tot[i] > 0) return ans;
	}
	return inf;
}

int go_left(int u) {
	for(int i=u-1; i; i--) {
		if(sum[i]) sum[i]--;
		if(tot[i] > 0) return tot[i]--, 0;
	}
}

int go_right(int u) {
	for(int i=u; i<=nn; i++) {
		if(tot[i] > 0) return tot[i]--, 0;
		sum[i]++;
	}
}

int calc_right(int u) {
	int ans = 0;
	for(int i=u+1; i<=nn; i++) {
		ans += rev[i] - rev[i-1];
		if(tot[i] > 0) return ans;
	}
	return inf;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n, m;
	cin >> n >> m;
	for(int i=1, x; i<=n; i++) {
		scanf("%d", &x);
		mp[x]--; arr[nn++] = x;
	}

	for(int i=1, u, p; i<=m; i++) {
		scanf("%d %d", &p, &u);
		mp[p] += u;
		arr[nn++] = p;
	}

	sort(arr, arr+nn);
	nn = unique(arr, arr+nn) - arr;

	for(int i=0; i<nn; i++) {
		rev[i+1] = arr[i]; tot[i+1] = mp[arr[i]];
	}

	long long ans = 0;

	for(int i=1; i<=nn; i++) {
		while(tot[i] < 0) {
			int ans_l = calc_left(i);
			int ans_r = calc_right(i);
			if(min(ans_l, ans_r) == inf) return puts("-1"), 0;
			if(ans_l <= ans_r) {
				ans += ans_l;
				go_left(i);
			} else {
				ans += ans_r;
				go_right(i);
			}
			tot[i]++;
		}
	}

	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
using LL = long long;
using pii = pair<int, int>;

struct data{
  LL val;
  int L, R, id;
  data(int L=0, int R=0, int id=0, LL val=0) : L(L), R(R), id(id), val(val) {}
};

const int N = 5050, M = 5e6;
data d[M], f[M];
int x[N];
pii p[N];
int s[N];

int main() {
#ifdef TRUE
  freopen("F.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  LL md1=0, md2=0;
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i=1; i<=n; i++) {
    scanf("%d", x+i);
    md1+=x[i];
  }
  sort(x+1, x+n+1);
  for(int i=1; i<=m; i++) {
    scanf("%d %d", &p[i].x, &p[i].y);
    md2+=(LL)p[i].x*p[i].y;
  }
  sort(p+1, p+m+1);
  int dn=0, fn;
  for(int i=1; i<=m; i++) {
    s[i] = s[i-1]+p[i].y;
  }
  if(n > s[m]) {
    puts("-1");
    return 0;
  }
  md1/=n;
  md2/=s[m];
  if(md1 > md2) {
    for(int i=1; i<=n; i++) {
      x[i] = -x[i];
    }
    sort(x+1, x+n+1);
    for(int i=1; i<=m; i++) {
      p[i].x = -p[i].x;
    }
    sort(p+1, p+m+1);
    for(int i=1; i<=m; i++) {
      s[i] = s[i-1]+p[i].y;
    }
  }
  for(int i=1; i<=m; i++) {
    d[i-1] = data(s[i-1]+1, s[i], i, abs(x[1]-p[i].x));
    if(i>1) {
      d[i-1].val = min(d[i-1].val, d[i-2].val);
    }
  }
  dn=m;
  for(int i=2; i<=n; i++) {
    fn=0;
    for(int j=0, id; j<dn; j++) {
      id = d[j].id;
      if(d[j].L < s[id]) {
        f[fn++]=data(d[j].L+1, min(s[id], d[j].R+1), id, d[j].val+abs(x[i]-p[id].x));
      }
      if(s[id] == d[j].R && id<m) {
        f[fn++]=data(s[id]+1, s[id]+1, id+1, d[j].val+abs(x[i]-p[id+1].x));
      }
    }
    dn=0;
    for(int j=0; j<fn; j++) {
      if(dn) {
        f[j].val = min(f[j].val, d[dn-1].val);
        if(f[j].id != d[dn-1].id || f[j].val < d[dn-1].val) {
          d[dn++]=f[j];
        }else {
          d[dn-1].R = f[j].R;
        }
      }else {
        d[dn++]=f[j];
      }
    }
  }
  cout<<d[dn-1].val<<endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 10101;
const int inf = 2100000000;
int x[N], xn, mouse[N], y[N], c[N];
int hole[N];
int rev[N];

int L(int p) {
  for(int ans = 0; p>=0; p--) {
    if(hole[p]) {
      return ans;
    }
    if(p) {
      if(rev[p]) {
        ans-=x[p]-x[p-1];
      }else {
        ans+=x[p]-x[p-1];
      }
    }
  }
  return inf;
}

int R(int p) {
  for(int ans = 0; p<xn; p++) {
    if(hole[p]) {
      return ans;
    }
    ans+=x[p+1]-x[p];
  }
  return inf;
}

void goL(int p) {
  for(; p>=0; p--) {
    if(hole[p]) {
      hole[p]--;
      return;
    }
    if(rev[p]) {
      rev[p]--;
    }
  }
}

void goR(int p) {
  for(; p<xn; p++) {
    if(hole[p]) {
      hole[p]--;
      return;
    }
    rev[p+1]++;
  }
}

int main() {
#ifdef TRUE
  freopen("F.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i=1; i<=n; i++) {
    scanf("%d", mouse+i);
    x[xn++]=mouse[i];
  }
  for(int i=1; i<=m; i++) {
    scanf("%d %d", y+i, c+i);
    x[xn++]=y[i];
  }
  sort(x, x+xn);
  xn = unique(x, x+xn)-x;
  sort(mouse+1, mouse+n+1);
  for(int i=1; i<=m; i++) {
    y[i] = lower_bound(x, x+xn, y[i])-x;
    hole[y[i]]+=c[i];
  }
  LL ans=0;
  for(int i=1, L, R, pos; i<=n; i++) {
    pos = lower_bound(x, x+xn, mouse[i])-x;
    L = ::L(pos);
    R = ::R(pos);
    if(L == inf && R == inf) {
      puts("-1");
      return 0;
    }
    if(L<R) {
      ans+=L;
      goL(pos);
    }else {
      ans+=R;
      goR(pos);
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
using LL = long long;
using pii = pair<int, int>;

struct data{
  LL val;
  int L, R, id;
  data(int L=0, int R=0, int id=0, LL val=0) : L(L), R(R), id(id), val(val) {}
};

const int N = 5050, M = 5e6;
data d[M], f[M];
int x[N];
pii p[N];
int s[N];

int main() {
#ifdef TRUE
  freopen("F.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i=1; i<=n; i++) {
    scanf("%d", x+i);
  }
  sort(x+1, x+n+1);
  for(int i=1; i<=m; i++) {
    scanf("%d %d", &p[i].x, &p[i].y);
  }
  sort(p+1, p+m+1);
  int dn=0, fn;
  for(int i=1; i<=m; i++) {
    s[i] = s[i-1]+p[i].y;
  }
  for(int i=1; i<=m; i++) {
    d[i-1] = data(s[i-1]+1, s[i], i, abs(x[1]-p[i].x));
    if(i>1) {
      d[i-1].val = min(d[i-1].val, d[i-2].val);
    }
  }
  dn=m;
  for(int i=2; i<=n; i++) {
    fn=0;
    for(int j=0, id; j<dn; j++) {
      id = d[j].id;
      if(d[j].L < s[id]) {
        f[fn++]=data(d[j].L+1, min(s[id], d[j].R+1), id, d[j].val+abs(x[i]-p[id].x));
      }
      if(s[id] == d[j].R && id<m) {
        f[fn++]=data(s[id]+1, s[id]+1, id+1, d[j].val+abs(x[i]-p[id+1].x));
      }
    }
    dn=0;
    for(int j=0; j<fn; j++) {
      if(dn) {
        f[j].val = min(f[j].val, d[dn-1].val);
        if(f[j].id != d[dn-1].id || f[j].val < d[dn-1].val) {
          d[dn++]=f[j];
        }else {
          d[dn-1].R = f[j].R;
        }
      }else {
        d[dn++]=f[j];
      }
    }
  }
  cout<<d[dn-1].val<<endl;
  return 0;
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}

#define NN 10101
#define MM 50050050
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

int x[NN];
int p[NN], c[NN];

PQ q;
int pr_v[NN], pr_e[NN], V, E;
int head[NN], nxt[MM], to[MM], cap[MM], cost[MM];
INT dis[NN], work[NN];

inline void add(int u, int v, int f, int c) {
	to[E]=v, cap[E]=f, cost[E]=c, nxt[E]=head[u], head[u]=E++;
	to[E]=u, cap[E]=0, cost[E]=-c, nxt[E]=head[u], head[u]=E++;
}

inline void minCost(int s, int t, int m_x) {
	int u, v, e, i=0, flow;
	INT m_n, Cost=0;
	while(m_x) {
		for(i=0; i<=V; i++) dis[i]=inf;
		q=PQ(), q.push({0, s}), dis[s]=0;
		while(!q.empty()) {
			u=q.top().y, m_n=q.top().x, q.pop();
			if(dis[u]<m_n) continue;
			for(e=head[u]; ~e; e=nxt[e]) {
				v=to[e], m_n=dis[u]+cost[e]+work[u]-work[v];
				if(cap[e]>0 && dis[v]>m_n) {
					dis[v]=m_n;
					pr_v[v]=u, pr_e[v]=e;
					q.push({dis[v], v});
				}
			}
		}
		if(dis[t]==inf) break;
		for(i=0; i<=V; i++) work[i]+=dis[i];
		flow=m_x;
		for(u=t; u!=s; u=pr_v[u]) smin(flow, cap[pr_e[u]]);
		m_x-=flow, Cost+=flow*work[t];
		for(u=t; u!=s; u=pr_v[u]) {
			cap[pr_e[u]]-=flow, cap[pr_e[u]^1]+=flow;
		}
	}
	if(m_x) puts("-1");
	else printf("%I64d", Cost);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n, m; scanf("%d %d", &n, &m);
	for(int i=1; i<=n; i++) scanf("%d", x+i);
	for(int i=1; i<=m; i++) scanf("%d %d", p+i, c+i);

	int s=0, t=n+m+1; V=t;
	for(int i=0; i<=V; i++) head[i]=-1;

	for(int i=1; i<=n; i++) {
		add(s, i, 1, 0);
		for(int j=1; j<=m; j++) {
			add(i, n+j, 1, abs(x[i]-p[j]));
		}
	}
	for(int i=1; i<=m; i++) add(n+i, t, c[i], 0);
	minCost(s, t, n);
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

struct edge {
	int to, capacity, cost, rev;
	edge() {}
	edge(int to,int _capacity,int _cost,int _rev):
		to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};

#define NN 10002

int x[5001];
int p[5001], c[5001];
PQ q;

struct Min_Cost_Max_Flow {
	int V, PreV[NN], PreE[NN];
	INT H[NN], dis[NN];
	vector<edge> G[NN];
   	inline void solve() {
   		int n, m; scanf("%d %d", &n, &m);
   		for(int i=1; i<=n; i++) scanf("%d", x+i);
   		int ans=0;
   		for(int i=1; i<=m; i++) {
		   	scanf("%d %d", p+i, c+i);
		   	ans+=c[i];
		}
		if(ans<n) puts("-1"), exit(0);

   		int s=0, t=n+m+1; V=t;
   		for(int i=1; i<=n; i++) {
   			add(s, i, 1, 0);
   			for(int j=1; j<=m; j++) {
   				add(i, n+j, 1, abs(x[i]-p[j]));
   			}
   		}
   		for(int i=1; i<=m; i++) add(n+i, t, min(n, c[i]), 0);
   		minCost(s, t, n);
   	}
	void add(int from, int to, int cap, int cost) {
		G[from].push_back(edge(to, cap, cost, G[to].size()));
		G[to].push_back(edge(from,0, -cost, G[from].size() - 1));
	}
	void minCost(int s, int t, int f) {
		INT res = 0;
		while (f) {
			q=PQ();
   			fill(dis, dis + 1 + V, inf);
   			dis[s] = 0; q.push(pii(0, s));
			while (!q.empty()) {
   				pii now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] < now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
   					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
   						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
   						PreV[e.to] = v; PreE[e.to] = i;
   						q.push(pii(dis[e.to], e.to));
	   				}
	   			}
	   		}
			if (dis[t] == inf)   break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
   			f -= d; res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
   				edge& e = G[PreV[v]][PreE[v]];
   				e.capacity -= d;
   				G[v][e.rev].capacity += d;
			}
   		}
   		if(f) puts("-1");
   		else printf("%I64d\n", res);
   	}
} mcmf;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	mcmf.solve();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=11111;
const int MM=25200000;
const int inf=0x3f3f3f3f;
const INT INF=0x3f3f3f3f3f3f3f3fll;

INT dis[NN],src,tar;
int E, head[NN], to[MM], nxt[MM], pre[NN];
int a[NN],b[NN],c[NN];
int flow[MM];
int cost[MM];

void add_edge(int u, int v, int f, int c){
	nxt[E]=head[u]; to[E]=v; flow[E]=f; cost[E]=c; head[u]=E++;
	nxt[E]=head[v]; to[E]=u; flow[E]=0; cost[E]=-c; head[v]=E++;
}

queue <int> q;

INT C;

INT maxflow(int s, int t){
	INT ans=0, u, v;
	int  bot;
	while(1) {
		q=queue<int> ();
		q.push(s);
		memset(dis, inf, sizeof(dis));
		dis[s]=0;
		while(!q.empty()){
			u=q.front();
			q.pop();
			for(int e=head[u]; ~e; e=nxt[e]){
				v=to[e];
				if(flow[e]==0) continue;
				if(dis[v]>dis[u]+cost[e]) {
					dis[v]=dis[u]+cost[e];
					q.push(v);
					pre[v]=e;
				}
			}
		}

		if(dis[t]==INF) return ans;
		bot=inf;
		for(u=t; u!=s; u=to[pre[u]^1]) bot=min(bot, flow[pre[u]]);
		for(u=t; u!=s; u=to[pre[u]^1]) {
			flow[pre[u]]-=bot;
			flow[pre[u]^1]+=bot;
		}
		C+=dis[t];
		ans+=bot;
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n,m; cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)scanf("%d%d",&b[i],&c[i]);

	memset(head,-1,sizeof head);
	src=0; tar=n+m+1;

	for(int i=1;i<=n;i++){
		add_edge(src,i,1,0);
		for(int j=1;j<=m;j++){
			add_edge(i,n+j,1,abs(a[i]-b[j]));
		}
	}
	for(int j=1;j<=m;j++){
		add_edge(n+j,tar,c[j],0);
	}

	if(maxflow(src,tar)<n){
		puts("-1");
		return 0;
	}
	cout<<C<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 10010
#define inf 2e9+1
using namespace std;

typedef long long INT;

int pos[NN], sum[NN];

map <int, int> mp;

int nn = 1;

void go_left(int u){
	for(int i = u - 1; i; i--){
		if(sum[i]) sum[i]--;
		if(mp[pos[i]] > 0) {mp[pos[i]]--; return ;}
	}
}

void go_right(int u){
	for(int i = u + 1; i <= nn; i++){
		if(mp[pos[i]] > 0) {mp[pos[i]]--; return ;}
		sum[i]++;
	}
}

int calc_left(int u){
	int ans = 0;
	for(int i = u - 1; i; i--){
		if(sum[i] == 0) ans += pos[i + 1] - pos[i];
		else ans -= pos[i + 1] - pos[i];
		if(mp[pos[i]] > 0) return ans;
	}
	return inf;
}

int calc_right(int u){
	int ans = 0;
	for(int i = u + 1; i <= nn; i++){
		ans += pos[i + 1] - pos[i];
		if(mp[pos[i]] > 0) return ans;
	}
	return inf;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	INT n, m;
	scanf("%I64d%I64d", &n, &m);
	for(int i = 1; i <= n; i++){
		int u;
		scanf("%d", &u);
		pos[nn++] = u;
		mp[u]--;
	}
	for(int i = 1; i <= m; i++){
		int p, val;
		scanf("%d%d", &p, &val);
		pos[nn++] = p, mp[p] += val;
	}
	sort(pos + 1, pos + nn);
	nn = unique(pos + 1, pos + nn) - pos - 1;
////	for(int i = 1; i <= nn; i++) cerr<<pos[i]<<" "<<mp[pos[i]]<<endl;
	INT ans = 0;
	for(int i = 1; i <= nn; i++){
		while(mp[pos[i]] < 0){
			int dl = calc_left(i);
			int dr = calc_right(i);
			if(dl <= dr){
				ans += dl;
				go_left(i);
			} else{
				ans += dr;
				go_right(i);
			}
			mp[pos[i]]++;
		}
//		cerr<<ans<<endl;
	}

	printf("%I64d\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=11111;
const int MM=25500000;
const int inf=0x3f3f3f3f;
const INT INF=0x3f3f3f3f3f3f3f3fll;

INT dis[NN],src,tar;
int E, head[NN], to[MM], nxt[MM], pre[NN];
int a[NN],b[NN],c[NN];
INT flow[MM];
int cost[MM];

void add_edge(int u, int v, int f, int c){
	nxt[E]=head[u]; to[E]=v; flow[E]=f; cost[E]=c; head[u]=E++;
	nxt[E]=head[v]; to[E]=u; flow[E]=0; cost[E]=-c; head[v]=E++;
}

queue <int> q;

INT C;

INT maxflow(int s, int t){
	INT ans=0, u, v, bot;
	while(1) {
		q=queue<int> ();
		q.push(s);
		memset(dis, inf, sizeof(dis));
		dis[s]=0;
		while(!q.empty()){
			u=q.front();
			q.pop();
			for(int e=head[u]; ~e; e=nxt[e]){
				v=to[e];
				if(flow[e]==0) continue;
				if(dis[v]>dis[u]+cost[e]) {
					dis[v]=dis[u]+cost[e];
					q.push(v);
					pre[v]=e;
				}
			}
		}

		if(dis[t]==INF) return ans;
		bot=INF;
		for(u=t; u!=s; u=to[pre[u]^1]) bot=min(bot, flow[pre[u]]);
		for(u=t; u!=s; u=to[pre[u]^1]) {
			flow[pre[u]]-=bot;
			flow[pre[u]^1]+=bot;
		}
		C+=dis[t];
		ans+=bot;
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n,m; cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)scanf("%d%d",&b[i],&c[i]);

	memset(head,-1,sizeof head);
	src=0; tar=n+m+1;

	for(int i=1;i<=n;i++){
		add_edge(src,i,1,0);
		for(int j=1;j<=m;j++){
			add_edge(i,n+j,1,abs(a[i]-b[j]));
		}
	}
	for(int j=1;j<=m;j++){
		add_edge(n+j,tar,c[j],0);
	}

	if(maxflow(src,tar)<n){
		puts("-1");
		return 0;
	}
	cout<<C<<endl;

	return 0;
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

struct edge {
	int to, capacity, cost, rev;
	edge() {}
	edge(int to,int _capacity,int _cost,int _rev):
		to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};

#define NN 101010

int x[5001], arr[NN];
int p[5001], c[5001];
PQ q;

struct Min_Cost_Max_Flow {
	int V, PreV[NN], PreE[NN];
	INT H[NN], dis[NN];
	vector<edge> G[NN];
   	inline void solve() {
   		int n, m; scanf("%d %d", &n, &m);
   		int nn=0;
   		for(int i=1; i<=n; i++) {
		   	scanf("%d", x+i), arr[nn++]=x[i];
		}
   		for(int i=1; i<=m; i++) {
		   	scanf("%d %d", p+i, c+i);
		   	arr[nn++]=p[i];
		}
   		sort(arr, arr+nn);
   		nn=unique(arr, arr+nn)-arr;

		int s=nn, t=s+1; V=t;

   		for(int i=1; i<nn; i++) {
   			int d=arr[i]-arr[i-1];
   			add(i-1, i, n, n, d, d);
   		}
   		for(int i=1; i<=n; i++) {
   			int id=lower_bound(arr, arr+nn, x[i])-arr;
   			add(s, id, 1, 0, 0, 0);
   		}
   		for(int i=1; i<=m; i++) {
   			int id=lower_bound(arr, arr+nn, p[i])-arr;
   			add(id, t, c[i], 0, 0, 0);
   		}
   		minCost(s, t, n);
   	}
	void add(int from, int to, int cap, int rev, int cost, int Rev) {
		G[from].push_back(edge(to, cap, cost, G[to].size()));
		G[to].push_back(edge(from, rev, Rev, G[from].size() - 1));
	}
	void minCost(int s, int t, int f) {
		INT res = 0;
		while (f) {
			q=PQ();
   			fill(dis, dis + 1 + V, inf);
   			dis[s] = 0; q.push(pii(0, s));
			while (!q.empty()) {
   				pii now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] < now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
   					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
   						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
   						PreV[e.to] = v; PreE[e.to] = i;
   						q.push(pii(dis[e.to], e.to));
	   				}
	   			}
	   		}
			if (dis[t] == inf)   break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v]) d = min(d, G[PreV[v]][PreE[v]].capacity);
   			f -= d; res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
   				edge& e = G[PreV[v]][PreE[v]];
   				e.capacity -= d;
   				G[v][e.rev].capacity += d;
			}
   		}
   		if(f) puts("-1");
   		else printf("%I64d\n", res);
   	}
} mcmf;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	mcmf.solve();
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

struct edge {
	int to, capacity, cost, rev;
	edge() {}
	edge(int to,int _capacity,int _cost,int _rev):
		to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};

#define NN 10002

int x[5001];
int p[5001], c[5001];
PQ q;

struct Min_Cost_Max_Flow {
	int V, PreV[NN], PreE[NN];
	INT H[NN], dis[NN];
	vector<edge> G[NN];
   	inline void solve() {
   		int n, m; scanf("%d %d", &n, &m);
   		for(int i=1; i<=n; i++) scanf("%d", x+i);
   		int ans=0;
   		for(int i=1; i<=m; i++) {
		   	scanf("%d %d", p+i, c+i);
		   	ans+=c[i];
	}
		if(ans<n) puts("-1"), exit(0);

   		int s=0, t=n+m+1; V=t;
   		for(int i=1; i<=n; i++) {
   			add(s, i, 1, 0);
   			for(int j=1; j<=m; j++) {
   				add(i, n+j, 1, abs(x[i]-p[j]));
   			}
   		}
   		for(int i=1; i<=m; i++) add(n+i, t, c[i], 0);
   		minCost(s, t, n);
   	}
	void add(int from, int to, int cap, int cost) {
		G[from].push_back(edge(to, cap, cost, G[to].size()));
		G[to].push_back(edge(from,0, -cost, G[from].size() - 1));
	}
	void minCost(int s, int t, int f) {
		INT res = 0;
		while (f) {
			q=PQ();
   			fill(dis, dis + 1 + V, inf);
   			dis[s] = 0; q.push(pii(0, s));
			while (!q.empty()) {
   				pii now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] < now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
   					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
   						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
   						PreV[e.to] = v; PreE[e.to] = i;
   						q.push(pii(dis[e.to], e.to));
	   				}
	   			}
	   		}
			if (dis[t] == inf)   break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
   			f -= d; res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
   				edge& e = G[PreV[v]][PreE[v]];
   				e.capacity -= d;
   				G[v][e.rev].capacity += d;
			}
   		}
   		if(f) puts("-1");
   		else printf("%I64d\n", res);
   	}
} mcmf;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	mcmf.solve();
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 10010
#define inf 2e9+1
using namespace std;

typedef long long INT;

int pos[NN], sum[NN];

map <int, int> mp;

int nn = 1;

void go_left(int u){
	for(int i = u - 1; i; i--){
		if(sum[i]) sum[i]--;
		if(mp[pos[i]] > 0) {mp[pos[i]]--; return ;}
	}
}

void go_right(int u){
	for(int i = u + 1; i <= nn; i++){
		if(mp[pos[i]] > 0) {mp[pos[i]]--; return ;}
		sum[i]++;
	}
}

int calc_left(int u){
	int ans = 0;
	for(int i = u - 1; i; i--){
		if(sum[i] == 0) ans += pos[i + 1] - pos[i];
		else ans -= pos[i + 1] - pos[i];
		if(mp[pos[i]] > 0) return ans;
	}
	return inf;
}

int calc_right(int u){
	int ans = 0;
	for(int i = u + 1; i <= nn; i++){
		ans += pos[i + 1] - pos[i];
		if(mp[pos[i]] > 0) return ans;
	}
	return inf;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	INT n, m;
	scanf("%I64d%I64d", &n, &m);
	for(int i = 1; i <= n; i++){
		int u;
		scanf("%d", &u);
		pos[nn++] = u;
		mp[u]--;
	}
	for(int i = 1; i <= m; i++){
		int p, val;
		scanf("%d%d", &p, &val);
		pos[nn++] = p, mp[p] += val;
	}
	sort(pos + 1, pos + nn);
	nn = unique(pos + 1, pos + nn) - pos - 1;
////	for(int i = 1; i <= nn; i++) cerr<<pos[i]<<" "<<mp[pos[i]]<<endl;
	INT ans = 0;
	for(int i = 1; i <= nn; i++){
		while(mp[pos[i]] < 0){
			int dl = calc_left(i);
			int dr = calc_right(i);
			if(dl == inf && dr == inf) {puts("-1"); return 0;}
			if(dl <= dr){
				ans += dl;
				go_left(i);
			} else{
				ans += dr;
				go_right(i);
			}
			mp[pos[i]]++;
		}
//		cerr<<ans<<endl;
	}

	printf("%I64d\n", ans);
	return 0;
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}

#define MM 25010000
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

int x[5001];
int p[5001], c[5001];

#define NN 10002

PQ q;
int pr_v[NN], pr_e[NN], V, E;
int head[NN], nxt[MM], to[MM], cap[MM], cost[MM];
INT dis[NN], work[NN];

inline void add(int u, int v, int f, int c) {
	to[E]=v, cap[E]=f, cost[E]=c, nxt[E]=head[u], head[u]=E++;
	to[E]=u, cap[E]=0, cost[E]=-c, nxt[E]=head[u], head[u]=E++;
}

inline void minCost(int s, int t, int m_x) {
	int u, v, e, i=0, flow;
	INT m_n, Cost=0;
	while(m_x) {
		for(i=0; i<=V; i++) dis[i]=inf;
		q=PQ(), q.push({0, s}), dis[s]=0;
		while(!q.empty()) {
			u=q.top().y, m_n=q.top().x, q.pop();
			if(dis[u]<m_n) continue;
			for(e=head[u]; ~e; e=nxt[e]) {
				v=to[e], m_n=dis[u]+cost[e]+work[u]-work[v];
				if(cap[e]>0 && dis[v]>m_n) {
					dis[v]=m_n;
					pr_v[v]=u, pr_e[v]=e;
					q.push({dis[v], v});
				}
			}
		}
		if(dis[t]==inf) break;
		for(i=0; i<=V; i++) work[i]+=dis[i];
		flow=m_x;
		for(u=t; u!=s; u=pr_v[u]) smin(flow, cap[pr_e[u]]);
		m_x-=flow, Cost+=flow*work[t];
		for(u=t; u!=s; u=pr_v[u]) {
			cap[pr_e[u]]-=flow, cap[pr_e[u]^1]+=flow;
		}
	}
	if(m_x) puts("-1");
	else printf("%I64d", Cost);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n, m; scanf("%d %d", &n, &m);
	for(int i=1; i<=n; i++) scanf("%d", x+i);
	for(int i=1; i<=m; i++) scanf("%d %d", p+i, c+i);

	int s=0, t=n+m+1; V=t;
	for(int i=0; i<=V; i++) head[i]=-1;

	for(int i=1; i<=n; i++) {
		add(s, i, 1, 0);
		for(int j=1; j<=m; j++) {
			add(i, n+j, 1, abs(x[i]-p[j]));
		}
	}
	for(int i=1; i<=m; i++) add(n+i, t, c[i], 0);
	minCost(s, t, n);
}


#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
using LL = long long;
using pii = pair<int, int>;

struct data{
  LL val;
  int L, R, id;
  data(int L=0, int R=0, int id=0, LL val=0) : L(L), R(R), id(id), val(val) {}
};

const int N = 5050, M = 5e6;
data d[M], f[M];
int x[N];
pii p[N];
int s[N];

int main() {
#ifdef TRUE
  freopen("F.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i=1; i<=n; i++) {
    scanf("%d", x+i);
  }
  sort(x+1, x+n+1);
  for(int i=1; i<=m; i++) {
    scanf("%d %d", &p[i].x, &p[i].y);
  }
  sort(p+1, p+m+1);
  int dn=0, fn;
  for(int i=1; i<=m; i++) {
    s[i] = s[i-1]+p[i].y;
  }
  if(n > s[m]) {
    puts("-1");
    return 0;
  }
  for(int i=1; i<=m; i++) {
    d[i-1] = data(s[i-1]+1, s[i], i, abs(x[1]-p[i].x));
    if(i>1) {
      d[i-1].val = min(d[i-1].val, d[i-2].val);
    }
  }
  dn=m;
  for(int i=2; i<=n; i++) {
    fn=0;
    for(int j=0, id; j<dn; j++) {
      id = d[j].id;
      if(d[j].L < s[id]) {
        f[fn++]=data(d[j].L+1, min(s[id], d[j].R+1), id, d[j].val+abs(x[i]-p[id].x));
      }
      if(s[id] == d[j].R && id<m) {
        f[fn++]=data(s[id]+1, s[id]+1, id+1, d[j].val+abs(x[i]-p[id+1].x));
      }
    }
    dn=0;
    for(int j=0; j<fn; j++) {
      if(dn) {
        f[j].val = min(f[j].val, d[dn-1].val);
        if(f[j].id != d[dn-1].id || f[j].val < d[dn-1].val) {
          d[dn++]=f[j];
        }else {
          d[dn-1].R = f[j].R;
        }
      }else {
        d[dn++]=f[j];
      }
    }
  }
  cout<<d[dn-1].val<<endl;
  return 0;
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}

struct edge {
	int to, capacity, cost, rev;
	edge() {}
	edge(int to,int _capacity,int _cost,int _rev):
		to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};

#define NN 10101

int x[NN];
int p[NN], c[NN];

struct Min_Cost_Max_Flow {
	int V, PreV[NN +5], PreE[NN + 5];
	INT H[NN+5], dis[NN+5];
	vector<edge> G[NN + 5];
   	inline void solve() {
   		int n, m; scanf("%d %d", &n, &m);
   		for(int i=1; i<=n; i++) scanf("%d", x+i);
   		for(int i=1; i<=m; i++) scanf("%d %d", p+i, c+i);

   		int s=0, t=n+m+1; V=t;
   		for(int i=1; i<=n; i++) {
   			add(s, i, 1, 0);
   			for(int j=1; j<=m; j++) {
   				add(i, n+j, 1, abs(x[i]-p[j]));
   			}
   		}
   		for(int i=1; i<=m; i++) add(n+i, t, c[i], 0);
   		minCost(s, t, n);
   	}
	void add(int from, int to, int cap, int cost) {
		G[from].push_back(edge(to, cap, cost, G[to].size()));
		G[to].push_back(edge(from,0, -cost, G[from].size() - 1));
	}
	void minCost(int s, int t, int f) {
		INT res = 0;
		while (f) {
   			priority_queue <pii, vector<pii>, greater<pii> > q;
   			fill(dis, dis + 1 + V, inf);
   			dis[s] = 0; q.push(pii(0, s));
			while (!q.empty()) {
   				pii now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] < now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
   					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
   						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
   						PreV[e.to] = v; PreE[e.to] = i;
   						q.push(pii(dis[e.to], e.to));
	   				}
	   			}
	   		}
			if (dis[t] == inf)   break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
   			f -= d; res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
   				edge& e = G[PreV[v]][PreE[v]];
   				e.capacity -= d;
   				G[v][e.rev].capacity += d;
			}
   		}
   		if(f) puts("-1");
   		else printf("%I64d\n", res);
   	}
} mcmf;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	mcmf.solve();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=11111;
const int MM=22222222;
const int inf=0x3f3f3f3f;

int dis[NN],src,tar;
int E, head[NN], to[MM], nxt[MM], pre[NN];
int a[NN],b[NN],c[NN];
int flow[MM];
int cost[MM];

void add_edge(int u, int v, int f, int c){
	nxt[E]=head[u]; to[E]=v; flow[E]=f; cost[E]=c; head[u]=E++;
	nxt[E]=head[v]; to[E]=u; flow[E]=0; cost[E]=-c; head[v]=E++;
}

queue <int> q;

INT C;

INT maxflow(int s, int t){
	INT ans=0, u, v;
	int bot;
	while(1) {
		q=queue<int> ();
		q.push(s);
		memset(dis, inf, sizeof(dis));
		dis[s]=0;
		while(!q.empty()){
			u=q.front();
			q.pop();
			for(int e=head[u]; ~e; e=nxt[e]){
				v=to[e];
				if(flow[e]==0) continue;
				if(dis[v]>dis[u]+cost[e]) {
					dis[v]=dis[u]+cost[e];
					q.push(v);
					pre[v]=e;
				}
			}
		}

		if(dis[t]==inf) return ans;
		bot=inf;
		for(u=t; u!=s; u=to[pre[u]^1]) bot=min(bot, flow[pre[u]]);
		for(u=t; u!=s; u=to[pre[u]^1]) {
			flow[pre[u]]-=bot;
			flow[pre[u]^1]+=bot;
		}
		C+=dis[t];
		ans+=bot;
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n,m; cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)scanf("%d%d",&b[i],&c[i]);

	memset(head,-1,sizeof head);
	src=0; tar=n+m+1;

	for(int i=1;i<=n;i++){
		add_edge(src,i,1,0);
		for(int j=1;j<=m;j++){
			add_edge(i,n+j,1,abs(a[i]-b[j]));
		}
	}
	for(int j=1;j<=m;j++){
		add_edge(n+j,tar,c[j],0);
	}

	if(maxflow(src,tar)<n){
		puts("-1");
		return 0;
	}
	cout<<C<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 10101;
const int inf = 0x3f3f3f3f;
int x[N], xn, mouse[N], y[N], c[N];
int hole[N];
int rev[N];

int L(int p) {
  for(int ans = 0; p>=0; p--) {
    if(hole[p]>0) {
      return ans;
    }
    if(p) {
      if(rev[p]) {
        ans-=x[p]-x[p-1];
      }else {
        ans+=x[p]-x[p-1];
      }
    }
  }
  return inf;
}

int R(int p) {
  for(int ans = 0; p<xn; p++) {
    if(hole[p]>0) {
      return ans;
    }
    ans+=x[p+1]-x[p];
  }
  return inf;
}

void goL(int p) {
  for(; p>=0; p--) {
    if(hole[p]>0) {
      hole[p]--;
      return;
    }
    if(rev[p]) {
      rev[p]--;
    }
  }
}

void goR(int p) {
  for(; p<xn; p++) {
    if(hole[p]>0) {
      hole[p]--;
      return;
    }
    rev[p+1]++;
  }
}

int main() {
#ifdef TRUE
  freopen("F.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n, m;
  scanf("%d %d", &n, &m);
  for(int i=1; i<=n; i++) {
    scanf("%d", mouse+i);
    x[xn++]=mouse[i];
  }
  for(int i=1; i<=m; i++) {
    scanf("%d %d", y+i, c+i);
    x[xn++]=y[i];
  }
  sort(x, x+xn);
  xn = unique(x, x+xn)-x;
  for(int i=1; i<=m; i++) {
    y[i] = lower_bound(x, x+xn, y[i])-x;
    hole[y[i]]+=c[i];
  }
  for(int i=1, pos; i<=n; i++) {
    pos = lower_bound(x, x+xn, mouse[i])-x;
    hole[pos]--;
  }
  LL ans=0;
  for(int i=0; i<xn; i++) {
    for(int L, R; hole[i]<0;) {
      L = ::L(i);
      R = ::R(i);
      if(L == inf && R == inf) {
        puts("-1");
        return 0;
      }
      if(L<R) {
        ans+=L;
        goL(i);
      }else {
        ans+=R;
        goR(i);
      }
      hole[i]++;
    }
  }
  cout<<ans<<endl;
  return 0;
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}

struct edge {
	int to, capacity, cost, rev;
	edge() {}
	edge(int to,int _capacity,int _cost,int _rev):
		to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};

#define NN 10002

int x[5001];
int p[5001], c[5001];

struct Min_Cost_Max_Flow {
	int V, PreV[NN], PreE[NN];
	INT H[NN], dis[NN];
	vector<edge> G[NN];
   	inline void solve() {
   		int n, m; scanf("%d %d", &n, &m);
   		for(int i=1; i<=n; i++) scanf("%d", x+i);
   		for(int i=1; i<=m; i++) scanf("%d %d", p+i, c+i);

   		int s=0, t=n+m+1; V=t;
   		for(int i=1; i<=n; i++) {
   			add(s, i, 1, 0);
   			for(int j=1; j<=m; j++) {
   				add(i, n+j, 1, abs(x[i]-p[j]));
   			}
   		}
   		for(int i=1; i<=m; i++) add(n+i, t, c[i], 0);
   		minCost(s, t, n);
   	}
	void add(int from, int to, int cap, int cost) {
		G[from].push_back(edge(to, cap, cost, G[to].size()));
		G[to].push_back(edge(from,0, -cost, G[from].size() - 1));
	}
	void minCost(int s, int t, int f) {
		INT res = 0;
		while (f) {
   			priority_queue <pii, vector<pii>, greater<pii> > q;
   			fill(dis, dis + 1 + V, inf);
   			dis[s] = 0; q.push(pii(0, s));
			while (!q.empty()) {
   				pii now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] < now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
   					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
   						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
   						PreV[e.to] = v; PreE[e.to] = i;
   						q.push(pii(dis[e.to], e.to));
	   				}
	   			}
	   		}
			if (dis[t] == inf)   break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
   			f -= d; res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
   				edge& e = G[PreV[v]][PreE[v]];
   				e.capacity -= d;
   				G[v][e.rev].capacity += d;
			}
   		}
   		if(f) puts("-1");
   		else printf("%I64d\n", res);
   	}
} mcmf;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	mcmf.solve();
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

struct edge {
	int to, capacity, cost, rev;
	edge() {}
	edge(int to,int _capacity,int _cost,int _rev):
		to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};

#define NN 101010

int x[5001], arr[NN];
int p[5001], c[5001];
PQ q;

struct Min_Cost_Max_Flow {
	int V, PreV[NN], PreE[NN];
	INT H[NN], dis[NN];
	vector<edge> G[NN];
   	inline void solve() {
   		int n, m; scanf("%d %d", &n, &m);
   		int nn=0;
   		for(int i=1; i<=n; i++) {
		   	scanf("%d", x+i), arr[nn++]=x[i];
		}
   		for(int i=1; i<=m; i++) {
		   	scanf("%d %d", p+i, c+i);
		   	arr[nn++]=p[i];
		}
   		sort(arr, arr+nn);
   		nn=unique(arr, arr+nn)-arr;

		int s=nn, t=s+1; V=t;

   		for(int i=1; i<nn; i++) {
   			int d=arr[i]-arr[i-1];
   			add(i-1, i, n, 0, d, -d);
   			add(i, i-1, n, 0, d, -1);
   		}
   		for(int i=1; i<=n; i++) {
   			int id=lower_bound(arr, arr+nn, x[i])-arr;
   			add(s, id, 1, 0, 0, 0);
   		}
   		for(int i=1; i<=m; i++) {
   			int id=lower_bound(arr, arr+nn, p[i])-arr;
   			add(id, t, c[i], 0, 0, 0);
   		}
   		minCost(s, t, n);
   	}
	void add(int from, int to, int cap, int rev, int cost, int Rev) {
		G[from].push_back(edge(to, cap, cost, G[to].size()));
		G[to].push_back(edge(from, rev, Rev, G[from].size() - 1));
	}
	void minCost(int s, int t, int f) {
		INT res = 0;
		while (f) {
			q=PQ();
   			fill(dis, dis + 1 + V, inf);
   			dis[s] = 0; q.push(pii(0, s));
			while (!q.empty()) {
   				pii now = q.top(); q.pop();
				int v = now.second;
				if (dis[v] < now.first)continue;
				for (int i = 0; i < G[v].size(); ++i) {
   					edge& e = G[v][i];
					if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
   						dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
   						PreV[e.to] = v; PreE[e.to] = i;
   						q.push(pii(dis[e.to], e.to));
	   				}
	   			}
	   		}
			if (dis[t] == inf)   break;
			for (int i = 0; i <= V; ++i)H[i] += dis[i];
			int d = f;
			for (int v = t; v != s; v = PreV[v]) d = min(d, G[PreV[v]][PreE[v]].capacity);
   			f -= d; res += d*H[t];
			for (int v = t; v != s; v = PreV[v]) {
   				edge& e = G[PreV[v]][PreE[v]];
   				e.capacity -= d;
   				G[v][e.rev].capacity += d;
			}
   		}
   		if(f) puts("-1");
   		else printf("%I64d\n", res);
   	}
} mcmf;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	mcmf.solve();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=11111;
const int MM=33333333;
const int inf=0x3f3f3f3f;
const INT INF=0x3f3f3f3f3f3f3f3fll;

INT dis[NN],src,tar;
INT E, head[NN], to[MM], nxt[MM], flow[MM], cost[MM], pre[NN];
int a[NN],b[NN],c[NN];

void add_edge(int u, int v, int f, int c){
	nxt[E]=head[u]; to[E]=v; flow[E]=f; cost[E]=c; head[u]=E++;
	nxt[E]=head[v]; to[E]=u; flow[E]=0; cost[E]=-c; head[v]=E++;
}

queue <int> q;

INT C;

INT maxflow(int s, int t){
	INT ans=0, u, v, bot;
	while(1) {
		q=queue<int> ();
		q.push(s);
		memset(dis, inf, sizeof(dis));
		dis[s]=0;
		while(!q.empty()){
			u=q.front();
			q.pop();
			for(int e=head[u]; ~e; e=nxt[e]){
				v=to[e];
				if(flow[e]==0) continue;
				if(dis[v]>dis[u]+cost[e]) {
					dis[v]=dis[u]+cost[e];
					q.push(v);
					pre[v]=e;
				}
			}
		}

		if(dis[t]==INF) return ans;
		bot=inf;
		for(u=t; u!=s; u=to[pre[u]^1]) bot=min(bot, flow[pre[u]]);
		for(u=t; u!=s; u=to[pre[u]^1]) {
			flow[pre[u]]-=bot;
			flow[pre[u]^1]+=bot;
		}
		C+=dis[t];
		ans+=bot;
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n,m; cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)scanf("%d%d",&b[i],&c[i]);

	memset(head,-1,sizeof head);
	src=0; tar=n+m+1;

	for(int i=1;i<=n;i++){
		add_edge(src,i,1,0);
		for(int j=1;j<=m;j++){
			add_edge(i,n+j,1,abs(a[i]-b[j]));
		}
	}
	for(int j=1;j<=m;j++){
		add_edge(n+j,tar,c[j],0);
	}

	if(maxflow(src,tar)<n){
		puts("-1");
		return 0;
	}
	cout<<C<<endl;

	return 0;
}


#define inf 0x3f3f3f3f3f3f3f3f
#include<bits/stdc++.h>
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<INT, int>;
template<typename A, typename B> inline void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> inline void smax(A &a, const B &b) {if(a<b) a=b;}

#define MM 16010000
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

int x[5001];
int p[5001], c[5001];

#define NN 10002

PQ q;
vector<int> G[NN];
int to[MM], cap[MM], cost[MM], V, E;
INT dis[NN], work[NN];
int pr_v[NN], pr_e[NN];

inline void add(int u, int v, int f, int c) {
	to[E]=v, cap[E]=f, cost[E]=c, G[u].pb(E++);
	to[E]=u, cap[E]=0, cost[E]=c, G[u].pb(E++);
}

inline void minCost(int s, int t, int m_x) {
	int u, v, e, i=0, flow;
	INT m_n, Cost=0;
	while(m_x) {
		for(i=0; i<=V; i++) dis[i]=inf;
		q=PQ(), q.push({0, s}), dis[s]=0;
		while(!q.empty()) {
			u=q.top().y, m_n=q.top().x, q.pop();
			if(dis[u]<m_n) continue;
			for(i=G[u].size(); i--; ) {
				e=G[u][i], v=to[e];
				m_n=dis[u]+cost[e]+work[u]-work[v];
				if(cap[e]>0 && dis[v]>m_n) {
					dis[v]=m_n;
					pr_v[v]=u, pr_e[v]=e;
					q.push({dis[v], v});
				}
			}
		}
		if(dis[t]==inf) break;
		for(i=0; i<=V; i++) work[i]+=dis[i];
		flow=m_x;
		for(u=t; u!=s; u=pr_v[u]) smin(flow, cap[pr_e[u]]);
		m_x-=flow, Cost+=flow*work[t];
		for(u=t; u!=s; u=pr_v[u]) {
			cap[pr_e[u]]-=flow, cap[pr_e[u]^1]+=flow;
		}
	}
	if(m_x) puts("-1");
	else printf("%I64d", Cost);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n, m; scanf("%d %d", &n, &m);
	for(int i=1; i<=n; i++) scanf("%d", x+i);
	for(int i=1; i<=m; i++) scanf("%d %d", p+i, c+i);

	int s=0, t=n+m+1; V=t;
	for(int i=1; i<=n; i++) {
		add(s, i, 1, 0);
		for(int j=1; j<=m; j++) {
			add(i, n+j, 1, abs(x[i]-p[j]));
		}
	}
	for(int i=1; i<=m; i++) add(n+i, t, c[i], 0);
	minCost(s, t, n);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=11111;
const int MM=22222222;
const int inf=0x3f3f3f3f;
const INT INF=0x3f3f3f3f3f3f3f3fll;

INT dis[NN],src,tar;
int E, head[NN], to[MM], nxt[MM], pre[NN];
int a[NN],b[NN],c[NN];
int flow[MM];
int cost[MM];

void add_edge(int u, int v, int f, int c){
	nxt[E]=head[u]; to[E]=v; flow[E]=f; cost[E]=c; head[u]=E++;
	nxt[E]=head[v]; to[E]=u; flow[E]=0; cost[E]=-c; head[v]=E++;
}

queue <int> q;

INT C;

INT maxflow(int s, int t){
	INT ans=0, u, v;
	int bot;
	while(1) {
		q=queue<int> ();
		q.push(s);
		memset(dis, inf, sizeof(dis));
		dis[s]=0;
		while(!q.empty()){
			u=q.front();
			q.pop();
			for(int e=head[u]; ~e; e=nxt[e]){
				v=to[e];
				if(flow[e]==0) continue;
				if(dis[v]>dis[u]+cost[e]) {
					dis[v]=dis[u]+cost[e];
					q.push(v);
					pre[v]=e;
				}
			}
		}

		if(dis[t]==INF) return ans;
		bot=inf;
		for(u=t; u!=s; u=to[pre[u]^1]) bot=min(bot, flow[pre[u]]);
		for(u=t; u!=s; u=to[pre[u]^1]) {
			flow[pre[u]]-=bot;
			flow[pre[u]^1]+=bot;
		}
		C+=dis[t];
		ans+=bot;
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int n,m; cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)scanf("%d%d",&b[i],&c[i]);

	memset(head,-1,sizeof head);
	src=0; tar=n+m+1;

	for(int i=1;i<=n;i++){
		add_edge(src,i,1,0);
		for(int j=1;j<=m;j++){
			add_edge(i,n+j,1,abs(a[i]-b[j]));
		}
	}
	for(int j=1;j<=m;j++){
		add_edge(n+j,tar,c[j],0);
	}

	if(maxflow(src,tar)<n){
		puts("-1");
		return 0;
	}
	cout<<C<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 10010
#define inf 2e9+1
using namespace std;

typedef long long INT;

int pos[NN], sum[NN];

map <int, int> mp;

int nn = 1;

void go_left(int u){
	for(int i = u - 1; i; i--){
		if(sum[i]) sum[i]--;
		if(mp[pos[i]] > 0) {mp[pos[i]]--; return ;}
	}
}

void go_right(int u){
	for(int i = u + 1; i <= nn; i++){
		if(mp[pos[i]] > 0) {mp[pos[i]]--; return ;}
		sum[i]++;
	}
}

int calc_left(int u){
	int ans = 0;
	for(int i = u - 1; i >= 0; i--){
		if(sum[i] == 0) ans += pos[i + 1] - pos[i];
		else ans -= pos[i + 1] - pos[i];
		if(mp[pos[i]] > 0) return ans;
	}
	return inf;
}

int calc_right(int u){
	int ans = 0;
	for(int i = u + 1; i <= nn; i++){
		ans += pos[i + 1] - pos[i];
		if(mp[pos[i]] > 0) return ans;
	}
	return inf;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	INT n, m;
	scanf("%I64d%I64d", &n, &m);
	for(int i = 1; i <= n; i++){
		int u;
		scanf("%d", &u);
		pos[nn++] = u;
		mp[u]--;
	}
	for(int i = 1; i <= m; i++){
		int p, val;
		scanf("%d%d", &p, &val);
		pos[nn++] = p, mp[p] += val;
	}
	sort(pos + 1, pos + nn);
	nn = unique(pos + 1, pos + nn) - pos;
//	cerr<<nn<<endl;
//	for(int i = 1; i <= nn; i++) cerr<<pos[i]<<" ";
	INT ans = 0;
	for(int i = 1; i <= nn; i++){
		while(mp[pos[i]] < 0){
			int dl = calc_left(i);
			int dr = calc_right(i);
			if(dl <= dr){
				ans += dl;
				go_left(i);
			} else{
				ans += dr;
				go_right(i);
			}
			mp[pos[i]]++;
		}
	}

	printf("%I64d\n", ans);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <climits>
#include <cassert>

using namespace std;

int N, M;
int X[5000];
pair<int, int> H[5000];
int P[5000], C[5000];
long long dp[2][5001];  // dp[i][j] = min cost using holes i..N and mouses j..M
int opt[5000][5000];

int main() {
  ios::sync_with_stdio(false); cin.tie(0);

  cin >> N >> M;
  for(int i = 0; i < N; i++) cin >> X[i];
  sort(X, X + N);

  for(int i = 0; i < M; i++) {
    cin >> H[i].first >> H[i].second;
  }
  sort(H, H + M);
  for(int i = 0; i < M; i++) {
    P[i] = H[i].first;
    C[i] = H[i].second;
  }

  int bit = 0;

  for(int j = 0; j <= N; j++) dp[bit][j] = LLONG_MAX/2;
  dp[bit][N] = 0;

  for(int i = M - 1; i >= 0; i--) {
    bit ^= 1;
    dp[bit][N] = 0;
    for(int j = N - 1; j >= 0; j--) {
      dp[bit][j] = LLONG_MAX/2;
      long long add = 0;
      for(int k = j - 1; k < min(j + C[i], N); k++) {
        if(add + dp[bit ^ 1][k + 1] < dp[bit][j]) {
          dp[bit][j] = add + dp[bit ^ 1][k + 1];
          opt[i][j] = k;
        }
        add += abs(X[k + 1] - P[i]);
      }
    }
  }

  long long ans = dp[bit][0];
  if(ans == LLONG_MAX/2) ans = -1;

  if(ans != -1) {
    for(int i = 0; i < M; i++) {
      for(int j = 0; j < N - 1; j++) {
        assert(opt[i][j] <= opt[i][j + 1]);
      }
    }
  }

  cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <climits>

using namespace std;

int N, M;
int X[5000];
pair<int, int> H[5000];
int P[5000], C[5000];
long long dp[5000][5000];

long long solve(int i, int j) {
  if(i == N) return 0;
  if(j == M) return LLONG_MAX/2;
  if(dp[i][j] != -1) return dp[i][j];

  long long ans = solve(i, j + 1);
  long long add = 0;
  for(int k = i; k < min(i + C[j], N); k++) {
    add += abs(X[k] - P[j]);
    ans = min(ans, add + solve(k + 1, j + 1));
  }

  return dp[i][j] = ans;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(0);

  cin >> N >> M;
  for(int i = 0; i < N; i++) cin >> X[i];
  sort(X, X + N);

  for(int i = 0; i < M; i++) {
    cin >> H[i].first >> H[i].second;
  }
  sort(H, H + M);
  for(int i = 0; i < M; i++) {
    P[i] = H[i].first;
    C[i] = H[i].second;
  }

  for(int i = 0; i < N; i++) {
    for(int j = 0; j < M; j++) {
      dp[i][j] = -1;
    }
  }

  cout << solve(0, 0) << endl;
}


#include <iostream>
#include <algorithm>
#include <climits>

using namespace std;

int N, M;
int X[5000];
pair<int, int> H[5000];
int P[5000], C[5000];
long long dp[5000][5000];

long long solve(int i, int j) {
  if(i == N) return 0;
  if(j == M) return LLONG_MAX/2;
  if(dp[i][j] != -1) return dp[i][j];

  long long ans = solve(i, j + 1);
  long long add = 0;
  for(int k = i; k < min(i + C[j], N); k++) {
    add += abs(X[k] - P[j]);
    ans = min(ans, add + solve(k + 1, j + 1));
  }

  return dp[i][j] = ans;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(0);

  cin >> N >> M;
  for(int i = 0; i < N; i++) cin >> X[i];
  sort(X, X + N);

  for(int i = 0; i < M; i++) {
    cin >> H[i].first >> H[i].second;
  }
  sort(H, H + M);
  for(int i = 0; i < M; i++) {
    P[i] = H[i].first;
    C[i] = H[i].second;
  }

  for(int i = 0; i < N; i++) {
    for(int j = 0; j < M; j++) {
      dp[i][j] = -1;
    }
  }

  long long ans = solve(0, 0);
  if(ans == LLONG_MAX/2) ans = -1;
  cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <climits>
#include <cassert>

using namespace std;

int N, M;
int X[5000];
pair<int, int> H[5000];
int P[5000], C[5000];
long long dp[5001][5001];  // dp[i][j] = min cost using holes i..N and mouses j..M
long long cost_pref[5000][5000]; // cost_pref[i][j] = cost for mouses 0..j to get to hole i
int opt[5000][5000];

inline long long cost(int i, int L, int R) {
  if(L == 0) return cost_pref[i][R];
  return cost_pref[i][R] - cost_pref[i][L - 1];
}

long long solve(int i, int j) {
  if(j == N) return 0;
  if(i == M) return LLONG_MAX/2;
  if(dp[i][j] != -1) return dp[i][j];

  long long ans = solve(i + 1, j);
  for(int k = j; k < min(j + C[i], N); k++) {
    ans = min(ans, cost(i, j, k) + solve(i + 1, k + 1));
  }

  return dp[i][j] = ans;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(0);

  cin >> N >> M;
  for(int i = 0; i < N; i++) cin >> X[i];
  sort(X, X + N);

  for(int i = 0; i < M; i++) {
    cin >> H[i].first >> H[i].second;
  }
  sort(H, H + M);
  for(int i = 0; i < M; i++) {
    P[i] = H[i].first;
    C[i] = H[i].second;
  }

  for(int i = 0; i < M; i++) {
    long long pref = 0;
    for(int j = 0; j < N; j++) {
      pref += abs(X[j] - P[i]);
      cost_pref[i][j] = pref;
    }
  }

  for(int j = 0; j <= N; j++) dp[M][j] = LLONG_MAX/2;
  for(int i = 0; i <= M; i++) dp[i][N] = 0;

  for(int i = M - 1; i >= 0; i--) {
    for(int j = N - 1; j >= 0; j--) {
      dp[i][j] = LLONG_MAX/2;
      for(int k = j - 1; k < min(j + C[i], N); k++) {
        if(cost(i, j, k) + dp[i + 1][k + 1] < dp[i][j]) {
          dp[i][j] = cost(i, j, k) + dp[i + 1][k + 1];
          opt[i][j] = k;
        }
      }
    }
  }

  long long ans = dp[0][0];
  if(ans == LLONG_MAX/2) ans = -1;

  if(ans != -1) {
    for(int i = 0; i < M; i++) {
      for(int j = 0; j < N - 1; j++) {
        assert(opt[i][j] <= opt[i][j + 1]);
      }
    }
  }

  cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <climits>
#include <cassert>

using namespace std;

int N, M;
int X[5000];
pair<int, int> H[5000];
int P[5000], C[5000];
long long dp[2][5001];  // dp[i][j] = min cost using holes i..N and mouses j..M
long long cost_pref[5000];

inline long long cost(int L, int R) {
  if(L == 0) return cost_pref[R];
  return cost_pref[R] - cost_pref[L - 1];
}

void solve(int bit, int i, int L, int R, int start, int end) {
  if(L > R) return;

  int j = (L + R)/2;
  int opt = -1;

  dp[bit][j] = LLONG_MAX/2;
  for(int k = start; k <= min(j + C[i] - 1, end); k++) {
    if(cost(j, k) + dp[bit ^ 1][k + 1] < dp[bit][j]) {
      dp[bit][j] = cost(j, k) + dp[bit ^ 1][k + 1];
      opt = k;
    }
  }

  solve(bit, i, L, j - 1, start, opt);
  solve(bit, i, j + 1, R, opt, end);
}

int main() {
  ios::sync_with_stdio(false); cin.tie(0);

  cin >> N >> M;
  for(int i = 0; i < N; i++) cin >> X[i];
  sort(X, X + N);

  for(int i = 0; i < M; i++) {
    cin >> H[i].first >> H[i].second;
  }
  sort(H, H + M);
  for(int i = 0; i < M; i++) {
    P[i] = H[i].first;
    C[i] = H[i].second;
  }

  int bit = 0;

  for(int j = 0; j <= N; j++) dp[bit][j] = LLONG_MAX/2;
  dp[bit][N] = 0;

  for(int i = M - 1; i >= 0; i--) {
    bit ^= 1;
    dp[bit][N] = 0;
    long long pref = 0;
    for(int j = 0; j < N; j++) {
      pref += abs(X[j] - P[i]);
      cost_pref[j] = pref;
    }
    solve(bit, i, 0, N - 1, -1, N - 1);
  }

  long long ans = dp[bit][0];
  if(ans == LLONG_MAX/2) ans = -1;
  cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <climits>
#include <cassert>

using namespace std;

int N, M;
int X[5000];
pair<int, int> H[5000];
int P[5000], C[5000];
long long dp[2][5001];  // dp[i][j] = min cost using holes i..N and mouses j..M
long long cost_pref[5000];

inline long long cost(int L, int R) {
  if(L == 0) return cost_pref[R];
  return cost_pref[R] - cost_pref[L - 1];
}

void solve(int bit, int i, int L, int R, int start, int end) {
  if(L > R) return;

  int j = (L + R)/2;
  int opt = -1;

  dp[bit][j] = LLONG_MAX/2;
  for(int k = max(start, j - 1); k <= min(j + C[i] - 1, end); k++) {
    if(cost(j, k) + dp[bit ^ 1][k + 1] < dp[bit][j]) {
      dp[bit][j] = cost(j, k) + dp[bit ^ 1][k + 1];
      opt = k;
    }
  }

  solve(bit, i, L, j - 1, start, opt);
  solve(bit, i, j + 1, R, opt, end);
}

int main() {
  ios::sync_with_stdio(false); cin.tie(0);

  cin >> N >> M;
  for(int i = 0; i < N; i++) cin >> X[i];
  sort(X, X + N);

  for(int i = 0; i < M; i++) {
    cin >> H[i].first >> H[i].second;
  }
  sort(H, H + M);
  for(int i = 0; i < M; i++) {
    P[i] = H[i].first;
    C[i] = H[i].second;
  }

  int bit = 0;

  for(int j = 0; j <= N; j++) dp[bit][j] = LLONG_MAX/2;
  dp[bit][N] = 0;

  for(int i = M - 1; i >= 0; i--) {
    bit ^= 1;
    dp[bit][N] = 0;
    long long pref = 0;
    for(int j = 0; j < N; j++) {
      pref += abs(X[j] - P[i]);
      cost_pref[j] = pref;
    }
    solve(bit, i, 0, N - 1, -1, N - 1);
  }

  long long ans = dp[bit][0];
  if(ans == LLONG_MAX/2) ans = -1;
  cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <climits>
#include <cassert>

using namespace std;

int N, M;
int X[5000];
pair<int, int> H[5000];
int P[5000], C[5000];
long long dp[5001][5001];  // dp[i][j] = min cost using holes i..N and mouses j..M
int opt[5000][5000];

int main() {
  ios::sync_with_stdio(false); cin.tie(0);

  cin >> N >> M;
  for(int i = 0; i < N; i++) cin >> X[i];
  sort(X, X + N);

  for(int i = 0; i < M; i++) {
    cin >> H[i].first >> H[i].second;
  }
  sort(H, H + M);
  for(int i = 0; i < M; i++) {
    P[i] = H[i].first;
    C[i] = H[i].second;
  }

  for(int j = 0; j <= N; j++) dp[M][j] = LLONG_MAX/2;
  for(int i = 0; i <= M; i++) dp[i][N] = 0;

  for(int i = M - 1; i >= 0; i--) {
    for(int j = N - 1; j >= 0; j--) {
      dp[i][j] = LLONG_MAX/2;
      long long add = 0;
      for(int k = j - 1; k < min(j + C[i], N); k++) {
        if(add + dp[i + 1][k + 1] < dp[i][j]) {
          dp[i][j] = add + dp[i + 1][k + 1];
          opt[i][j] = k;
        }
        add += abs(X[k + 1] - P[i]);
      }
    }
  }

  long long ans = dp[0][0];
  if(ans == LLONG_MAX/2) ans = -1;

  if(ans != -1) {
    for(int i = 0; i < M; i++) {
      for(int j = 0; j < N - 1; j++) {
        assert(opt[i][j] <= opt[i][j + 1]);
      }
    }
  }

  cout << ans << endl;
}


/********************************************
*                                           *
*    Solved By : Bir Bahadur Khatri(B'ru)   *
*      Be Positive,be Happy.                *
*                                           *
*********************************************/
#define ff first
#define D double
#define sz size()
#define ss second
#define PB push_back
#define MP make_pair
#define SQR(n) (n*n)
#define CHR getchar()
#define NL printf("\n")
#include<bits/stdc++.h>
#define ULL unsigned LL
#define PI 2.0*acos(0.0)
#define LL long long int
#define S1(a) a=in<int>()
#define SL1(a) a=in<LL>()
#define Max(a,b) ((a>b)?a:b)
#define Min(a,b) ((a<b)?a:b)
#define all(a) a.begin(),a.end()
#define _Max(a,b,c) Max(a,Max(b,c))
#define _Min(a,b,c) Min(a,Min(b,c))
#define SL2(a,b) a=in<LL>(),b=in<LL>()
#define F(i,a,b) for(int i=a;i<b; i++)
#define S2(a,b) a=in<int>(),b=in<int>()
#define R(i,a,b) for(int i=a-1;i>=b; i--)
#define BitCnt(a) __builtin_popcountll(a)
#define MEM(a,val) memset(a,val,sizeof(a))
#define SL3(a,b,c) a=in<LL>(),b=in<LL>(),c=in<LL>()
#define S3(a,b,c) a=in<int>(),b=in<int>(),c=in<int>()
#define _cin ios_base::sync_with_stdio(0); cin.tie(0)
#define cp printf("***** here here here here *****\n");
#define trace1(x)                cerr << #x << ": " << x << endl;
#define InpOut freopen("A.in","r",stdin),freopen("A1.out","w",stdout)
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

using namespace std;
template <typename T> T in(){char ch;T n = 0;bool ng = false;while (1){ch = getchar();if (ch == '-'){ng = true;ch = getchar();break;}if (ch>='0' && ch<='9')     break;}while (1){n = n*10 + (ch - '0');ch = getchar();if (ch<'0' || ch>'9')   break;}return (ng?-n:n);}
template<typename T>inline T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
template<typename T>inline T Gcd(T a,T b){if(a<0)return Gcd(-a,b);if(b<0)return Gcd(a,-b);return (b==0)?a:Gcd(b,a%b);}
template<typename T>inline T Lcm(T a,T b) {if(a<0)return Lcm(-a,b);if(b<0)return Lcm(a,-b);return a*(b/Gcd(a,b));}
long long Bigmod(long long base, long long power, long long MOD){long long ret=1;while(power){if(power & 1)ret=(ret*base)%MOD;base=(base*base)%MOD;power>>=1;}return ret;}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
template <typename T> LL isLeft(T a,T b,T c) {return(a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y);}
long long ModInverse(long long number, long long MOD){return Bigmod(number, MOD-2, MOD);}
bool isConst(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}
int toInt(string s)  { int sm; stringstream ss(s); ss>>sm; return sm; }
///**********************************************************//

#define MD1 1000000007ULL
#define MD2 1000000009ULL
#define MD3 1000000021ULL
#define BS1 10000019ULL
#define BS2 10000079ULL
#define BS3 10000103ULL
#define PUL pair<ULL,ULL>
#define PLL pair<LL,LL>
#define PII pair<int,int>

#define lchild lft,l,mid
#define rchild rgt,mid+1,r
#define ngood  r<st||l>ed
#define good   st<=l&&ed>=r
#define seg    int mid=(l+r)/2,lft=2*id,rgt=lft+1

///         0123456789
#define MX  5005
#define MOD 1000000007
#define INF 100000000000000LL
#define EPS 1e-9
/// ==========================================////

int a[MX];
PII b[MX];
LL dp[MX][2];
int k;
int flg;
LL sm[MX];
void lol(int n) {
    F(i,1,n+1) {
        sm[i]=sm[i-1]+abs( a[i]-b[k].ff );
    }
}

LL Cost(int l,int r) {
    return sm[r]-sm[l-1];
}

void Func(int l,int r,int x,int y) {
    if(l>r) return;
    int mid=(l+r)/2;

    LL &res=dp[mid][flg];
    res=dp[mid][flg^1];
    int Id=mid;
    int rg=mid-b[k].ss+1;
    rg=max(rg,1);
    for(int i=max(rg,x);i<=min(y,mid);i++) {
        LL tp=dp[i-1][flg^1]+Cost(i,mid);
        if(res>tp) {
            res=tp;
            Id=i;
        }
    }
    Func(l,mid-1,x,Id);
    Func(mid+1,r,Id,y);
}

LL Solve(int K,int n) {
    F(i,1,n+1) dp[i][0]=1e16;
    flg=0;
    for(int i=1;i<=K;i++) {
        k=i;
        lol(n);
        flg^=1;
        Func(1,n,1,n);
    }
    if(dp[n][flg]>=1e16) dp[n][flg]=-1;
    return dp[n][flg];
}

int main()
{

    int n,m;
    S2(n,m);
    F(i,1,n+1) {
        S1(a[i]);
    }

    F(i,1,m+1) {
        S2(b[i].ff,b[i].ss);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    cout<<Solve(m,n)<<endl;

    return 0;
}


/********************************************
*                                           *
*    Solved By : Bir Bahadur Khatri(B'ru)   *
*      Be Positive,be Happy.                *
*                                           *
*********************************************/
#define ff first
#define D double
#define sz size()
#define ss second
#define PB push_back
#define MP make_pair
#define SQR(n) (n*n)
#define CHR getchar()
#define NL printf("\n")
#include<bits/stdc++.h>
#define ULL unsigned LL
#define PI 2.0*acos(0.0)
#define LL long long int
#define S1(a) a=in<int>()
#define SL1(a) a=in<LL>()
#define Max(a,b) ((a>b)?a:b)
#define Min(a,b) ((a<b)?a:b)
#define all(a) a.begin(),a.end()
#define _Max(a,b,c) Max(a,Max(b,c))
#define _Min(a,b,c) Min(a,Min(b,c))
#define SL2(a,b) a=in<LL>(),b=in<LL>()
#define F(i,a,b) for(int i=a;i<b; i++)
#define S2(a,b) a=in<int>(),b=in<int>()
#define R(i,a,b) for(int i=a-1;i>=b; i--)
#define BitCnt(a) __builtin_popcountll(a)
#define MEM(a,val) memset(a,val,sizeof(a))
#define SL3(a,b,c) a=in<LL>(),b=in<LL>(),c=in<LL>()
#define S3(a,b,c) a=in<int>(),b=in<int>(),c=in<int>()
#define _cin ios_base::sync_with_stdio(0); cin.tie(0)
#define cp printf("***** here here here here *****\n");
#define trace1(x)                cerr << #x << ": " << x << endl;
#define InpOut freopen("A.in","r",stdin),freopen("A1.out","w",stdout)
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

using namespace std;
template <typename T> T in(){char ch;T n = 0;bool ng = false;while (1){ch = getchar();if (ch == '-'){ng = true;ch = getchar();break;}if (ch>='0' && ch<='9')     break;}while (1){n = n*10 + (ch - '0');ch = getchar();if (ch<'0' || ch>'9')   break;}return (ng?-n:n);}
template<typename T>inline T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
template<typename T>inline T Gcd(T a,T b){if(a<0)return Gcd(-a,b);if(b<0)return Gcd(a,-b);return (b==0)?a:Gcd(b,a%b);}
template<typename T>inline T Lcm(T a,T b) {if(a<0)return Lcm(-a,b);if(b<0)return Lcm(a,-b);return a*(b/Gcd(a,b));}
long long Bigmod(long long base, long long power, long long MOD){long long ret=1;while(power){if(power & 1)ret=(ret*base)%MOD;base=(base*base)%MOD;power>>=1;}return ret;}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
template <typename T> LL isLeft(T a,T b,T c) {return(a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y);}
long long ModInverse(long long number, long long MOD){return Bigmod(number, MOD-2, MOD);}
bool isConst(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}
int toInt(string s)  { int sm; stringstream ss(s); ss>>sm; return sm; }
///**********************************************************//

#define MD1 1000000007ULL
#define MD2 1000000009ULL
#define MD3 1000000021ULL
#define BS1 10000019ULL
#define BS2 10000079ULL
#define BS3 10000103ULL
#define PUL pair<ULL,ULL>
#define PLL pair<LL,LL>
#define PII pair<int,int>

#define lchild lft,l,mid
#define rchild rgt,mid+1,r
#define ngood  r<st||l>ed
#define good   st<=l&&ed>=r
#define seg    int mid=(l+r)/2,lft=2*id,rgt=lft+1

///         0123456789
#define MX  505
#define MOD 1000000007
#define INF 100000000000000LL
#define EPS 1e-9
/// ==========================================////

int a[MX];
PII b[MX];
LL dp[MX][2];
int k;
int flg;
LL sm[MX];
void lol(int n) {
    F(i,1,n+1) {
        sm[i]=sm[i-1]+abs( a[i]-b[k].ff );
    }
}

LL Cost(int l,int r) {
    return sm[r]-sm[l-1];
}

void Func(int l,int r,int x,int y) {
    if(l>r) return;
    int mid=(l+r)/2;

    LL &res=dp[mid][flg];
    res=dp[mid][flg^1];
    int Id=mid;
    int rg=mid-b[k].ss+1;
    rg=max(rg,1);
    for(int i=max(rg,x);i<=min(y,mid);i++) {
        LL tp=dp[i-1][flg^1]+Cost(i,mid);
        if(res>tp) {
            res=tp;
            Id=i;
        }
    }
    Func(l,mid-1,x,Id);
    Func(mid+1,r,Id,y);
}

LL Solve(int K,int n) {
    F(i,1,n+1) dp[i][0]=1e16;
    flg=0;
    for(int i=1;i<=K;i++) {
        k=i;
        lol(n);
        flg^=1;
        Func(1,n,1,n);
    }
    if(dp[n][flg]>=1e16) dp[n][flg]=-1;
    return dp[n][flg];
}

int main()
{

    int n,m;
    S2(n,m);
    F(i,1,n+1) {
        S1(a[i]);
    }

    F(i,1,m+1) {
        S2(b[i].ff,b[i].ss);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);

    cout<<Solve(m,n)<<endl;

    return 0;
}


/********************************************
*                                           *
*    Solved By : Bir Bahadur Khatri(B'ru)   *
*      Be Positive,be Happy.                *
*                                           *
*********************************************/
#define ff first
#define D double
#define sz size()
#define ss second
#define PB push_back
#define MP make_pair
#define SQR(n) (n*n)
#define CHR getchar()
#define NL printf("\n")
#include<bits/stdc++.h>
#define ULL unsigned LL
#define PI 2.0*acos(0.0)
#define LL long long int
#define S1(a) a=in<int>()
#define SL1(a) a=in<LL>()
#define Max(a,b) ((a>b)?a:b)
#define Min(a,b) ((a<b)?a:b)
#define all(a) a.begin(),a.end()
#define _Max(a,b,c) Max(a,Max(b,c))
#define _Min(a,b,c) Min(a,Min(b,c))
#define SL2(a,b) a=in<LL>(),b=in<LL>()
#define F(i,a,b) for(int i=a;i<b; i++)
#define S2(a,b) a=in<int>(),b=in<int>()
#define R(i,a,b) for(int i=a-1;i>=b; i--)
#define BitCnt(a) __builtin_popcountll(a)
#define MEM(a,val) memset(a,val,sizeof(a))
#define SL3(a,b,c) a=in<LL>(),b=in<LL>(),c=in<LL>()
#define S3(a,b,c) a=in<int>(),b=in<int>(),c=in<int>()
#define _cin ios_base::sync_with_stdio(0); cin.tie(0)
#define cp printf("***** here here here here *****\n");
#define trace1(x)                cerr << #x << ": " << x << endl;
#define InpOut freopen("A.in","r",stdin),freopen("A1.out","w",stdout)
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

using namespace std;
template <typename T> T in(){char ch;T n = 0;bool ng = false;while (1){ch = getchar();if (ch == '-'){ng = true;ch = getchar();break;}if (ch>='0' && ch<='9')     break;}while (1){n = n*10 + (ch - '0');ch = getchar();if (ch<'0' || ch>'9')   break;}return (ng?-n:n);}
template<typename T>inline T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
template<typename T>inline T Gcd(T a,T b){if(a<0)return Gcd(-a,b);if(b<0)return Gcd(a,-b);return (b==0)?a:Gcd(b,a%b);}
template<typename T>inline T Lcm(T a,T b) {if(a<0)return Lcm(-a,b);if(b<0)return Lcm(a,-b);return a*(b/Gcd(a,b));}
long long Bigmod(long long base, long long power, long long MOD){long long ret=1;while(power){if(power & 1)ret=(ret*base)%MOD;base=(base*base)%MOD;power>>=1;}return ret;}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
template <typename T> LL isLeft(T a,T b,T c) {return(a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y);}
long long ModInverse(long long number, long long MOD){return Bigmod(number, MOD-2, MOD);}
bool isConst(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}
int toInt(string s)  { int sm; stringstream ss(s); ss>>sm; return sm; }
///**********************************************************//

#define MD1 1000000007ULL
#define MD2 1000000009ULL
#define MD3 1000000021ULL
#define BS1 10000019ULL
#define BS2 10000079ULL
#define BS3 10000103ULL
#define PUL pair<ULL,ULL>
#define PLL pair<LL,LL>
#define PII pair<int,int>

#define lchild lft,l,mid
#define rchild rgt,mid+1,r
#define ngood  r<st||l>ed
#define good   st<=l&&ed>=r
#define seg    int mid=(l+r)/2,lft=2*id,rgt=lft+1

///         0123456789
#define MX  505
#define MOD 1000000007
#define INF 100000000000000LL
#define EPS 1e-9
/// ==========================================////

int a[MX];
PII b[MX];
LL dp[MX][MX];
int k;

LL Cost(int l,int r) {
    LL an=0;
    F(i,l,r+1) {
        an+=abs( a[i]-b[k].ff );
    }
    return an;
}

void Func(int l,int r,int x,int y) {
    if(l>r) return;
    int mid=(l+r)/2;

    LL &res=dp[mid][k];
    res=dp[mid][k-1];
    int Id=mid;
    int rg=mid-b[k].ss+1;
    rg=max(rg,1);
    for(int i=max(rg,x);i<=min(y,mid);i++) {
        LL tp=dp[i-1][k-1]+Cost(i,mid);
        if(res>tp) {
            res=tp;
            Id=i;
        }
    }
    Func(l,mid-1,x,Id);
    Func(mid+1,r,Id,y);
}

LL Solve(int K,int n) {
    F(i,1,n+1) dp[i][0]=1e16;
    F(i,1,k+1) dp[0][i]=1e16;
    for(int i=1;i<=K;i++) {
        k=i;
        Func(1,n,1,n);
    }
    if(dp[n][K]>=1e16) dp[n][K]=-1;
    return dp[n][K];
}

int main()
{

    int n,m;
    S2(n,m);
    F(i,1,n+1) {
        S1(a[i]);
    }

    F(i,1,m+1) {
        S2(b[i].ff,b[i].ss);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+m+1);

    cout<<Solve(m,n)<<endl;

    return 0;
}