/** * @FileName a.cpp * @Author kanpurin * @Created 2020.06.20 19:28:53 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; int main() { int n,m;cin >> n >> m; vector a(n); ll sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } vector b(n); for (int i = 0; i < m; i++) { int x,w;cin >> x >> w; x--; sum += w; b[0] += min(x,w); if (n > 1) b[1] -= min(x,w); if (x <= w && n > 1) { b[1] -= 1; } else if (x - w + 1 >= 0 && x - w + 1 < n) { b[x-w+1] -= 1; } if (x + 1 < n) b[x+1] += 2; if (x + w + 1 < n) b[x + w + 1] -= 1; } for (int i = 0; i < n - 1; i++) { b[i + 1] += b[i]; } for (int i = 0; i < n - 1; i++) { b[i + 1] += b[i]; } ll l = -1, r = 10000000001; while(r - l > 1) { ll mid = l + (r - l) / 2; bool ok = true; for (int i = 0; i < n; i++) { if (a[i] <= sum - mid * b[i]) { ok = false; break; } } if (ok) { r = mid; } else { l = mid; } } if (r == 10000000001) { cout << -1 << endl; } else { cout << r << endl; } return 0; }