Problem: One day Masha came home and noticed n mice in the corridor of her flat. Of course, she shouted loudly, so scared mice started to run to the holes in the corridor.

The corridor can be represeted as a numeric axis with n mice and m holes on it. ith mouse is at the coordinate xi, and jth hole — at coordinate pj. jth hole has enough room for cj mice, so not more than cj mice can enter this hole.

What is the minimum sum of distances that mice have to go through so that they all can hide in the holes? If ith mouse goes to the hole j, then its distance is |xi - pj|.

Print the minimum sum of distances.

Input

The first line contains two integer numbers n, m (1 ≤ n, m ≤ 5000) — the number of mice and the number of holes, respectively.

The second line contains n integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109), where xi is the coordinate of ith mouse.

Next m lines contain pairs of integer numbers pj, cj ( - 109 ≤ pj ≤ 109, 1 ≤ cj ≤ 5000), where pj is the coordinate of jth hole, and cj is the maximum number of mice that can hide in the hole j.

Output

Print one integer number — the minimum sum of distances. If there is no solution, print -1 instead.

Examples

Input

4 5
6 2 8 9
3 6
2 1
3 6
4 7
4 7


Output

11


Input

7 2
10 20 30 40 50 45 35
-1000000000 10
1000000000 1


Output

7000000130

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 7;
const long long inf = 1e15;
bool flag = 1;
int n, m, tot = 0;
long long w, sumc = 0, ans = 0;
struct hole {
  int c, flag;
  long long p;
} o[N];
struct xd {
  int i;
  long long z;
  bool operator<(const xd &a) const { return z > a.z; }
} tmp, nw;
priority_queue<xd> q;
priority_queue<long long, vector<long long>, greater<long long> > q2;
inline int read() {
  int T = 0, F = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') F = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    T = (T << 3) + (T << 1) + (ch - 48), ch = getchar();
  return F * T;
}
bool cmp(hole u, hole v) { return u.p < v.p; }
int main() {
  n = read(), m = read();
  for (register int i = 1; i <= n; ++i)
    ++tot, o[tot].p = read(), o[tot].flag = 0;
  for (register int i = 1; i <= m; ++i) {
    ++tot, o[tot].p = read(), o[tot].c = read(), o[tot].flag = 1,
           sumc += o[tot].c;
    if (o[tot].c != n) flag = 0;
  }
  if (sumc < n) {
    printf("-1\n");
    return 0;
  }
  if (flag) {
    tot = n, sort(o + 1, o + n + 1, cmp), sort(o + n + 1, o + n + m + 1, cmp);
    for (register int i = 1; i <= n; ++i) {
      while (o[i].p >= o[tot + 1].p && tot < n + m) ++tot;
      if (tot >= n + m)
        ans += o[i].p - o[tot].p;
      else if (tot <= n)
        ans += o[tot + 1].p - o[i].p;
      else
        ans += min(o[i].p - o[tot].p, o[tot + 1].p - o[i].p);
    }
    printf("%lld\n", ans);
    return 0;
  }
  sort(o + 1, o + tot + 1, cmp);
  for (register int i = 1; i <= tot; ++i) {
    if (!o[i].flag) {
      w = inf;
      if (!q.empty()) {
        tmp = q.top(), w = o[i].p + tmp.z, q.pop();
        if (o[tmp.i].c) --o[tmp.i].c, q.push(tmp);
      }
      ans += w, q2.push(-o[i].p - w);
    } else {
      while (o[i].c && !q2.empty() && o[i].p + q2.top() < 0)
        --o[i].c, w = o[i].p + q2.top(), q2.pop(), ans += w,
                  tmp.z = -o[i].p - w, tmp.i = 0, q.push(tmp);
      if (o[i].c) --o[i].c, tmp.z = -o[i].p, tmp.i = i, q.push(tmp);
    }
  }
  printf("%lld\n", ans);
  return 0;
}