#include using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define range(a) a.begin(), a.end() int main() { ll N, M; cin >> N >> M; vector A(N); rep(i, N) cin >> A[i]; vector X(M), W(M); rep(i, M) cin >> X[i] >> W[i], X[i]--; ll total = 0; rep(i, M) total += W[i]; if (*min_element(range(A)) >= total) { cout << 0 << endl; return 0; } ll l = 0; ll r = 1e9; while (r - l > 1) { ll c = (l + r) / 2; vector sa(N + 1); vector sb(N + 1); auto linear = [&](ll l, ll r, ll a, ll b) { // ax + b sa[l] += a; sa[r] -= a; sb[l] += b; sb[r] -= b; }; rep(j, M) { // -w[j] / c <= di <= w[j]/c ll L = max(0LL, X[j] - W[j] / c); ll R = min(N - 1, X[j] + W[j] / c); // 左側では W[j] + (i - X[j]) * C // 右側では W[j] - (i - X[j]) * C linear(L, X[j], c, W[j] - X[j] * c); linear(X[j], R + 1, -c, W[j] + X[j] * c); } bool ok = true; rep(i, N) { sa[i + 1] += sa[i]; sb[i + 1] += sb[i]; } rep(i, N) { ok &= sa[i] * i + sb[i] <= A[i]; } if (ok) r = c; else l = c; } if (r == 1e9) { cout << -1 << endl; } else { cout << r << endl; } }