妥妥的贪心,将花费升序排列,直接模拟即可!
注意要开long long
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
long long n,r,v;
struct tw{
long long s;
long long r;
}a[100100];
int comp(const tw&x,const tw&y)
{
return x.r<y.r;
}
int main()
{
freopen("exam.in","r",stdin);
freopen("exam.out","w",stdout);
scanf("%lld%lld%lld",&n,&r,&v);
long long tot=0;
long long ans=0;
v*=n;
for(int i=1;i<=n;i++)
scanf("%lld%lld",&a[i].s,&a[i].r),tot+=a[i].s;
tot=v-tot;
sort(a+1,a+n+1,comp);
long long i=1;
while(1)
{
if(a[i].s<r)
{
int tmp=r-a[i].s;
if(tot>tmp)
{
ans+=(a[i].r*tmp);
tot-=tmp;
i++;
continue;
}
if(tot==tmp)
{
ans+=a[i].r*tmp;
break;
}
if(tot<tmp)
{
ans+=tot*a[i].r;
break;
}
}
else i++;
}
printf("%lld",ans);
return 0;
}