#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(N), w(N); 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; if (c == 0) { ll sumw = 0; for (int j = 0; j < M; j++) sumw += w[j]; bool flag = false; for (int i = 0; i < N; i++) { if (a[i] <= sumw) { flag = true; break; } } if (flag) left = c + 1; else right = c; continue; } vector ft(N); for (int j = 0; j < M; j++) { ft[max(0, x[j] - w[j]/c)] += w[j]; if (x[j] + w[j]/c + 1 < N) ft[x[j] + w[j]/c + 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++) { cd[max(0, x[j] - w[j]/c)] += c; cd[x[j]] -= c; if (x[j] + 1 < N) cd[x[j] + 1] -= c; if (x[j] + w[j]/c + 1 < N) cd[x[j] + w[j]/c + 1] += c; } for (int i = 1; i < N; i++) cd[i] += cd[i-1]; for (int j = 0; j < M; j++) { if (w[j]/c == 0) continue; if (x[j] - w[j]/c < 0) cd[0] -= x[j] ? (x[j] + 1) * c : 0; else cd[x[j] - w[j]/c] -= (w[j]/c + 1) * c; if (x[j] + w[j]/c + 1 < N) cd[x[j] + w[j]/c + 1] += w[j]/c * 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"; }