#define _GLIBCXX_DEBUG #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; template void chmax(T &x, T y) {if(x void chmin(T &x, T y) {if(x>y) x=y;} main() { ios::sync_with_stdio(false), cin.tie(nullptr); int n,m; cin>>n>>m; vector a(n); for(int &e:a) cin>>e; vector> h(m); for(auto &e:h) { int x,w; cin>>x>>w; --x; e={x,w}; } // binary search long long ok=1e5+1; long long ng=0; while(ok-ng>1) { long long c=(ok+ng)/2; vector b(n+1); for(auto &e:h) { int x,w; tie(x,w)=e; const int l=max(0LL,x-w/c); const int r=x+w/c+1; b[l+1]+=c; b[x+1]-=c*2; if(r<=n) b[r]+=c; } for(int i=0;i=a[i]) { fail=1; } } if(fail) { ng=c; } else { ok=c; } } if(ng) { if(ok>1e5) cout << -1 << "\n"; else cout << ok << "\n"; } else { long long ws=0; for(auto &e:h) { ws+=e.second; } if(ws>=*min_element(begin(a),end(a))) { cout << 1 << "\n"; } else { cout << 0 << "\n"; } } }