5 solutions

  • 0
    @ 2026-1-19 16:37:42
    #include<bits/stdc++.h>
    using namespace std;
    const int N=25;
    int w[N];
    int main()
    {
    	int n,m;
    	cin>>n>>m;
    	for(int i=0;i<n;i++)
    	{
    		cin>>w[i];
    	}
    	int res=0;
    	for(int i=0;i<1<<n;i++)
    	{
    		bitset<32> a(i); //将i转换成二进制存到a里面
    		int money=0;
    		for(int j=0;j<n;j++)
    		{
    			if(a[j]==1) //选了
    			{
    				money+=w[j];
    			}
    		}
    		if(money<=m)
    		{
    			res=max(res,money);
    		}
    	}
    	cout<<res;
    	return 0;
    }
    

    Information

    ID
    229
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    3
    Tags
    # Submissions
    70
    Accepted
    21
    Uploaded By