#include #include using namespace std; using ll = long long; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector a(N); for (int i = 0; i < N; i++) cin >> a[i]; vector x(M), w(M); int maxw = 0; for (int j = 0; j < M; j++) { cin >> x[j] >> w[j]; x[j]--; maxw = (maxw, w[j]); } int left = 0, right = maxw+1; while (left < right) { int c = (left + right) / 2; vector ft(N); for (int j = 0; j < M; j++) { int d = c ? w[j] / c : N; ft[max(0, x[j] - d)] += w[j]; if (x[j] + d + 1 < N) ft[x[j] + d + 1] -= w[j]; } for (int i = 1; i < N; i++) ft[i] += ft[i-1]; vector cd(N); for (int j = 0; j < M; j++) { int d = c ? w[j] / c : N; if (x[j] + 1 < N) { cd[max(1, x[j] - d + 1)] += c; cd[x[j] + 1] -= c; } if (x[j] + 1 < N) cd[x[j] + 1] -= c; if (x[j] + d + 1 < N) cd[x[j] + d + 1] += c; } for (int i = 1; i < N; i++) cd[i] += cd[i-1]; for (int j = 0; j < M; j++) { int d = c ? w[j] / c : N; if (x[j] - d < 0) cd[0] -= x[j] * c; else cd[x[j] - d] -= d * c; if (x[j] + d + 1 < N) cd[x[j] + d + 1] += d * c; } for (int i = 1; i < N; i++) cd[i] += cd[i-1]; bool flag = false; for (int i = 0; i < N; i++) { if (a[i] <= ft[i] + cd[i]) { flag = true; break; } } if (flag) left = c + 1; else right = c; } cout << (left < maxw+1 ? left : -1) << "\n"; }