#define _USE_MATH_DEFINES #include using namespace std; constexpr long long INF = 1LL << 60; constexpr int geta = 2e5; constexpr int N = 505050, M = 101010; long long a[N], x[M], w[M]; long long imos[N]; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; fill(a, a + N, INF); for (int i = 1; i <= n; i++) cin >> a[i + geta]; long long sum = 0; for (int i = 0; i < m; i++) { cin >> x[i] >> w[i]; x[i] += geta; sum += w[i]; } bool zero = true; for (int i = 0; i < N; i++) { if (a[i] <= sum) zero = false; } if (zero) { cout << 0 << endl; return 0; } auto judge = [&] (long long d) { for (int i = 0; i < N; i++) imos[i] = 0; for (int i = 0; i < m; i++) { long long pos_fi = x[i] - w[i] / d; long long pos_la = x[i]; if (pos_fi < pos_la) { imos[pos_fi + 1] += d; } if (pos_fi < pos_la) { imos[pos_la + 1] -= d; } } for (int i = 0; i < m; i++) { long long fi = max(0LL, w[i] - d); long long pos_fi = x[i] + 1; long long pos_la = x[i] + 1 + fi / d; if (pos_fi < pos_la) { imos[pos_fi + 1] += -d; } if (pos_fi < pos_la) { imos[pos_la + 1] -= -d; } } // cerr << d << endl; // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " "; // cerr << endl; for (int i = 1; i < N; i++) imos[i] += imos[i - 1]; // cerr << d << endl; // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " "; // cerr << endl; for (int i = 0; i < m; i++) { long long fi = w[i] % d; long long pos_fi = x[i] - w[i] / d; long long pos_la = x[i]; imos[pos_fi] += fi; imos[pos_la + 1] -= fi; if (pos_fi < pos_la) { imos[pos_la + 1] -= d * (pos_la - pos_fi); } } // cerr << d << endl; // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " "; // cerr << endl; for (int i = 0; i < m; i++) { long long fi = max(0LL, w[i] - d); long long pos_fi = x[i] + 1; long long pos_la = x[i] + 1 + fi / d; imos[pos_fi] += fi; imos[pos_la + 1] -= fi; if (pos_fi < pos_la) { imos[pos_la + 1] -= -d * (pos_la - pos_fi); } } // cerr << d << endl; // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " "; // cerr << endl; for (int i = 1; i < N; i++) imos[i] += imos[i - 1]; // cerr << d << endl; // for (int i = 0; i <= n; i++) cerr << imos[i + geta] << " "; // cerr << endl; for (int i = 0; i < N; i++) { if (a[i] <= imos[i]) return false; } return true; }; long long lo = 0, hi = 2e5; while (hi - lo > 1) { long long mid = (lo + hi) >> 1; if (judge(mid)) hi = mid; else lo = mid; } cout << (hi == 2e5 ? -1 : hi) << endl; return 0; }