ints = list(map(int, input().split()))
n, m = ints[0], ints[1]
trees = list(map(int, input().split()))
trees.sort()

occupied = {}
for i in range(len(trees)):
    occupied[trees[i]] = 1

trees = set(trees)

overall = 0
distance = 1
guys = []
while m > 0:
    deletable = set()
    for tree in trees:
        if m == 0:
            break

        left = tree - distance
        must_delete = 0
        if left not in occupied:
            occupied[left] = 1
            guys.append(left)
            overall += distance
            m -= 1
        else:
            must_delete += 1

        if m == 0:
            break

        right = tree + distance
        if right not in occupied:
            occupied[right] = 1
            guys.append(right)
            overall += distance
            m -= 1
        else:
            must_delete += 1

        if must_delete == 2:
            deletable.add(tree)

    trees = trees - deletable

    distance += 1

print(overall)
print(*guys)


ints = list(map(int, input().split()))
n, m = ints[0], ints[1]
trees = list(map(int, input().split()))

occupied = {}
for i in range(len(trees)):
    occupied[trees[i]] = 1

overall = 0
distance = 1
guys = []
while m > 0:
    for tree in trees:
        left = tree - distance
        if left not in occupied:
            occupied[left] = 1
            guys.append(left)
            overall += distance
            m -= 1

        if m == 0:
            break

        right = tree + distance
        if right not in occupied:
            occupied[right] = 1
            guys.append(right)
            overall += distance
            m -= 1
    distance += 1

print(overall)
print(*guys)


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

# M = mod = 998244353
def factors(n):return sorted(list(set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))))
def inv_mod(n):return pow(n, mod - 2, mod)

def li():return [int(i) for i in input().rstrip('\n').split(' ')]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]

n, m = li()
l = sorted(li())
cnt = Counter(l)
temp = set(l)
print(l)
ans = 0
for i in range(n):
    if cnt[l[i] - 1] == 0:
        cnt[l[i] - 1] = 1
        ans += 1
        m -= 1
        if not m:break
    if cnt[l[i] + 1] == 0:
        cnt[l[i] + 1] = 1
        ans += 1
        m -= 1
        if not m:break
print(cnt)

while m:
    curr = 2
    for i in range(n):
        if cnt[l[i] + curr] == 0:
            cnt[l[i] + curr] = 1
            m -= 1
            ans += curr
        if not m:break

    if m:
        for i in range(n-1,-1,-1):
            if cnt[l[i] - curr] == 0:
                cnt[l[i] - curr] = 1
                m -= 1
                ans += curr
            if not m:break
    curr += 1

print('what')
print(ans)
for i in cnt:
    if i not in temp:
        print(i,end = " ")
print()

# n = val()
# l = li()
# l1 = l[:]
# l2 = l[:]

# i = 0
# l1 = sorted(list(set(l1)))
# doit = tot1 = 0
# cn = Counter(l1)
# l = l1[:]
# while i < len(l1):
#     if cn[l[i] - 1] > 0:
#         cn[l[i]] -= 1
#         cn[l[i] - 1] += 1
#         l[i] -= 1
#         i += 1
#     else:
#         cn[l[i]] -= 1
#         cn[l[i] + 1] += 1
#         l[i] += 1
#         if i<len(l1) - 1 and l[i + 1] == l[i]:i += 2
#         else:i += 1
# # print(cn)
# tot1 = sum(1 for i in cn if cn[i])

# # print(l1)

# i = 0

# cnt = Counter(l2)
# l2 = sorted(l2)

# l = l2[:]

# for i in range(n):
#     if cnt[l[i] - 1] == 0:
#         cnt[l[i] - 1] += 1
#         cnt[l[i]] -= 1
#         l[i] -= 1
#     elif cnt[l[i]] > 1:
#         cnt[l[i] + 1] += 1
#         cnt[l[i]] -= 1
#         l[i] += 1

# tot2 = sum(1 for i in cnt if cnt[i])

# print(tot1,tot2)


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

# M = mod = 998244353
def factors(n):return sorted(list(set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))))
def inv_mod(n):return pow(n, mod - 2, mod)

def li():return [int(i) for i in input().rstrip('\n').split(' ')]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]

n, m = li()
l = sorted(li())
cnt = Counter(l)
temp = set(l)
# print(l)
ans = 0
leftcounter = deque()
for i in range(n):
    if cnt[l[i] - 1] == 0:
        cnt[l[i] - 1] = 1
        ans += 1
        m -= 1
        leftcounter.append([l[i] - 2,2,-1])
        if not m:break
    if cnt[l[i] + 1] == 0:
        cnt[l[i] + 1] = 1
        ans += 1
        m -= 1
        leftcounter.append([l[i] + 2,2,1])
        if not m:break
# print(cnt)

while m:
    curr,steps,change = leftcounter.popleft()
    if curr not in cnt:
        cnt[curr] = 1
        ans += steps
        m -= 1
    if curr + change not in cnt:
        leftcounter.append([curr + change,steps + 1,change])

# print('what')
print(ans)
for i in cnt:
    if i not in temp:
        print(i,end = " ")
print()

# n = val()
# l = li()
# l1 = l[:]
# l2 = l[:]

# i = 0
# l1 = sorted(list(set(l1)))
# doit = tot1 = 0
# cn = Counter(l1)
# l = l1[:]
# while i < len(l1):
#     if cn[l[i] - 1] > 0:
#         cn[l[i]] -= 1
#         cn[l[i] - 1] += 1
#         l[i] -= 1
#         i += 1
#     else:
#         cn[l[i]] -= 1
#         cn[l[i] + 1] += 1
#         l[i] += 1
#         if i<len(l1) - 1 and l[i + 1] == l[i]:i += 2
#         else:i += 1
# # print(cn)
# tot1 = sum(1 for i in cn if cn[i])

# # print(l1)

# i = 0

# cnt = Counter(l2)
# l2 = sorted(l2)

# l = l2[:]

# for i in range(n):
#     if cnt[l[i] - 1] == 0:
#         cnt[l[i] - 1] += 1
#         cnt[l[i]] -= 1
#         l[i] -= 1
#     elif cnt[l[i]] > 1:
#         cnt[l[i] + 1] += 1
#         cnt[l[i]] -= 1
#         l[i] += 1

# tot2 = sum(1 for i in cnt if cnt[i])

# print(tot1,tot2)


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

# M = mod = 998244353
def factors(n):return sorted(list(set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))))
def inv_mod(n):return pow(n, mod - 2, mod)

def li():return [int(i) for i in input().rstrip('\n').split(' ')]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]

n, m = li()
l = sorted(li())
cnt = Counter(l)
temp = set(l)
# print(l)
ans = 0
for i in range(n):
    if cnt[l[i] - 1] == 0:
        cnt[l[i] - 1] = 1
        ans += 1
        m -= 1
        if not m:break
    if cnt[l[i] + 1] == 0:
        cnt[l[i] + 1] = 1
        ans += 1
        m -= 1
        if not m:break
# print(cnt)

while m:
    curr = 2
    for i in range(n):
        if cnt[l[i] + curr] == 0:
            cnt[l[i] + curr] = 1
            m -= 1
            ans += curr
        if not m:break

    if m:
        for i in range(n-1,-1,-1):
            if cnt[l[i] - curr] == 0:
                cnt[l[i] - curr] = 1
                m -= 1
                ans += curr
            if not m:break
    curr += 1

# print('what')
print(ans)
for i in cnt:
    if i not in temp:
        print(i,end = " ")
print()

# n = val()
# l = li()
# l1 = l[:]
# l2 = l[:]

# i = 0
# l1 = sorted(list(set(l1)))
# doit = tot1 = 0
# cn = Counter(l1)
# l = l1[:]
# while i < len(l1):
#     if cn[l[i] - 1] > 0:
#         cn[l[i]] -= 1
#         cn[l[i] - 1] += 1
#         l[i] -= 1
#         i += 1
#     else:
#         cn[l[i]] -= 1
#         cn[l[i] + 1] += 1
#         l[i] += 1
#         if i<len(l1) - 1 and l[i + 1] == l[i]:i += 2
#         else:i += 1
# # print(cn)
# tot1 = sum(1 for i in cn if cn[i])

# # print(l1)

# i = 0

# cnt = Counter(l2)
# l2 = sorted(l2)

# l = l2[:]

# for i in range(n):
#     if cnt[l[i] - 1] == 0:
#         cnt[l[i] - 1] += 1
#         cnt[l[i]] -= 1
#         l[i] -= 1
#     elif cnt[l[i]] > 1:
#         cnt[l[i] + 1] += 1
#         cnt[l[i]] -= 1
#         l[i] += 1

# tot2 = sum(1 for i in cnt if cnt[i])

# print(tot1,tot2)


p, m = map(int, input().split())
a = {*map(int, input().split())}
r = {*a}
z = {*a}
m += len(r)
k = 1
s = 0
while len(r) < m:
    b = set()
    for i in a:
        for p in -1, 1:
            if i + p*k not in r:
                if len(r) < m:
                    s += abs(p*k)
                    r.add(i + p*k)
                    b.add(i)
                    if len(r) == m:
                        print(s)
                        print(*(r-z))
                        exit()
    k += 1
    a = b


# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def bfs(src):
    from collections import deque

    que = deque()
    for x in src:
        que.append((x, 0))
    dist = {}
    while len(dist) < N+M:
        u, c = que.popleft()
        if u in dist:
            continue
        dist[u] = c
        que.append((u-1, c+1))
        que.append((u+1, c+1))
    return dist

N, M = MAP()
A = LIST()

res = bfs(A)
ans = []
sm = 0
for k, v in res.items():
    if v != 0:
        sm += v
        ans.append(k)
print(sm)
print(*ans)


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 5e3 + 5;

