#include #include #include #include using namespace std; int N,D; main() { cin>>N>>D; vector > >PQ(N); for(int i=0;i>p>>q; PQ[i]=make_pair(p,make_pair(p-q,i)); } sort(PQ.begin(),PQ.end()); int L=-1e9,R=1; while(R-L>1) { int mid=(L+R)/2; int now=0; int id=0; multiset >S; int prv=-1; bool ok=true; for(int d=0;d0&&PQ[id-1].first>now-mid) { id--; S.erase(S.find(PQ[id].second)); } if(S.empty()) { ok=false; break; } auto it=S.begin(); if(it->second!=prv) { now-=it->first; prv=it->second; } else { it++; if(it==S.end()) { ok=false; break; } now-=it->first; prv=it->second; } } if(ok)L=mid; else R=mid; } cout<