#include using namespace std; using ll = long long; using ld = long double; using vl = vector; template using vc = vector; template using vvc = vector>; const ll MOD = 1e9 + 7; const ll INF = 1e16; const ld EPS = 1e-11; const ld PI = acos(-1.0L); #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (b>N>>M; vl A(N); rep(i, N) cin >> A[i]; vc> pp(M); rep(i,M){ ll x, w; cin >> x >> w; x--; pp[i] = {x, w}; } sort(all(pp)); //c=0でokなら返す ll sum = 0; rep(i,M){ sum += pp[i].second; } bool ok0 = true; rep(i,N){ if(sum>=A[i]){ ok0 = false; break; } } if(ok0){ cout << 0 << endl; return 0; } //二分探索 ll ng = 0, ok = 1e9; while(ok-ng>1){ bool isok = true; ll mid = (ok + ng) / 2; vvc> add(N), del(N); for(auto p:pp){ ll x, w; tie(x, w) = p; ll r = w / mid; add[max(0ll, x - r)].eb(x,w); del[min(N - 1, x + r)].eb(x,w); } // cout << "add" << endl; // rep(i,N){ // cout << i << endl; // for (auto p : add[i]) cout << p.first << " " << p.second << " "; // cout << endl; // } // cout << "del" << endl; // rep(i,N){ // cout << i << endl; // for (auto p : del[i]) cout << p.first << " " << p.second << " "; // cout << endl<=A[i]){ isok = false; break; } for(auto p:del[i]){ ll x, w; tie(x, w) = p; cnt--; val -= w - (i - x) * mid; } } (isok ? ok : ng) = mid; } if(ok==1e9){ cout << -1 << endl; return 0; } cout << ok << endl; }