题解:
按照单价排序
然后贪心
代码:
#include<bits/stdc++.h> using namespace std; const int N=100005; typedef long long ll; ll ans,m,a[N],b[N]; int n,f[N]; int cmp(int x,int y) { return b[x]<b[y]; } int main() { scanf("%d%lld",&n,&m); for (int i=1;i<=n;i++)scanf("%lld%lld",&b[i],&a[i]),f[i]=i; sort(f+1,f+n+1,cmp); for (int i=1;i<=n;i++) { ll k=min(a[f[i]],m/b[f[i]]); m-=k*b[f[i]]; ans+=k; } printf("%lld",ans); }