1 条题解

  • 0
    @ 2024-12-24 10:06:16

    C++ :

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N = 1005;
    struct food{
    	int w,v;
    };
    food a[N];
    
    bool comp(food x,food y){
    	return x.v>y.v;
    }
    
    int main(){
    	int M,n,ans=0;
    	cin>>M>>n;
    	for (int i=1; i<=n; i++) cin>>a[i].w>>a[i].v;
    	sort(a+1,a+n+1,comp);
    	for (int i=1; i<=n&&M>0; i++)
    		if (M>=a[i].w) ans+=a[i].v*a[i].w,M-=a[i].w;
    		else ans+=a[i].v*M,M=0;
    	cout<<ans<<endl;
    	return 0;
    }
    
    • 1

    信息

    ID
    2573
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者