#include #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair P; #define yn {puts("YES");}else{puts("NO");} #define MAX_N 200005 int main() { int n, m; cin >> n >> m; ll a[n]; rep(i,n)scanf("%lld", &a[i]); ll x[m], w[m]; rep(i,m){ scanf("%lld%lld", &x[i], &w[i]); x[i]--; } ll mi = 1001001001; ll ma = 0; ll ww[n] = {}; rep(i,m){ ww[x[i]] += w[i]; } rep(i,n){ if(ww[i] >= a[i]){ cout << "-1" << endl; return 0; } } rep(i,m){ if(w[i] >= a[x[i]]){ cout << "-1" << endl; return 0; } ma += w[i]; } rep(i,n)mi = min(mi, a[i]); if(mi > ma){ cout << 0 << endl; return 0; } ll l = 0; ll r = 1001001001; while(l + 1 < r){ ll c = (l + r) / 2; priority_queue, greater

> que1, que2, que3; rep(i,m){ ll d = w[i] / c; P p; p.first = max(x[i] - d, (ll)0); p.second = i; que1.push(p); p.first = x[i]; que2.push(p); p.first = x[i] + d; que3.push(p); } int flag = 0; ll cnt1 = 0; ll cnt2 = 0; ll now = 0; rep(i,n){ now += c * cnt1; now -= c * cnt2; while(que1.size() && que1.top().first <= i){ P p = que1.top(); que1.pop(); now += w[p.second]; now -= c * (x[p.second] - (ll)i); cnt1++; } while(que2.size() && que2.top().first <= i){ cnt1--; cnt2++; que2.pop(); } if(now >= a[i]){ flag = 1; break; } while(que3.size() && que3.top().first <= i){ P p = que3.top(); que3.pop(); now -= w[p.second]; now += c * ((ll)i - x[p.second]); cnt2--; } } if(flag == 0){ r = c; }else{ l = c; } } cout << r << endl; return 0; }