#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair P; int main() { int n, m; cin>>n>>m; ll a[100010]; for(int i=0; i>a[i]; int x[100010]; ll w[100010]; for(int i=0; i>x[i]>>w[i], x[i]--; ll s0=0; for(int i=0; i=a[i]){ ok=0; break; } } if(ok){ cout<<0<=a[0]) return false; ll s1[100010]={}, s2[100010]={}, d[100010]={}, e[100010]={}, f[100010]={}; for(int i=0; i=0){ ll t=x[i]-w[i]/c; s1[t]+=w[i]-abs(t-x[i])*c; d[t]++; } } for(int i=0; i=a[i]) return false; cnt1+=d[i]; cnt1-=e[i], cnt2+=e[i]; cnt2-=f[i]; s-=s2[i]; } return true; }; while(r-l>1){ ll mid=(l+r)/2; if(check(mid)) r=mid; else l=mid; } if(r==INF) cout<<-1<