#include #include using namespace std; const int N=2e5+10; int n,k; long long dp[N]; struct Work { int t,d; }a[N]; bool check(int x) { int lst=-1e9; for(int i=1;i<=n;i++) { if(a[i].d<=x) continue; if(a[i].t-lst>1; if(check(mid)) ans=mid,r=mid-1; else l=mid+1; } printf("%d\n",ans); for(int i=2;i<=n+1;i++) dp[i]=-1e9; long long sum=0; for(int i=1,p=0;i<=n;i++) { while(a[i].t-a[p].t>=k) p++; if(!p||a[i].t-a[p-1].t>=k) dp[i+1]=max(dp[i+1],dp[p]+a[i].d); if(a[i].d<=ans) dp[i+1]=max(dp[i+1],dp[i]); else p=i+1; sum+=a[i].d; } printf("%lld",sum-dp[n+1]); return 0; }