#include using namespace std; typedef long long ll; typedef pair P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) templateostream& operator<<(ostream& os,const pair& a) {os << "(" << a.first << ", " << a.second << ")";return os;} struct LazySegmentTree { private: int n; vector node,lazy; public: LazySegmentTree(vector v) { int sz = v.size(); n = 1; while (n < sz) n *= 2; node.resize(2*n-1); lazy.resize(2*n-1,0); for (int i = 0;i < sz;++i) node[i+n-1] = v[i]; for (int i = n-2;i >= 0;--i) node[i] = node[i*2+1]+node[i*2+2]; } void eval(int k,int l,int r) { if (lazy[k] != 0) { node[k] += lazy[k]; if (r-l > 1) { lazy[2*k+1] += lazy[k]/2; lazy[2*k+2] += lazy[k]/2; } lazy[k] = 0; } } void add(int a,int b,ll x,int k = 0,int l = 0,int r = -1) { if (r < 0) r = n; eval(k,l,r); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazy[k] += (r-l)*x; eval(k,l,r); } else { add(a,b,x,2*k+1,l,(l+r)/2); add(a,b,x,2*k+2,(l+r)/2,r); node[k] = node[2*k+1]+node[2*k+2]; } } ll getsum(int a,int b,int k = 0,int l = 0,int r = -1) { if (r < 0) r = n; if (b <= l || r <= a) return 0; eval(k,l,r); if (a <= l && r <= b) return node[k]; ll vl = getsum(a,b,k*2+1,l,(l+r)/2); ll vr = getsum(a,b,2*k+2,(l+r)/2,r); return vl+vr; } }; int main() { int n,m; cin >> n >> m; vector a(n),w(m),sum(n,0); vector x(m); for (int i = 0;i < n;++i) scanf("%lld",&a[i]); for (int i = 0;i < m;++i) scanf("%d%lld",&x[i],&w[i]),x[i]--; for (int i = 0;i < m;++i) sum[x[i]] += w[i]; for (int i = 0;i < n;++i) if (a[i] < sum[i]) { cout << -1 << endl; return 0; } ll s = 0; for (int i = 0;i < m;++i) s += w[i]; bool f = true; for (int i = 0;i < n;++i) if (a[i] < s) {f = false;break;} if (f) { cout << 0 << endl; return 0; } ll mn = 0,mx = 1e9; while (mx-mn > 1) { int mid = (mn+mx)/2; LazySegmentTree seg1(vector(n,0)),seg2(vector(n,0)),seg3(vector(n,0)); for (int i = 0;i < m;++i) { int c = w[i]/mid; seg1.add(x[i]-c,x[i]+c+1,w[i]); seg2.add(x[i]+1,x[i]+1+c,mid); seg2.add(x[i]+1+c,x[i]+2+c,-(ll)c*mid); seg3.add(x[i]-c,x[i],mid); seg3.add(x[i]-c-1,x[i]-c,-(ll)c*mid); } bool ok = true; for (int i = 0;i < n;++i) if (seg1.getsum(i,i+1)-seg2.getsum(0,i+1)-seg3.getsum(i,n) > a[i]) { ok = false; break; } (ok ? mx : mn) = mid; } cout << mx << endl; }