#include<iostream> #include<queue> #include<vector> using namespace std; int N,M; long A[1<<17]; vector<int>W[1<<17]; bool ok(long c) { long B[1<<17]; priority_queue<long>P; long Ps=0; for(int i=0;i<N;i++) { while(!P.empty()&&-P.top()<=i*c) { Ps+=P.top(); P.pop(); } for(int w:W[i]) { long t=w+i*c; Ps+=t; P.push(-t); } B[i]=A[i]-(Ps-i*c*P.size()); } while(!P.empty())P.pop(); Ps=0; for(int i=N;i--;) { while(!P.empty()&&-P.top()<=(N-i-1)*c) { Ps+=P.top(); P.pop(); } B[i]-=Ps-(N-i-1)*c*P.size(); for(int w:W[i]) { long t=w+(N-i-1)*c; Ps+=t; P.push(-t); } } for(int i=0;i<N;i++)if(B[i]<=0)return false; return true; } main() { cin>>N>>M; for(int i=0;i<N;i++)cin>>A[i]; for(int i=0;i<M;i++) { int x,w;cin>>x>>w; W[x-1].push_back(w); } int L=-1,R=1e5; if(!ok(R))R=-1; while(R-L>1) { int M=(L+R)/2; if(ok(M))R=M; else L=M; } cout<<R<<endl; }