#include #include #include using namespace std; //Minimum Cost Flow O(FE log V) //Minimum Cost Flow with negative cost O(NE+FE log V) #include #include #include #include #include #include template struct MCF{ struct edge{ int to,rev,cap; T cost; }; int n; bool negedge,ok; vector >G; vectorh,d; vectorpv,pe; MCF(int n_=0):n(n_),negedge(false),G(n_),h(n_),d(n_),pv(n_),pe(n_){} void add_edge(int from,int to,int cap,T cost) { if(cost<0)negedge=true; G[from].push_back({to,(int)G[to].size(),cap,cost}); G[to].push_back({from,(int)G[from].size()-1,0,-cost}); } T min_cost_flow(int s,int t,int f)//ans or -1 { ok=false; if(negedge) { fill(h.begin(),h.end(),numeric_limits::max()); h[s]=0; for(int i=0;i::max()) { for(const edge&e:G[i]) { if(e.cap>0&&h[e.to]>h[i]+e.cost) { h[e.to]=h[i]+e.cost; } } } } T ret=0; while(f>0) { priority_queue,vector >,greater > >P; fill(d.begin(),d.end(),numeric_limits::max()); d[s]=0; P.push(make_pair(0,s)); while(!P.empty()) { pairp=P.top();P.pop(); if(d[p.second]0&&d[e.to]>d[p.second]+e.cost+h[p.second]-h[e.to]) { d[e.to]=d[p.second]+e.cost+h[p.second]-h[e.to]; pv[e.to]=p.second; pe[e.to]=i; P.push(make_pair(d[e.to],e.to)); } } } if(d[t]==numeric_limits::max())return -1; for(int u=0;u>N>>K; MCFP(N+1); for(int i=1;i<=N;i++) { int M; P.add_edge(i-1,i,K,0); cin>>A[i]>>M; for(int j=0;j>u; P.add_edge(u,i,1,A[u]-A[i]); } } cout<<-P.min_cost_flow(0,N,K)<