#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 v(N), w(M); for (int i = 0; i < M; i++) { int x; cin >> x >> w[i]; x--; v[x]++; } ll sow = 0; for (int i = 0; i < M; i++) sow += w[i]; for (int i = 0; i < N; i++) a[i] = max(0LL, sow - a[i]); ll left = 0, right = 0, col = 0, cor = 0; for (int i = 1; i < N; i++) { cor += v[i]; right += v[i] * i; } ll ans = 0; if (a[0] != 0) { if (right == 0) { cout << -1 << "\n"; return 0; } ans = (a[0]+right-1) / right; } for (int i = 1; i < N; i++) { col += v[i-1]; left += col; right -= cor; cor -= v[i]; if (a[i] == 0) continue; if (left + right == 0) { cout << -1 << "\n"; return 0; } ans = max(ans, (a[i]+left+right-1) / (left+right)); } cout << ans << "\n"; }