int n, m, mouse[N], dist[N], dp[N], dq[N], f[N];
pair <int, int> hole[N];

void input(){
    scanf("%lld %lld", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%lld", &mouse[i]);
    for (int i = 1; i <= m; i++) scanf("%lld %lld", &hole[i].first, &hole[i].second);
    sort(mouse + 1, mouse + n + 1);
    sort(hole + 1, hole + m + 1);
}

void solve(){
    int cap = 0;
    for (int i = 1; i <= m; i++) cap += hole[i].second;

    if (n > cap) {
        printf("-1");
        return;
    }

    memset(dp, 0x3f, sizeof(dp));
    dp[0] = 0;
    memset(dq, 0, sizeof(dq));
    for (int i = 1; i <= m; i++) { //calculate over layer
        dist[0] = 0;

        //dist: prefix sum of distance from mouse to current hole
        for (int j = 1; j <= n; j++) {
            dist[j] = abs(mouse[j] - hole[i].first);
            dist[j] += dist[j - 1];
        }

        //Save head position and tail position in deque dq
        int head = 1;
        int tail = 1;
        dq[tail] = 0;
        for (int j = 1; j <= n; j++) {
            while (head <= tail && dq[head] < j - hole[i].second) head++;
            f[j] = dp[dq[head]] + dist[j] - dist[dq[head]];
            while (head <= tail && dp[j] - dist[j] <= dp[dq[tail]] - dist[dq[tail]]) tail--;
            dq[++tail] = j;
        }

        for (int j = 1; j <= n; j++) dp[j] = min(dp[j], f[j]);
    }

    printf("%lld", dp[n]);
}

signed main(){
    input();
    solve();
    return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 2e3 + 5;

int n, m, mouse[N], dist[N][N], dp[N][N];
pair <int, int> hole[N];

void input(){
    scanf("%lld %lld", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%lld", &mouse[i]);
    for (int i = 1; i <= m; i++) scanf("%lld %lld", &hole[i].first, &hole[i].second);
    sort(mouse + 1, mouse + n + 1);
    sort(hole + 1, hole + m + 1);
}

int cost (int layer, int l, int r) {
    return dist[layer][r] - dist[layer][l - 1];
}

void constructLayer (int layer, int l, int r, int optL, int optR) {
    if (l > r) return;

    int optPos = -1;
    int mid = (l + r) / 2;

    for (int i = optL; i <= min(mid, optR); i++) {
        if (dp[mid][layer] > dp[i][layer - 1] + cost(layer, i + 1, mid) && mid - i <= hole[layer].second) {
            dp[mid][layer] = dp[i][layer - 1] + cost(layer, i + 1, mid);
            optPos = i;
        }
    }

    constructLayer(layer, l, mid - 1, optL, optPos);
    constructLayer(layer, mid + 1, r, optPos, optR);
}

void solve(){
    for (int i = 1; i <= m; i++) {
        dist[i][0] = 0;
        for (int j = 1; j <= n; j++) {
            dist[i][j] = abs(hole[i].first - mouse[j]);
            if (j - 1) dist[i][j] += dist[i][j - 1];
        }
    }

    memset(dp, 0x3f, sizeof(dp));
    for (int i = 1; i <= n; i++) dp[i][1] = dist[1][i];
    for (int i = 2; i <= m; i++) constructLayer(i, 1, n, 1, n);

    if (dp[n][m] > 1e18) printf("-1");
    else printf("%lld", dp[n][m]);
}

signed main(){
    input();
    solve();
    return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 2e3 + 5;

int n, m, a[N], dist[N][N], dp[N][N];
pair <int, int> hole[N];

void input(){
    scanf("%lld %lld", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for (int i = 1; i <= m; i++) scanf("%lld %lld", &hole[i].first, &hole[i].second);
    sort(a + 1, a + n + 1);
    sort(hole + 1, hole + m + 1);
}

void solve(){
    for (int i = 1; i <= m; i++) {
        dist[i][0] = 0;
        for (int j = 1; j <= n; j++) {
            dist[i][j] = abs(hole[i].first - a[j]);
            if (j - 1) dist[i][j] += dist[i][j - 1];
        }
    }

    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            for (int k = 0; k <= i; k++) {
                if (dp[k][j - 1] < 1e18 && i - k <= hole[j].second) {
                    dp[i][j] = min(dp[i][j], dp[k][j - 1] + dist[j][i] - dist[j][k]);
                }
            }
        }
    }

    printf("%lld", dp[n][m]);
}

signed main(){
    input();
    solve();
    return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 2e3 + 5;

int n, m, mouse[N], dist[N][N], dp[N][N];
pair <int, int> hole[N];

void input(){
    scanf("%lld %lld", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%lld", &mouse[i]);
    for (int i = 1; i <= m; i++) scanf("%lld %lld", &hole[i].first, &hole[i].second);
    sort(mouse + 1, mouse + n + 1);
    sort(hole + 1, hole + m + 1);
}

int cost (int layer, int l, int r) {
    return dist[layer][r] - dist[layer][l - 1];
}

void constructLayer (int layer, int l, int r, int optL, int optR) {
    if (l > r) return;

    int optPos = -1;
    int mid = (l + r) / 2;

    for (int i = optL; i <= min(mid, optR); i++) {
        if (dp[mid][layer] > dp[i][layer - 1] + cost(layer, i + 1, mid) && mid - i <= hole[layer].second) {
            dp[mid][layer] = dp[i][layer - 1] + cost(layer, i + 1, mid);
            optPos = i;
        }
    }

    constructLayer(layer, l, mid - 1, optL, optPos);
    constructLayer(layer, mid + 1, r, optPos, optR);
}

void solve(){
    int cap = 0;
    for (int i = 1; i <= m; i++) cap += hole[i].second;

    if (n > cap) {
        printf("-1");
        return;
    }

    for (int i = 1; i <= m; i++) {
        dist[i][0] = 0;
        for (int j = 1; j <= n; j++) {
            dist[i][j] = abs(hole[i].first - mouse[j]);
            if (j - 1) dist[i][j] += dist[i][j - 1];
        }
    }

    memset(dp, 0x3f, sizeof(dp));
    for (int i = 1; i <= n; i++) dp[i][1] = dist[1][i];
    for (int i = 2; i <= m; i++) constructLayer(i, 1, n, 1, n);

    printf("%lld", dp[n][m]);
}

signed main(){
    input();
    solve();
    return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 2e3 + 5;

int n, m, a[N], dist[N][N], dp[N][N];
pair <int, int> hole[N];

void input(){
    scanf("%lld %lld", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for (int i = 1; i <= m; i++) scanf("%lld %lld", &hole[i].first, &hole[i].second);
    sort(a + 1, a + n + 1);
    sort(hole + 1, hole + m + 1);
}

void solve(){
    for (int i = 1; i <= m; i++) {
        dist[i][0] = 0;
        for (int j = 1; j <= n; j++) {
            dist[i][j] = abs(hole[i].first - a[j]);
            if (j - 1) dist[i][j] += dist[i][j - 1];
        }
    }

    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            for (int k = 0; k <= i; k++) {
                if (dp[k][j - 1] < 1e18 && i - k <= hole[j].second) {
                    dp[i][j] = min(dp[i][j], dp[k][j - 1] + dist[j][i] - dist[j][k]);
                }
            }
        }
    }

    if (dp[n][m] > 1e18) printf("-1");
    else printf("%lld", dp[n][m]);
}

signed main(){
    input();
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 5000 + 5;

int n, m;
int a[N], nxt[N];
ii b[N];
ll sum[N], dp[N], dp2[N];

ll cost(int i, int l, int r) {
	ll res = 0;
	if(nxt[i] > l) {
		int x = min(r, nxt[i] - 1);
		res += (ll) b[i].first * (x - l + 1) - (sum[x] - sum[l - 1]);
		// printf("fp = %lld %lld\n", (ll) b[i].first * (x - l + 1), (sum[x] - sum[l - 1]));
	}
	if(nxt[i] <= r) {
		int x = max(l, nxt[i]);
		res += sum[r] - sum[x - 1] - (ll) b[i].first * (r - x + 1);
		// printf("tp = %lld %lld\n", sum[r] - sum[x - 1], (ll) b[i].first * (r - x + 1));
	}
	return res;
}

void solve(int i, int l, int r, int optL, int optR) {
	if(l > r)
		return;
	int m = (l + r) >> 1;
	int mn = m - 1;
	dp2[m] = dp[m];
	for(int k = max(m, optL); k <= min(n, optR); k++) {
		if(k - m + 1 > b[i].second)
			break;
		if(dp[k + 1] + cost(i, m, k) < dp2[m]) {
			dp2[m] = dp[k + 1] + cost(i, m, k);
			mn = k;
		}
	}
	solve(i, l, m - 1, optL, mn);
	solve(i, m + 1, r, mn, optR);
}

int main() {

	scanf("%d %d", &n, &m);

	for(int i = 1; i <= n; i++)
		scanf("%d", a + i);

	for(int i = 1; i <= m; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		b[i] = {x, y};
	}

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

	for(int i = 1; i <= n; i++)
		sum[i] = sum[i - 1] + a[i];

	int it = 1;

	for(int i = 1; i <= m; i++) {
		while(it <= n and a[it] < b[i].first)
			it++;
		nxt[i] = it;
		// printf("nxt[%d] = %d\n", i, it);
	}

	memset(dp, 63, sizeof(dp));

	dp[n + 1] = 0;

	for(int i = m; i >= 1; i--) {
		solve(i, 1, n, 1, n);
		for(int i = 1; i <= n; i++)
			dp[i] = dp2[i];
	}

	if(dp[1] > 3e18)
		puts("-1");
	else
		printf("%lld\n", dp[1]);

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 5000 + 5;

int n, m;
int a[N], nxt[N];
ii b[N];
ll sum[N], dp[N];

ll cost(int i, int l, int r) {
	ll res = 0;
	if(nxt[i] > l) {
		int x = min(r, nxt[i] - 1);
		res += (ll) b[i].first * (x - l + 1) - (sum[x] - sum[l - 1]);
	}
	if(nxt[i] <= r) {
		int x = max(l, nxt[i]);
		res += sum[r] - sum[x - 1] - (ll) b[i].first * (r - x + 1);
	}
	return res;
}

int main() {

	scanf("%d %d", &n, &m);

	for(int i = 1; i <= n; i++)
		scanf("%d", a + i);

	for(int i = 1; i <= m; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		b[i] = {x, y};
	}

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

	for(int i = 1; i <= n; i++)
		sum[i] = sum[i - 1] + a[i];

	int it = 1;

	for(int i = 1; i <= m; i++) {
		while(it <= n and a[it] < b[i].first)
			it++;
		nxt[i] = it;
	}

	memset(dp, 63, sizeof(dp));

	dp[n + 1] = 0;

	for(int i = m; i >= 1; i--) {
		for(int j = 1; j <= n; j++) {
			for(int k = j; k <= n; k++) {
				if(k - j + 1 > b[i].second)
					break;
				dp[j] = min(dp[j], dp[k + 1] + cost(i, j, k));
			}
		}
	}

	if(dp[1] > 3e18)
		puts("-1");
	else
		printf("%lld\n", dp[1]);

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 5000 + 5;

int n, m;
int a[N], nxt[N];
ii b[N];
ll sum[N], dp[N];

ll cost(int i, int l, int r) {
	ll res = 0;
	if(nxt[i] > l) {
		int x = min(r, nxt[i] - 1);
		res += (ll) b[i].first * (x - l + 1) - (sum[x] - sum[l - 1]);
	}
	if(nxt[i] <= r) {
		int x = max(l, nxt[i]);
		res += sum[r] - sum[x - 1] - (ll) b[i].first * (r - x + 1);
	}
	return res;
}

int l, r;
pair < int, ll > q[N];

void clear() {
	l = 1;
	r = 0;
}

void add(int i, ll x) {
	while(l <= r and q[r].second >= x)
		r--;
	q[++r] = {i, x};
}

ll get(int i) {
	while(l <= r and q[l].first < i)
		l++;
	if(l <= r)
		return q[l].second;
	return 4e18;
}

int main() {

	scanf("%d %d", &n, &m);

	for(int i = 1; i <= n; i++)
		scanf("%d", a + i);

	for(int i = 1; i <= m; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		b[i] = {x, y};
	}

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

	for(int i = 1; i <= n; i++)
		sum[i] = sum[i - 1] + a[i];

	int it = 1;

	for(int i = 1; i <= m; i++) {
		while(it <= n and a[it] < b[i].first)
			it++;
		nxt[i] = it;
	}

	memset(dp, 63, sizeof(dp));

	dp[n + 1] = 0;

	for(int i = m; i >= 1; i--) {
		clear();
		auto add = [&](int j) {
			:: add(j, dp[j + 1] + cost(i, 1, j));
		};
		int it = 1;
		for(int j = 1; j <= n; j++) {
			while(it <= n and it - j + 1 <= b[i].second)
				add(it++);
			dp[j] = min(dp[j], get(j) - cost(i, 1, j - 1));
		}
	}

	if(dp[1] > 3e18)
		puts("-1");
	else
		printf("%lld\n", dp[1]);

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 5000 + 5;

int n, m;
int a[N];
ii b[N];
ll dp[N], dp2[N];

ll cost(int i, int l, int r) {
	ll res = 0;
	for(int j = l; j <= r; j++)
		res += abs(b[i].first - a[j]);
	return res;
}

void solve(int i, int l, int r, int optL, int optR) {
	if(l > r)
		return;
	int m = (l + r) >> 1;
	int mn = m - 1;
	dp2[m] = dp[m];
	for(int k = max(m, optL); k <= min(n, optR); k++) {
		if(k - m + 1 > b[i].second)
			break;
		if(dp[k + 1] + cost(i, m, k) < dp2[m]) {
			dp2[m] = dp[k + 1] + cost(i, m, k);
			mn = k;
		}
	}
	solve(i, l, m - 1, optL, mn);
	solve(i, m + 1, r, mn, optR);
}

int main() {

	scanf("%d %d", &n, &m);

	for(int i = 1; i <= n; i++)
		scanf("%d", a + i);

	for(int i = 1; i <= m; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		b[i] = {x, y};
	}

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

	memset(dp, 63, sizeof(dp));

	dp[n + 1] = 0;

	for(int i = m; i >= 1; i--) {
		solve(i, 1, n, 1, n);
		for(int i = 1; i <= n; i++)
			dp[i] = dp2[i];
	}

	if(dp[1] > 3e18)
		puts("-1");
	else
		printf("%lld\n", dp[1]);

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 5000 + 5;

int n, m;
int a[N];
ii b[N];
ll dp[N];

int main() {

	scanf("%d %d", &n, &m);

	for(int i = 1; i <= n; i++)
		scanf("%d", a + i);

	for(int i = 1; i <= m; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		b[i] = {x, y};
	}

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

	memset(dp, 63, sizeof(dp));

	dp[n + 1] = 0;

	for(int i = m; i >= 1; i--) {
		for(int j = 1; j <= n; j++) {
			ll res = 0;
			for(int k = j; k <= n; k++) {
				if(k - j + 1 > b[i].second)
					break;
				res += abs(a[k] - b[i].first);
				dp[j] = min(dp[j], dp[k + 1] + res);
			}
		}
	}

	if(dp[1] > 3e18)
		puts("-1");
	else
		printf("%lld\n", dp[1]);

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 5000 + 5;

int n, m;
int a[N];
ii b[N];
ll dp[N], dp2[N];

ll cost(int i, int l, int r) {
	ll res = 0;
	for(int j = l; j <= r; j++)
		res += abs(b[i].first - a[j]);
	return res;
}

void solve(int i, int l, int r, int optL, int optR) {
	if(l > r)
		return;
	int m = (l + r) >> 1;
	int mn = m - 1;
	dp2[m] = dp[m];
	optL=1,optR=n;
	for(int k = max(m, optL); k <= min(n, optR); k++) {
		if(k - m + 1 > b[i].second)
			break;
		if(dp[k + 1] + cost(i, m, k) < dp2[m]) {
			dp2[m] = dp[k + 1] + cost(i, m, k);
			mn = k;
		}
	}
	solve(i, l, m - 1, optL, mn);
	solve(i, m + 1, r, mn, optR);
}

int main() {

	scanf("%d %d", &n, &m);

	for(int i = 1; i <= n; i++)
		scanf("%d", a + i);

	for(int i = 1; i <= m; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		b[i] = {x, y};
	}

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

	memset(dp, 63, sizeof(dp));

	dp[n + 1] = 0;

	for(int i = m; i >= 1; i--) {
		solve(i, 1, n, 1, n);
		for(int i = 1; i <= n; i++)
			dp[i] = dp2[i];
	}

	if(dp[1] > 3e18)
		puts("-1");
	else
		printf("%lld\n", dp[1]);

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 5000 + 5;

int n, m;
int a[N], nxt[N];
ii b[N];
ll sum[N], dp[N];

ll cost(int i, int l, int r) {
	ll res = 0;
	if(nxt[i] > l) {
		int x = min(r, nxt[i] - 1);
		res += (ll) b[i].first * (x - l + 1) - (sum[x] - sum[l - 1]);
	}
	if(nxt[i] <= r) {
		int x = max(l, nxt[i]);
		res += sum[r] - sum[x - 1] - (ll) b[i].first * (r - x + 1);
	}
	return res;
}

int main() {

	scanf("%d %d", &n, &m);

	for(int i = 1; i <= n; i++)
		scanf("%d", a + i);

	for(int i = 1; i <= m; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		b[i] = {x, y};
	}

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

	for(int i = 1; i <= n; i++)
		sum[i] = sum[i - 1] + a[i];

	int it = 1;

	for(int i = 1; i <= m; i++) {
		while(it <= n and a[it] < b[i].first)
			it++;
		nxt[i] = it;
	}

	memset(dp, 63, sizeof(dp));

	dp[n + 1] = 0;

	for(int i = m; i >= 1; i--) {
		for(int j = 1; j <= n; j++) {
			for(int k = j; k <= n; k++) {
				if(k - j + 1 > b[i].second)
					break;
				dp[j] = min(dp[j], dp[k + 1] + cost(i, 1, k) - cost(i, 1, j - 1));
			}
		}
	}

	if(dp[1] > 3e18)
		puts("-1");
	else
		printf("%lld\n", dp[1]);

    return 0;

}


#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <math.h>
#include <iomanip>
#include <cmath>
#include <time.h>
#include <sstream>
#include <string>
#include <fstream>
#include <functional>
#include <list>
#include <cstring>
#include <iomanip>
#include <tuple>

using namespace std;

#define FOR(i,n) for (int i = 0; i < n; ++i)
#define FORN(i, a, b) for (int i = a; i < b; ++i)
#define FORE(i, a, b) for (int i = a; i <= b; ++i)
#define DOWN(i,n) for (int i = n - 1; i >= 0; --i)
#define FORX(i,c) for(decltype(c.begin())i=c.begin();i!=c.end();++i)
#define SQR(x) ((x)*(x))
#define ALL(c) (c).begin(), (c).end()

typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;

const double EPS = 1e-12;
const double DELTA = 1e-12;
const int MAXN = (int)5001;
const int INF = ~(1 << 31);
const LL LINF = ~(1LL << 63);
const int MOD = (int)1e9 + 7;

LL n, m, a, b;
int q[MAXN];
LL mice[MAXN], s[MAXN];
PII sh[MAXN];
LL d[MAXN][MAXN];

int main()
{
#ifdef _KMK
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif // _KMK

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> m;
	FORE(i, 1, n) {
		cin >> mice[i];
	}

	FORE(i, 1, m) {
		cin >> a >> b;
		sh[i] = { a, b };
	}

	sort(mice + 1, mice + n + 1);
	sort(sh + 1, sh + m + 1);

	FORE(i, 0, m) {
		FORE(j, 1, n) {
			d[i][j] = LINF;
		}
	}

	FORE(i, 1, m) {
		int head = 0, tail = 1;
		q[head] = 0;

		FORE(j, 1, n) {
			s[j] = s[j - 1] + abs(sh[i].first - mice[j]);
			if (head < tail && j - q[head] > sh[i].second) ++head;

			LL c = d[i - 1][j] - s[j];
			if (d[i - 1][j] != LINF) {
				while (head < tail && d[i-1][q[head]] - s[q[head]] >= c) --tail;
				q[tail++] = j;
			}

			if (head < tail) {
				d[i][j] = d[i - 1][q[head]] - s[q[head]] + s[j];
			}
		}
	}

	cout << ((d[m][n] == INF) ? -1 : d[m][n]);

	return 0;

}


#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <math.h>
#include <iomanip>
#include <cmath>
#include <time.h>
#include <sstream>
#include <string>
#include <fstream>
#include <functional>
#include <list>
#include <cstring>
#include <iomanip>
#include <tuple>

using namespace std;

#define FOR(i,n) for (int i = 0; i < n; ++i)
#define FORN(i, a, b) for (int i = a; i < b; ++i)
#define FORE(i, a, b) for (int i = a; i <= b; ++i)
#define DOWN(i,n) for (int i = n - 1; i >= 0; --i)
#define FORX(i,c) for(decltype(c.begin())i=c.begin();i!=c.end();++i)
#define SQR(x) ((x)*(x))
#define ALL(c) (c).begin(), (c).end()

typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;

const double EPS = 1e-12;
const double DELTA = 1e-12;
const int MAXN = (int)5001;
const int INF = ~(1 << 31);
const LL LINF = ~(1LL << 63);
const int MOD = (int)1e9 + 7;

LL n, m, a, b;
int q[MAXN];
LL mice[MAXN], s[MAXN];
PII sh[MAXN];
LL d[MAXN][MAXN];

int main()
{
#ifdef _KMK
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif // _KMK

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> m;
	FORE(i, 1, n) {
		cin >> mice[i];
	}

	FORE(i, 1, m) {
		cin >> a >> b;
		sh[i] = { a, b };
	}

	sort(mice + 1, mice + n + 1);
	sort(sh + 1, sh + m + 1);

	FORE(i, 0, m) {
		FORE(j, 1, n) {
			d[i][j] = LINF;
		}
	}

	FORE(i, 1, m) {
		int head = 0, tail = 1;
		q[head] = 0;

		FORE(j, 1, n) {
			s[j] = s[j - 1] + abs(sh[i].first - mice[j]);
			if (head < tail && j - q[head] > sh[i].second) ++head;

			LL c = d[i - 1][j] - s[j];
			if (d[i - 1][j] != LINF) {
				while (head < tail && d[i-1][q[head]] - s[q[head]] >= c) --tail;
				q[tail++] = j;
			}

			if (head < tail) {
				d[i][j] = d[i - 1][q[head]] - s[q[head]] + s[j];
			}
		}
	}

	cout << ((d[m][n] == LINF) ? -1 : d[m][n]);

	return 0;

}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5500+50;
typedef long long ll;
const int inf=1e9;

ll x[maxn];
struct node{
    ll p,c;
}my[maxn];
ll dp[maxn][maxn]; ///the 1-i hole hided 1-j mice
ll sum[maxn];
int Q[maxn],head,tail;
int s[maxn];
ll getDP(int i,int x,int y){
    return dp[i-1][x]+sum[y]-sum[x];
}
int main(){
    int n,m;
    memset(dp,127,sizeof(dp));
    dp[0][0]=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>x[i];
    sort(x+1,x+1+n);
    for(int i=1;i<=m;i++){
        cin>>my[i].p>>my[i].c;
    }
    sort(my+1,my+1+m,[](node &a,node &b){
        return a.p<b.p;
    });
    for(int i=1;i<=m;i++)s[i]=s[i-1]+my[i].c;
    if(s[m]<n){
        cout<<-1<<endl;return 0;
    }
    for(int i=1;i<=m;i++){
        tail=head=0;Q[++tail]=0;
        dp[i][0]=0;
        for(int j=1;j<=s[i]&&j<=n;j++){
            dp[i][j]=dp[i-1][j];
            sum[j]=sum[j-1]+abs(my[i].p-x[j]);
            while(j-Q[head+1]>my[i].c)head++;
            while(head+2<=tail&&getDP(i,Q[head+1],j)>=getDP(i,Q[head+2],j))head++;
            dp[i][j]=min(dp[i][j],getDP(i,Q[head+1],j));
            Q[++tail]=j;
        }
    }
    cout<<dp[m][n]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=5500+50;
typedef long long ll;
const int inf=1e9;

ll x[maxn];
struct node{
    ll p,c;
}my[maxn];
ll dp[maxn][maxn]; ///the 1-i hole hided 1-j mice
ll sum[maxn];
int Q[maxn],head,tail;
int s[maxn];
ll getDP(int i,int x,int y){
    return dp[i-1][x]+sum[y]-sum[x];
}
int main(){
    int n,m;
    memset(dp,127,sizeof(dp));
    dp[0][0]=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>x[i];
    sort(x+1,x+1+n);
    for(int i=1;i<=m;i++){
        cin>>my[i].p>>my[i].c;
    }
    sort(my+1,my+1+m,[](node &a,node &b){
        return a.p<b.p;
    });
    for(int i=1;i<=m;i++)s[i]=s[i-1]+my[i].c;
    for(int i=1;i<=m;i++){
        tail=head=0;Q[++tail]=0;
        dp[i][0]=0;
        for(int j=1;j<=s[i]&&j<=n;j++){
            dp[i][j]=dp[i-1][j];
            sum[j]=sum[j-1]+abs(my[i].p-x[j]);
            while(j-Q[head+1]>my[i].c)head++;
            while(head+2<=tail&&getDP(i,Q[head+1],j)>=getDP(i,Q[head+2],j))head++;
            dp[i][j]=min(dp[i][j],getDP(i,Q[head+1],j));
            Q[++tail]=j;
        }
    }
    cout<<dp[m][n]<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  sort(begin(X), end(X));

  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs;
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  vector< int64 > D(N + 1);
  for(int i = 0; i < N; i++) {
    D[i + 1] += D[i];
    D[i + 1] += X[i];
  }
  vector< int64 > mx(xs.size());
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    mx[P[i]] += C[i];
  }
  auto dp = make_v< int64 >(N + 1);
  fill_v(dp, infll);
  dp[0] = 0;
  int64 ptr = 0;

  int used = 0;

  for(int i = 0; i < xs.size(); i++) {
    if(mx[i] > 0) {

      auto dp2(dp);
      while(ptr < N && X[ptr] < xs[i]) ++ptr;
      // [0, ptr) [ptr, N)

      for(int j = ptr + 1; j <= N; j++) {
        int d = max(ptr, j - mx[i]);
        for(int k = d; k < min(d + 3, j); k++) {
          int64 dist = (D[j] - D[k]) - xs[i] * (j - k);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      vector< int64 > ushitapu(ptr);
      for(int k = 0; k < ptr; k++) {
        ushitapu[k] = dp[k] - xs[i] * k + D[k];
      }
      for(int k = ptr - 2; k >= 0; k--) {
        chmin(ushitapu[k], ushitapu[k + 1]);
      }

      for(int j = ptr + 1; j <= N; j++) {
        int64 malta = (D[j] - D[ptr]) - xs[i] * (j - ptr);
        int left = max(0LL, j - mx[i]);
        if(left <= ptr - 1) {
          chmin(dp2[j], ushitapu[left] + malta + xs[i] * ptr - D[ptr]);
        }
        // dp[k]+malta + xs[i]*(ptr-k)-(D[ptr]-D[k])
      }

      for(int j = 0; j <= ptr; j++) {
        int d = min(used, j - 1);
        //最も右のやつ
        for(int k = d; k >= max(0, d - 2); k--) {
          if(j - k > mx[i]) break;
          int64 dist = xs[i] * (j - k) - (D[j] - D[k]);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      used += mx[i];

      dp.swap(dp2);
    }
  }
  cout << dp[N] << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  sort(begin(X), end(X));

  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs;
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  vector< int64 > D(N + 1);
  for(int i = 0; i < N; i++) {
    D[i + 1] += D[i];
    D[i + 1] += X[i];
  }
  vector< int64 > mx(xs.size());
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    mx[P[i]] += C[i];
  }
  auto dp = make_v< int64 >(N + 1);
  fill_v(dp, infll);
  dp[0] = 0;
  int64 ptr = 0;

  int used = 0;

  for(int i = 0; i < xs.size(); i++) {
    if(mx[i] > 0) {

      auto dp2(dp);
      while(ptr < N && X[ptr] < xs[i]) ++ptr;
      // [0, ptr) [ptr, N)

      {
        vector< int64 > ushitapu(N + 1, infll);
        for(int k = ptr; k <= N; k++) {
          ushitapu[k] = dp[k] - D[k] + xs[i] * k;
        }
        for(int k = N - 2; k >= ptr; k--) {
          chmin(ushitapu[k], ushitapu[k + 1]);
        }
        for(int j = ptr + 1; j <= N; j++) {
          int left = max(j - mx[i], ptr);
          chmin(dp2[j], ushitapu[left] + D[j] - xs[i] * j);
        }
      }

      {
        vector< int64 > ushitapu(ptr);
        for(int k = 0; k < ptr; k++) {
          ushitapu[k] = dp[k] - xs[i] * k + D[k];
        }
        for(int k = ptr - 2; k >= 0; k--) {
          chmin(ushitapu[k], ushitapu[k + 1]);
        }

        for(int j = ptr + 1; j <= N; j++) {
          int64 malta = (D[j] - D[ptr]) - xs[i] * (j - ptr);
          int left = max(0LL, j - mx[i]);
          if(left <= ptr - 1) {
            chmin(dp2[j], ushitapu[left] + malta + xs[i] * ptr - D[ptr]);
          }
        }
      }

      {

        vector< int64 > ushitapu(ptr);
        for(int k = 0; k < ptr; k++) {
          ushitapu[k] = -xs[i] * k + D[k] + dp[k];
        }
        for(int k = ptr - 2; k >= 0; k--) {
          chmin(ushitapu[k], ushitapu[k + 1]);
        }

        for(int j = 1; j <= ptr; j++) {
          int left = max(j - mx[i], 0LL);
          chmin(dp2[j], ushitapu[left]+xs[i]*j-D[j]);
        }
      }

      used += mx[i];

      dp.swap(dp2);
    }
  }
  cout << dp[N] << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  sort(begin(X), end(X));

  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs;
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  vector< int64 > D(N + 1);
  for(int i = 0; i < N; i++) {
    D[i + 1] += D[i];
    D[i + 1] += X[i];
  }
  vector< int64 > mx(xs.size());
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    mx[P[i]] += C[i];
  }
  auto dp = make_v< int64 >(N + 1);
  fill_v(dp, infll);
  dp[0] = 0;
  int64 ptr = 0;

  int used = 0;

  for(int i = 0; i < xs.size(); i++) {
    if(mx[i] > 0) {

      auto dp2(dp);
      while(ptr < N && X[ptr] < xs[i]) ++ptr;
      // [0, ptr) [ptr, N)

      for(int j = ptr + 1; j <= N; j++) {
        int d = max(ptr, j - mx[i]);
        for(int k = d; k < min(d + 2, j); k++) {
          int64 dist = (D[j] - D[k]) - xs[i] * (j - k);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      vector< int64 > ushitapu(ptr);
      for(int k = 0; k < ptr; k++) {
        ushitapu[k] = dp[k] - xs[i] * k + D[k];
      }
      for(int k = ptr - 2; k >= 0; k--) {
        chmin(ushitapu[k], ushitapu[k + 1]);
      }

      for(int j = ptr + 1; j <= N; j++) {
        int64 malta = (D[j] - D[ptr]) - xs[i] * (j - ptr);
        int left = max(0LL, j - mx[i]);
        if(left <= ptr - 1) {
          chmin(dp2[j], ushitapu[left] + malta + xs[i] * ptr - D[ptr]);
        }
      }

      for(int j = 0; j <= ptr; j++) {
        int d = min(used, j - 1);
        //最も右のやつ
        for(int k = d; k >= max(0, d - 1); k--) {
          if(j - k > mx[i]) break;
          int64 dist = xs[i] * (j - k) - (D[j] - D[k]);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      used += mx[i];

      dp.swap(dp2);
    }
  }
  cout << dp[N] << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  sort(begin(X), end(X));

  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs;
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  vector< int64 > D(N + 1);
  for(int i = 0; i < N; i++) {
    D[i + 1] += D[i];
    D[i + 1] += X[i];
  }
  vector< int64 > mx(xs.size());
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    mx[P[i]] += C[i];
  }
  auto dp = make_v< int64 >(N + 1);
  fill_v(dp, infll);
  dp[0] = 0;
  int64 ptr = 0;

  int used = 0;

  for(int i = 0; i < xs.size(); i++) {
    if(mx[i] > 0) {

      auto dp2(dp);
      while(ptr < N && X[ptr] < xs[i]) ++ptr;
      // [0, ptr) [ptr, N)

      for(int j = ptr + 1; j <= N; j++) {
        int d = max(ptr, j - mx[i]);
        for(int k = d; k < min(d + 2, j); k++) {
          // if(j - k > mx[i]) break;
          //mx[i]のスライド最小値にきあくん
          int64 dist = (D[j] - D[k]) - xs[i] * (j - k);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      vector< int64 > ushitapu(ptr);
      for(int k = 0; k < ptr; k++) {
        ushitapu[k] = dp[k] - xs[i] * k + D[k];
      }
      for(int k = ptr - 2; k >= 0; k--) {
        chmin(ushitapu[k], ushitapu[k + 1]);
      }

      for(int j = ptr + 1; j <= N; j++) {
        int64 malta = (D[j] - D[ptr]) * mx[i] * (j - ptr);
        int left = j - mx[i];
        if(left <= ptr - 1) {
          chmin(dp2[j], ushitapu[left] + malta + xs[i] * ptr - D[ptr]);
        }
      }

      for(int j = 0; j <= ptr; j++) {
        int d = min(used, j - 1);
        //最も右のやつ
        for(int k = d; k >= max(0, d - 1); k--) {
          if(j - k > mx[i]) break;
          int64 dist = xs[i] * j - xs[i] * k - D[ptr] + D[k];
          chmin(dp2[j], dp[k] + dist);
        }
      }

      used += mx[i];

      dp.swap(dp2);
    }
  }
  cout << dp[N] << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  sort(begin(X), end(X));

  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs;
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  vector< int64 > D(N + 1);
  for(int i = 0; i < N; i++) {
    D[i + 1] += D[i];
    D[i + 1] += X[i];
  }
  vector< int64 > mx(xs.size());
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    mx[P[i]] += C[i];
  }
  auto dp = make_v< int64 >(N + 1);
  fill_v(dp, infll);
  dp[0] = 0;
  int64 ptr = 0;

  int used = 0;

  for(int i = 0; i < xs.size(); i++) {
    if(mx[i] > 0) {

      auto dp2(dp);
      while(ptr < N && X[ptr] < xs[i]) ++ptr;
      // [0, ptr) [ptr, N)

      for(int j = ptr + 1; j <= N; j++) {
        // ptrしか試さなくてもよくない？
        int d = max(ptr, j - mx[i]);
        for(int k = d; k < min(d + 2, j); k++) {
          // if(j - k > mx[i]) break;
          //mx[i]のスライド最小値にきあくん
          int64 dist = (D[j] - D[k]) - xs[i] * j + xs[i] * k;
          chmin(dp2[j], dp[k] + dist);
        }
      }

      vector< int64 > ushitapu(ptr);
      for(int k = 0; k < ptr; k++) {
        ushitapu[k] = dp[k] - xs[i] * k + D[k];
      }
      for(int k = ptr - 2; k >= 0; k--) {
        chmin(ushitapu[k], ushitapu[k + 1]);
      }

      for(int j = ptr + 1; j <= N; j++) {
        int64 malta = (D[j] - D[ptr]) * mx[i] * (j - ptr);
        int64 latte = infll;
        int left = j - mx[i];
        if(left <= ptr - 1) {
          int64 dist = dp[left] - xs[i] * left + D[left];
          chmin(dp2[j], latte + malta + xs[i] * ptr - D[ptr]);
        }
      }

      for(int j = 0; j <= ptr; j++) {
        int d = min(used, j - 1);
        //最も右のやつ
        for(int k = d; k >= max(0, d - 1); k--) {
          if(j - k > mx[i]) break;
          int64 dist = xs[i] * j - xs[i] * k - D[ptr] + D[k];
          chmin(dp2[j], dp[k] + dist);
        }
      }

      used += mx[i];

      dp.swap(dp2);
    }
  }
  cout << dp[N] << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  sort(begin(X), end(X));

  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs;
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  vector< int64 > D(N + 1);
  for(int i = 0; i < N; i++) {
    D[i + 1] += D[i];
    D[i + 1] += X[i];
  }
  vector< int64 > mx(xs.size());
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    mx[P[i]] += C[i];
  }
  auto dp = make_v< int64 >(N + 1);
  fill_v(dp, infll);
  dp[0] = 0;
  int64 ptr = 0;

  int used = 0;

  for(int i = 0; i < xs.size(); i++) {
    if(mx[i] > 0) {

      auto dp2(dp);
      while(ptr < N && X[ptr] < xs[i]) ++ptr;
      // [0, ptr) [ptr, N)

      for(int j = ptr + 1; j <= N; j++) {
        int d = max(ptr, j - mx[i]);
        for(int k = d; k < min(d + 2, j); k++) {
          int64 dist = (D[j] - D[k]) - xs[i] * (j - k);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      vector< int64 > ushitapu(ptr);
      for(int k = 0; k < ptr; k++) {
        ushitapu[k] = dp[k] - xs[i] * k + D[k];
      }
      for(int k = ptr - 2; k >= 0; k--) {
        chmin(ushitapu[k], ushitapu[k + 1]);
      }

      for(int j = ptr + 1; j <= N; j++) {
        int64 malta = (D[j] - D[ptr]) - xs[i] * (j - ptr);
        int left = max(0LL, j - mx[i]);
        if(left <= ptr - 1) {
          chmin(dp2[j], ushitapu[left] + malta + xs[i] * ptr - D[ptr]);
        }
        // dp[k]+malta + xs[i]*(ptr-k)-(D[ptr]-D[k])
      }

      for(int j = 0; j <= ptr; j++) {
        int d = min(used, j - 1);
        //最も右のやつ
        for(int k = d; k >= max(0, d - 1); k--) {
          if(j - k > mx[i]) break;
          int64 dist = xs[i] * (j - k) - (D[j] - D[k]);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      used += mx[i];

      dp.swap(dp2);
    }
  }
  cout << dp[N] << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  sort(begin(X), end(X));

  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs;
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  vector< int64 > D(N + 1);
  for(int i = 0; i < N; i++) {
    D[i + 1] += D[i];
    D[i + 1] += X[i];
  }
  vector< int64 > mx(xs.size());
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    mx[P[i]] += C[i];
  }
  auto dp = make_v< int64 >(N + 1);
  fill_v(dp, infll);
  dp[0] = 0;
  int64 ptr = 0;

  int used = 0;

  for(int i = 0; i < xs.size(); i++) {
    if(mx[i] > 0) {

      auto dp2(dp);
      while(ptr < N && X[ptr] < xs[i]) ++ptr;
      // [0, ptr) [ptr, N)

      for(int j = ptr + 1; j <= N; j++) {
        int d = max(ptr, j - mx[i]);
        for(int k = d; k < min(d + 2, j); k++) {
          // if(j - k > mx[i]) break;
          //mx[i]のスライド最小値にきあくん
          int64 dist = (D[j] - D[k]) - xs[i] * (j - k);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      vector< int64 > ushitapu(ptr);
      for(int k = 0; k < ptr; k++) {
        ushitapu[k] = dp[k] - xs[i] * k + D[k];
      }
      for(int k = ptr - 2; k >= 0; k--) {
        chmin(ushitapu[k], ushitapu[k + 1]);
      }

      for(int j = ptr + 1; j <= N; j++) {
        int64 malta = (D[j] - D[ptr]) * mx[i] * (j - ptr);
        int left = max(0LL, j - mx[i]);
        if(left <= ptr - 1) {
          chmin(dp2[j], ushitapu[left] + malta + xs[i] * ptr - D[ptr]);
        }
      }

      for(int j = 0; j <= ptr; j++) {
        int d = min(used, j - 1);
        //最も右のやつ
        for(int k = d; k >= max(0, d - 1); k--) {
          if(j - k > mx[i]) break;
          int64 dist = xs[i] * (j - k) - (D[j] - D[k]);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      used += mx[i];

      dp.swap(dp2);
    }
  }
  cout << dp[N] << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

template< typename T >
struct RadixHeap
{
  using uint = unsigned;
  vector< pair< uint, T > > v[64];
  uint size, last;

  RadixHeap() : size(0), last(0) {}

  bool empty() const { return size == 0; }

  inline int getbit(int a)
  {
    return a ? 64 - __builtin_clz(a) : 0;
  }

  void push(uint key, const T &value)
  {
    ++size;
    v[getbit(key ^ last)].emplace_back(key, value);
  }

  pair< uint, T > pop()
  {
    if(v[0].empty()) {
      int idx = 1;
      while(v[idx].empty()) ++idx;
      last = min_element(begin(v[idx]), end(v[idx]))->first;
      for(auto &p : v[idx]) v[getbit(p.first ^ last)].emplace_back(p);
      v[idx].clear();
    }
    --size;
    auto ret = v[0].back();
    v[0].pop_back();
    return ret;
  }
};

template< typename flow_t, typename cost_t >
struct PrimalDual {
  const cost_t INF;

  struct edge {
    int to;
    flow_t cap;
    cost_t cost;
    int rev;
    bool isrev;
  };
  vector< vector< edge > > graph;
  vector< cost_t > potential, min_cost;
  vector< int > prevv, preve;

  PrimalDual(int V) : graph(V), INF(numeric_limits< cost_t >::max()) {}

  void add_edge(int from, int to, flow_t cap, cost_t cost) {
    graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});
    graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});
  }

  cost_t min_cost_flow(int s, int t, flow_t f) {
    int V = (int) graph.size();
    cost_t ret = 0;
    using Pi = pair< cost_t, int >;
    RadixHeap< cost_t > que;
    potential.assign(V, 0);
    preve.assign(V, -1);
    prevv.assign(V, -1);

    while(f > 0) {
      min_cost.assign(V, INF);
      que.push(0, s);
      min_cost[s] = 0;
      while(!que.empty()) {
        Pi p = que.pop();
        if(min_cost[p.second] < p.first) continue;
        for(int i = 0; i < graph[p.second].size(); i++) {
          edge &e = graph[p.second][i];
          cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];
          if(e.cap > 0 && min_cost[e.to] > nextCost) {
            min_cost[e.to] = nextCost;
            prevv[e.to] = p.second, preve[e.to] = i;
            que.push(min_cost[e.to], e.to);
          }
        }
      }
      if(min_cost[t] == INF) return -1;
      for(int v = 0; v < V; v++) potential[v] += min_cost[v];
      flow_t addflow = f;
      for(int v = t; v != s; v = prevv[v]) {
        addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
      }
      f -= addflow;
      ret += addflow * potential[t];
      for(int v = t; v != s; v = prevv[v]) {
        edge &e = graph[prevv[v]][preve[v]];
        e.cap -= addflow;
        graph[v][e.rev].cap += addflow;
      }
    }
    return ret;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      for(auto &e : graph[i]) {
        if(e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << rev_e.cap + e.cap << ")" << endl;
      }
    }
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs(X);
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  int64 S = xs.size(), T = xs.size() + 1;
  PrimalDual< int64, int64 > flow(xs.size() + 2);
  for(int i = 1; i < xs.size(); i++) {
    flow.add_edge(i, i - 1, flow.INF, xs[i] - xs[i - 1]);
    flow.add_edge(i - 1, i, flow.INF, xs[i] - xs[i - 1]);
  }
  for(int i = 0; i < N; i++) {
    X[i] = lower_bound(begin(xs), end(xs), X[i]) - begin(xs);
    flow.add_edge(S, X[i], 1, 0);
  }
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    flow.add_edge(P[i], T, C[i], 0);
  }
  cout << flow.min_cost_flow(S, T, N) << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  sort(begin(X), end(X));

  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs;
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  vector< int64 > D(N + 1);
  for(int i = 0; i < N; i++) {
    D[i + 1] += D[i];
    D[i + 1] += X[i];
  }
  vector< int64 > mx(xs.size());
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    mx[P[i]] += C[i];
  }
  auto dp = make_v< int64 >(N + 1);
  fill_v(dp, infll);
  dp[0] = 0;
  int64 ptr = 0;

  int used = 0;

  for(int i = 0; i < xs.size(); i++) {
    if(mx[i] > 0) {

      auto dp2(dp);
      while(ptr < N && X[ptr] < xs[i]) ++ptr;
      // [0, ptr) [ptr, N)

      for(int j = ptr + 1; j <= N; j++) {
        int d = max(ptr, j - mx[i]);
        for(int k = d; k < min(d + 2, j); k++) {
          int64 dist = (D[j] - D[k]) - xs[i] * (j - k);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      vector< int64 > ushitapu(ptr);
      for(int k = 0; k < ptr; k++) {
        ushitapu[k] = dp[k] - xs[i] * k + D[k];
      }
      for(int k = ptr - 2; k >= 0; k--) {
        chmin(ushitapu[k], ushitapu[k + 1]);
      }

      for(int j = ptr + 1; j <= N; j++) {
        int64 malta = (D[j] - D[ptr]) - mx[i] * (j - ptr);
        int left = max(0LL, j - mx[i]);
        if(left <= ptr - 1) {
          chmin(dp2[j], ushitapu[left] + malta + xs[i] * ptr - D[ptr]);
        }
      }

      for(int j = 0; j <= ptr; j++) {
        int d = min(used, j - 1);
        //最も右のやつ
        for(int k = d; k >= max(0, d - 1); k--) {
          if(j - k > mx[i]) break;
          int64 dist = xs[i] * (j - k) - (D[j] - D[k]);
          chmin(dp2[j], dp[k] + dist);
        }
      }

      used += mx[i];

      dp.swap(dp2);
    }
  }
  cout << dp[N] << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

template< typename flow_t, typename cost_t >
struct PrimalDual {
  const cost_t INF;

  struct edge {
    int to;
    flow_t cap;
    cost_t cost;
    int rev;
    bool isrev;
  };
  vector< vector< edge > > graph;
  vector< cost_t > potential, min_cost;
  vector< int > prevv, preve;

  PrimalDual(int V) : graph(V), INF(numeric_limits< cost_t >::max()) {}

  void add_edge(int from, int to, flow_t cap, cost_t cost) {
    graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});
    graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});
  }

  cost_t min_cost_flow(int s, int t, flow_t f) {
    int V = (int) graph.size();
    cost_t ret = 0;
    using Pi = pair< cost_t, int >;
    priority_queue< Pi, vector< Pi >, greater< Pi > > que;
    potential.assign(V, 0);
    preve.assign(V, -1);
    prevv.assign(V, -1);

    while(f > 0) {
      min_cost.assign(V, INF);
      que.emplace(0, s);
      min_cost[s] = 0;
      while(!que.empty()) {
        Pi p = que.top();
        que.pop();
        if(min_cost[p.second] < p.first) continue;
        for(int i = 0; i < graph[p.second].size(); i++) {
          edge &e = graph[p.second][i];
          cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];
          if(e.cap > 0 && min_cost[e.to] > nextCost) {
            min_cost[e.to] = nextCost;
            prevv[e.to] = p.second, preve[e.to] = i;
            que.emplace(min_cost[e.to], e.to);
          }
        }
      }
      if(min_cost[t] == INF) return -1;
      for(int v = 0; v < V; v++) potential[v] += min_cost[v];
      flow_t addflow = f;
      for(int v = t; v != s; v = prevv[v]) {
        addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
      }
      f -= addflow;
      ret += addflow * potential[t];
      for(int v = t; v != s; v = prevv[v]) {
        edge &e = graph[prevv[v]][preve[v]];
        e.cap -= addflow;
        graph[v][e.rev].cap += addflow;
      }
    }
    return ret;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      for(auto &e : graph[i]) {
        if(e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << rev_e.cap + e.cap << ")" << endl;
      }
    }
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs(X);
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  int64 S = xs.size(), T = xs.size() + 1;
  PrimalDual< int64, int64 > flow(xs.size() + 2);
  for(int i = 1; i < xs.size(); i++) {
    flow.add_edge(i, i - 1, flow.INF, xs[i] - xs[i - 1]);
    flow.add_edge(i - 1, i, flow.INF, xs[i] - xs[i - 1]);
  }
  for(int i = 0; i < N; i++) {
    X[i] = lower_bound(begin(xs), end(xs), X[i]) - begin(xs);
    flow.add_edge(S, X[i], 1, 0);
  }
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    flow.add_edge(P[i], T, C[i], 0);
  }
  cout << flow.min_cost_flow(S, T, N) << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

template< typename T >
struct RadixHeap {
  using uint = unsigned;
  vector< pair< uint, T > > v[64];
  uint size, last;

  RadixHeap() : size(0), last(0) {}

  bool empty() const { return size == 0; }

  inline int getbit(int a) {
    return a ? 64 - __builtin_clz(a) : 0;
  }

  void push(uint key, const T &value) {
    ++size;
    v[getbit(key ^ last)].emplace_back(key, value);
  }

  pair< uint, T > pop() {
    if(v[0].empty()) {
      int idx = 1;
      while(v[idx].empty()) ++idx;
      last = min_element(begin(v[idx]), end(v[idx]))->first;
      for(auto &p : v[idx]) v[getbit(p.first ^ last)].emplace_back(p);
      v[idx].clear();
    }
    --size;
    auto ret = v[0].back();
    v[0].pop_back();
    return ret;
  }
};

template< typename flow_t, typename cost_t >
struct PrimalDual {
  const cost_t INF;

  struct edge {
    int to;
    flow_t cap;
    cost_t cost;
    int rev;
    bool isrev;
  };
  vector< vector< edge > > graph;
  vector< cost_t > potential, min_cost;
  vector< int > prevv, preve;

  PrimalDual(int V) : graph(V), INF(numeric_limits< cost_t >::max()) {}

  void add_edge(int from, int to, flow_t cap, cost_t cost) {
    graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});
    graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});
  }

  cost_t min_cost_flow(int s, int t, flow_t f) {
    int V = (int) graph.size();
    cost_t ret = 0;
    using Pi = pair< cost_t, int >;

    auto latte = 63 - __builtin_clzll(f);

    for(int uku = latte; uku >= 0; uku--) {
      flow_t now = flow_t(1) << uku;
      RadixHeap< cost_t > que;
      potential.assign(V, 0);
      preve.assign(V, -1);
      prevv.assign(V, -1);
      while(f > 0) {
        min_cost.assign(V, INF);
        que.push(0, s);
        min_cost[s] = 0;
        while(!que.empty()) {
          Pi p = que.pop();
          if(min_cost[p.second] < p.first) continue;
          for(int i = 0; i < graph[p.second].size(); i++) {
            edge &e = graph[p.second][i];
            cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];
            if(e.cap >= now && min_cost[e.to] > nextCost) {
              min_cost[e.to] = nextCost;
              prevv[e.to] = p.second, preve[e.to] = i;
              que.push(min_cost[e.to], e.to);
            }
          }
        }
        if(min_cost[t] == INF) break;
        for(int v = 0; v < V; v++) potential[v] += min_cost[v];
        flow_t addflow = f;
        for(int v = t; v != s; v = prevv[v]) {
          addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
        }
        f -= addflow;
        ret += addflow * potential[t];
        for(int v = t; v != s; v = prevv[v]) {
          edge &e = graph[prevv[v]][preve[v]];
          e.cap -= addflow;
          graph[v][e.rev].cap += addflow;
        }
      }
    }
    return ret;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      for(auto &e : graph[i]) {
        if(e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << rev_e.cap + e.cap << ")" << endl;
      }
    }
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs(X);
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  int64 S = xs.size(), T = xs.size() + 1;
  PrimalDual< int64, int64 > flow(xs.size() + 2);
  for(int i = 1; i < xs.size(); i++) {
    flow.add_edge(i, i - 1, flow.INF, xs[i] - xs[i - 1]);
    flow.add_edge(i - 1, i, flow.INF, xs[i] - xs[i - 1]);
  }
  for(int i = 0; i < N; i++) {
    X[i] = lower_bound(begin(xs), end(xs), X[i]) - begin(xs);
    flow.add_edge(S, X[i], 1, 0);
  }
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    flow.add_edge(P[i], T, C[i], 0);
  }
  cout << flow.min_cost_flow(S, T, N) << endl;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

template< typename T >
struct RadixHeap {
  using uint = unsigned;
  vector< pair< uint, T > > v[64];
  uint size, last;

  RadixHeap() : size(0), last(0) {}

  bool empty() const { return size == 0; }

  inline int getbit(int a) {
    return a ? 64 - __builtin_clz(a) : 0;
  }

  void push(uint key, const T &value) {
    ++size;
    v[getbit(key ^ last)].emplace_back(key, value);
  }

  pair< uint, T > pop() {
    if(v[0].empty()) {
      int idx = 1;
      while(v[idx].empty()) ++idx;
      last = min_element(begin(v[idx]), end(v[idx]))->first;
      for(auto &p : v[idx]) v[getbit(p.first ^ last)].emplace_back(p);
      v[idx].clear();
    }
    --size;
    auto ret = v[0].back();
    v[0].pop_back();
    return ret;
  }
};

template< typename flow_t, typename cost_t >
struct PrimalDual {
  const cost_t INF;

  struct edge {
    int to;
    flow_t cap;
    cost_t cost;
    int rev;
    bool isrev;
  };
  vector< vector< edge > > graph;
  vector< cost_t > potential, min_cost;
  vector< int > prevv, preve;

  PrimalDual(int V) : graph(V), INF(numeric_limits< cost_t >::max()) {}

  void add_edge(int from, int to, flow_t cap, cost_t cost) {
    graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});
    graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});
  }

  void add_edge_undirected(int from, int to, flow_t cap, cost_t cost) {
    graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});
    graph[to].emplace_back((edge) {from, cap, cost, (int) graph[from].size() - 1, true});
  }

  cost_t min_cost_flow(int s, int t, flow_t f) {
    int V = (int) graph.size();
    cost_t ret = 0;
    using Pi = pair< cost_t, int >;
    RadixHeap< cost_t > que;
    potential.assign(V, 0);
    preve.assign(V, -1);
    prevv.assign(V, -1);

    while(f > 0) {
      min_cost.assign(V, INF);
      que.push(0, s);
      min_cost[s] = 0;
      while(!que.empty()) {
        Pi p = que.pop();
        if(min_cost[p.second] < p.first) continue;
        for(int i = 0; i < graph[p.second].size(); i++) {
          edge &e = graph[p.second][i];
          cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];
          if(e.cap > 0 && min_cost[e.to] > nextCost) {
            min_cost[e.to] = nextCost;
            prevv[e.to] = p.second, preve[e.to] = i;
            que.push(min_cost[e.to], e.to);
          }
        }
      }
      if(min_cost[t] == INF) return -1;
      for(int v = 0; v < V; v++) potential[v] += min_cost[v];
      flow_t addflow = f;
      for(int v = t; v != s; v = prevv[v]) {
        addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
      }
      f -= addflow;
      ret += addflow * potential[t];
      for(int v = t; v != s; v = prevv[v]) {
        edge &e = graph[prevv[v]][preve[v]];
        e.cap -= addflow;
        graph[v][e.rev].cap += addflow;
      }
    }
    return ret;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      for(auto &e : graph[i]) {
        if(e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << rev_e.cap + e.cap << ")" << endl;
      }
    }
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector< int64 > X(N);
  cin >> X;
  vector< int64 > P(M), C(M);
  int64 sum = 0;
  vector< int64 > xs(X);
  for(int i = 0; i < M; i++) {
    cin >> P[i] >> C[i];
    sum += C[i];
    xs.emplace_back(P[i]);
  }
  if(sum < N) {
    cout << -1 << endl;
    return 0;
  }
  sort(begin(xs), end(xs));
  xs.erase(unique(begin(xs), end(xs)), end(xs));
  int64 S = xs.size(), T = xs.size() + 1;
  PrimalDual< int64, int64 > flow(xs.size() + 2);
  for(int i = 1; i < xs.size(); i++) {
    flow.add_edge_undirected(i, i - 1, flow.INF, xs[i] - xs[i - 1]);
  }
  for(int i = 0; i < N; i++) {
    X[i] = lower_bound(begin(xs), end(xs), X[i]) - begin(xs);
    flow.add_edge(S, X[i], 1, 0);
  }
  for(int i = 0; i < M; i++) {
    P[i] = lower_bound(begin(xs), end(xs), P[i]) - begin(xs);
    flow.add_edge(P[i], T, C[i], 0);
  }
  cout << flow.min_cost_flow(S, T, N) << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define N 5003
#define OO 99999999999999LL
#define mp make_pair
P p[N];
ll n, k, x, y, dp[N][N], res, m[N], sg[N], a[N], lz[N];
void build(int low,int high,int pos,int i)
{
	lz[pos] = 0;
	if (low == high)
	{
		sg[pos] = dp[i][low];
		return;
	}
	ll mid = (high + low) / 2;
	build(low, mid, pos * 2 + 1,i);
	build(mid + 1, high, pos * 2 + 2,i);
	sg[pos] = min(sg[pos * 2 + 1], sg[pos * 2 + 2]);
}
inline void lazy(int low, int high,int pos)
{
	if (!lz[pos])return;
	sg[pos] += lz[pos];
	if (low != high)
		lz[pos * 2 + 1] += lz[pos], lz[pos * 2 + 2] += lz[pos];
	lz[pos] = 0;
}
ll query(int low, int high, int pos, int qlow, int qhigh)
{
	lazy(low, high, pos);
	if (qlow > high || qhigh < low)
		return OO;
	if (qlow <= low&&high <= qhigh)
		return sg[pos];
	ll mid = (high + low) / 2;
	return min(query(low,mid, pos * 2 + 1, qlow, qhigh), query(mid+1, high , pos * 2 + 2, qlow, qhigh));
}
void update(int low, int high, int pos, int qlow, int qhigh, ll va)
{
	lazy(low, high, pos);
	if (qlow > high || qhigh < low)
		return;
	if (qlow <= low&&high <= qhigh)
	{
		sg[pos] += va;
		if (low != high)
			lz[pos * 2 + 1] += va, lz[pos * 2 + 2] += va;
		return;
	}
	ll mid = (high + low) / 2;
	update(low, mid, pos * 2 + 1, qlow, qhigh, va);
	update(mid + 1, high, pos * 2 + 2, qlow, qhigh, va);
	sg[pos] = min(sg[pos * 2 + 1], sg[pos * 2 + 2]);
}
inline ll dis(int hole,int mice)
{
	return max(ll(p[hole].first), m[mice]) - min(ll(p[hole].first),m[mice]);
}
int main()
{
	scanf("%lld%lld", &n, &k);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &m[i]);
	for (int i = 1; i <= k; i++)
		scanf("%lld%lld", &x, &y), p[i] = mp(x, y);
	sort(m + 1, m + n + 1);
	sort(p + 1, p + k + 1);
	for (int i = 1; i <= n; i++)
		dp[0][i] = OO;
	for (int i = 1; i <= k; i++)
	{
		build(0, n, 0 , i-1);
		for (int j = 1; j <= n; j++)
		{
			update(0, n, 0, 0, j-1, dis(i, j));
			dp[i][j] = query(0,n,0,max(0,j - p[i].second), j-1);
			dp[i][j] = min(dp[i][j], dp[i - 1][j]);
		}

	}
	res = dp[k][n];
	if (res >= OO)res = -1;
	printf("%lld\n", res);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
#define N 5003
#define OO 99999999999999LL
ll all = 0, m[N], n, k, res, dp[N][N], a[N], stop, last, b[N];
deque<int>d;
P p[N];
inline ll dis(int hole, int mice)
{
	return max(p[hole].first, m[mice]) - min(p[hole].first, m[mice]);
}
void to(int pos)
{
	for (; stop <= pos; stop++)
	{
		while (!d.empty() && d.back() < last)
			d.pop_back();
		while (!d.empty() && a[d.front()] > a[stop])
			d.pop_front();
		d.push_front(stop);
	}
}
int main()
{
	scanf("%lld%lld", &n, &k);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &m[i]);
	for (int i = 1; i <= k; i++)
		scanf("%lld%lld", &p[i].first, &p[i].second);
	sort(m + 1, m + n + 1);
	sort(p + 1, p + k + 1);
	for (int i = 1; i <= n; i++)
		dp[0][i] = OO;
	for (int i = 1; i <= k; i++)
	{
		for (int j = n; j >= 1; j--)
			b[j] = b[j + 1] + dis(i, j), a[j] = b[j] + dp[i - 1][j-1];
		stop = 1;
		d.clear();
		for (int j = 1; j <= n; j++)
		{
			all = -b[j + 1];
			last = j - p[i].second + 1;
			to(j);
			dp[i][j] = min(dp[i - 1][j], a[d.back()] + all);
		}
	}
	res = dp[k][n];
	if (res >= OO)res = -1;
	printf("%lld\n", res);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
#define N 5003
#define OO 99999999999999LL
#define mp make_pair
P p[N];
ll n, k, x, y, dp[N][N], res, m[N], sg[N], a[N], lz[N];
void build(ll low, ll high, ll pos)
{
	if (low == high)
	{
		sg[pos] = a[low];
		return;
	}
	ll mid = (high + low) / 2;
	build(low, mid, pos * 2 + 1);
	build(mid + 1, high, pos * 2 + 2);
	sg[pos] = min(sg[pos * 2 + 1], sg[pos * 2 + 2]);
}
inline void lazy(ll low, ll high, ll pos)
{
	if (!lz[pos])return;
	sg[pos] += lz[pos];
	if (low != high)
		lz[pos * 2 + 1] += lz[pos], lz[pos * 2 + 2] += lz[pos];
	lz[pos] = 0;
}
ll query(ll low, ll high, ll pos, ll qlow, ll qhigh)
{
	lazy(low, high, pos);
	if (qlow > high || qhigh < low)
		return OO;
	if (qlow <= low&&high <= qhigh)
		return sg[pos];
	ll mid = (high + low) / 2;
	return min(query(low,mid, pos * 2 + 1, qlow, qhigh), query(mid+1, high , pos * 2 + 2, qlow, qhigh));
}
void update(ll low, ll high, ll pos, ll qlow, ll qhigh, ll va)
{
	lazy(low, high, pos);
	if (qlow > high || qhigh < low)
		return;
	if (qlow <= low&&high <= qhigh)
	{
		sg[pos] += va;
		if (low != high)
			lz[pos * 2 + 1] += va, lz[pos * 2 + 2] += va;
		return;
	}
	ll mid = (high + low) / 2;
	update(low, mid, pos * 2 + 1, qlow, qhigh, va);
	update(mid + 1, high, pos * 2 + 2, qlow, qhigh, va);
	sg[pos] = min(sg[pos * 2 + 1], sg[pos * 2 + 2]);
}
inline ll dis(ll hole, ll mice)
{
	return max(p[hole].first, m[mice]) - min(p[hole].first, m[mice]);
}
int main()
{
	scanf("%lld%lld", &n, &k);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &m[i]);
	for (int i = 1; i <= k; i++)
		scanf("%lld%lld", &x, &y), p[i] = mp(x, y);
	sort(m + 1, m + n + 1);
	sort(p + 1, p + k + 1);
	for (int i = 1; i <= n; i++)
		dp[0][i] = OO;
	for (int i = 1; i <= k; i++)
	{
		memset(lz, 0, sizeof lz);
		for (int j = 1; j <= n; j++)
			a[j] = dp[i-1][j];
		build(0, n, 0);
		for (int j = 1; j <= n; j++)
		{
			update(0, n, 0, 0, j-1, dis(i, j));
			dp[i][j] = query(0,n,0,max(0LL,j - p[i].second), j-1);
			dp[i][j] = min(dp[i][j], dp[i - 1][j]);
		}
	}
	res = dp[k][n];
	if (res >= OO)res = -1;
	printf("%lld\n", res);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
#define N 5003
#define OO 99999999999999LL
#define mp make_pair
P p[N];
ll n, k, x, y, dp[N][N], res, m[N], sg[N], a[N], lz[N];
void build(ll low, ll high, ll pos)
{
	if (low == high)
	{
		sg[pos] = a[low];
		return;
	}
	ll mid = (high + low) / 2;
	build(low, mid, pos * 2 + 1);
	build(mid + 1, high, pos * 2 + 2);
	sg[pos] = min(sg[pos * 2 + 1], sg[pos * 2 + 2]);
}
inline void lazy(ll low, ll high, ll pos)
{
	if (!lz[pos])return;
	sg[pos] += lz[pos];
	if (low != high)
		lz[pos * 2 + 1] += lz[pos], lz[pos * 2 + 2] += lz[pos];
	lz[pos] = 0;
}
ll query(ll low, ll high, ll pos, ll qlow, ll qhigh)
{
	lazy(low, high, pos);
	if (qlow > high || qhigh < low)
		return OO;
	if (qlow <= low&&high <= qhigh)
		return sg[pos];
	ll mid = (high + low) / 2;
	return min(query(low,mid, pos * 2 + 1, qlow, qhigh), query(mid+1, high , pos * 2 + 2, qlow, qhigh));
}
void update(ll low, ll high, ll pos, ll qlow, ll qhigh, ll va)
{
	lazy(low, high, pos);
	if (qlow > high || qhigh < low)
		return;
	if (qlow <= low&&high <= qhigh)
	{
		sg[pos] += va;
		if (low != high)
			lz[pos * 2 + 1] += va, lz[pos * 2 + 2] += va;
		return;
	}
	ll mid = (high + low) / 2;
	update(low, mid, pos * 2 + 1, qlow, qhigh, va);
	update(mid + 1, high, pos * 2 + 2, qlow, qhigh, va);
	sg[pos] = min(sg[pos * 2 + 1], sg[pos * 2 + 2]);
}
inline ll dis(ll hole, ll mice)
{
	return max(p[hole].first, m[mice]) - min(p[hole].first, m[mice]);
}
int main()
{
	scanf("%lld%lld", &n, &k);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &m[i]);
	for (int i = 1; i <= k; i++)
		scanf("%lld%lld", &x, &y), p[i] = mp(x, y);
	sort(m + 1, m + n + 1);
	sort(p + 1, p + k + 1);
	for (int i = 1; i <= n; i++)
		dp[0][i] = OO;
	for (int i = 1; i <= k; i++)
	{
		memset(lz, 0, sizeof lz);
		for (int j = 1; j <= n; j++)
			a[j] = dp[i-1][j];
		build(0, n, 0);
		for (int j = 1; j <= n; j++)
		{
			update(0, n, 0, 0, j-1, dis(i, j));
			dp[i][j] = query(0,n,0,max(0LL,j - p[i].second), j-1);
			dp[i][j] = min(dp[i][j], dp[i - 1][j]);
		}
	}
	res = dp[k][n];
	printf("%lld\n", res);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=11111;
const int MM=15555555;
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;
}