#include using namespace std; using lint = long long; const lint inf = 1e9; const lint mod = 1000000007; template bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); lint n, m; cin >> n >> m; vector a(n); lint amin = inf; for (int i = 0; i < n; ++i) { cin >> a[i]; chmin(amin, a[i]); } lint wtot = 0; vector x(m), w(m); for (int i = 0; i < m; ++i) { cin >> x[i] >> w[i]; x[i]--; wtot += w[i]; } if (amin > wtot) { cout << 0 << "\n"; return 0; } lint ok = inf; lint ng = 0; auto check = [&](lint mid) { vector imos(n, 0), imos1(n, 0); for (int i = 0; i < m; ++i) { if (mid * 2 >= w[i]) { imos1[x[i]] += w[i]; if (x[i] - 1 >= 0) imos1[x[i] - 1] += max(0LL, w[i] - mid); if (x[i] + 1 <= n - 1) imos1[x[i] + 1] += max(0LL, w[i] - mid); continue; } imos[max(0LL, (x[i] - w[i] / mid + 1))] += mid; if (x[i] + 1 <= n - 1) imos[x[i] + 1] -= 2 * mid; if (x[i] + w[i] / mid + 1 <= n - 1) imos[x[i] + w[i] / mid + 1] += mid; } for (int i = 1; i < n; ++i) { imos[i] += imos[i - 1]; } for (int i = 0; i < m; ++i) { if (mid * 2 >= w[i]) continue; if (x[i] - w[i] / mid >= 0) imos[x[i] - w[i] / mid] += w[i] % mid; else if (x[i] - w[i] / mid < 0) { imos[0] += w[i] % mid + max(abs(x[i] - w[i] / mid) - 1, 0LL) * mid; } if (x[i] + w[i] / mid + 1 <= n - 1) imos[x[i] + w[i] / mid + 1] -= w[i] % mid; } for (int i = 1; i < n; ++i) { imos[i] += imos[i - 1]; } for (int i = 0; i < n; ++i) { if (a[i] <= imos[i] + imos1[i]) return false; } return true; }; if (!check(inf)) { cout << -1 << "\n"; return 0; } while (abs(ok - ng) != 1) { lint mid = (ok + ng) / 2; (check(mid) ? ok : ng) = mid; } cout << ok << "\n"; return 0; }