枚举最远跳到哪栋楼,这栋楼前面的所有楼里面选最大的几个加上
#include<bits/stdc++.h>
using namespace std;
long long h[1000100],v[1000010];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
long long s[1000100];
int main()
{
s[0]=0;
long long n,m,ans=0,sum=0;
cin>>n>>m;m++;
for(long long i=1;i<=n;i++)
{
cin>>h[i]>>v[i];
s[i]=s[i-1]+v[i];
}
for(long long i=1;i<=n;i++)
{
m-=h[i];
q.push(make_pair(v[i],h[i]-1));
sum+=v[i]*h[i];
while(!q.empty()&&m<0)
{
long long x=q.top().first,y=q.top().second;
q.pop();
if(y<=-m)
{
sum-=x*y;
m+=y;
}
else
{
q.push(make_pair(x,y+m));
sum+=x*m;
m=0;
}
}
ans=max(ans,sum);
}
cout<<ans;
return 0;
}