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;
long long N;
long long l[5555];
long long r[5555];
long long dp[2][11111];
long long idx[2][11111];
long long minDp[2][11111];
bool in(long long x, vector<int> &w) { return (0 <= x && x < w.size()); }
bool in(long long x, long long l, long long r) { return (l <= x && x < r); }
long long dist(long long x, long long y) { return abs(x - y); }
void getMin(long long i) {
  minDp[i][0] = dp[i][0];
  for (long long j = 1; j < N; j++) {
    minDp[i][j] = min(minDp[i][j - 1], dp[i][j]);
  }
}
int main(void) {
  cin.tie(0);
  cin.sync_with_stdio(0);
  long long n, m;
  cin >> n >> m;
  vector<long long> x(n);
  for (long long i = 0; i < (long long)(n); i++) {
    cin >> x[i];
  }
  vector<pair<long long, long long> > v(m);
  for (long long i = 0; i < (long long)(m); i++) {
    cin >> v[i].first >> v[i].second;
  }
  sort(x.begin(), x.end());
  sort(v.begin(), v.end());
  vector<int> w;
  for (long long i = 0; i < (long long)(m); i++) {
    for (long long j = 0; j < (long long)(v[i].second); j++)
      w.push_back(v[i].first);
  }
  if (x.size() > w.size()) {
    cout << -1 << '\n';
    return 0;
  }
  for (long long i = 0; i < (long long)(n); i++) {
    if (x[i] < w.front()) {
      l[i] = -1;
      r[i] = 0;
      continue;
    }
    if (x[i] > w.back()) {
      l[i] = w.size() - 1;
      r[i] = -1;
      continue;
    }
    long long L = 0;
    long long R = w.size() - 1;
    while (L != R) {
      long long mid = (L + R + 1) >> 1;
      if (w[mid] <= x[i])
        L = mid;
      else
        R = mid - 1;
    }
    l[i] = L;
    r[i] = L + 1;
  }
  N = 2 * n + 1;
  for (long long j = 0; j < (long long)(N); j++) {
    long long k = l[0] - (n - j);
    idx[0][j] = k;
    if (!in(k, w)) {
      dp[0][j] = 1e18;
      continue;
    }
    dp[0][j] = dist(x[0], w[k]);
  }
  getMin(0);
  for (long long I = 1; I < n; I++) {
    long long i = I & 1;
    for (long long j = 0; j < (long long)(N); j++) {
      long long k = l[I] - (n - j);
      idx[i][j] = k;
      dp[i][j] = 1e18;
    }
    for (long long j = idx[!i][0] + 1 - l[I] + n, jdx = 0; j < N; j++, jdx++) {
      if (j < 0 || jdx < 0) continue;
      long long k = l[I] - (n - j);
      if (!in(k, w)) continue;
      dp[i][j] = dist(x[I], w[k]);
      dp[i][j] += minDp[!i][min(jdx, N - 1)];
    }
    getMin(i);
  }
  cout << minDp[(n - 1) & 1][N - 1] << '\n';
  return 0;
}