题目
#include<bits/stdc++.h>
using namespace std;
int car[20],mem[20],n,w,ans;
bool cmp (int a,int b)
{
return a>b;
}
void dfs(int now,int cnt)
{
//cout<<now<<" "<<ans<<" "<<cnt<<endl;
if(cnt >= ans ) return ;
if(now == n+1)
{
ans=min ( ans , cnt );
//
return ;
}
for(int i = 1 ; i <= cnt; i++)
{
if(car[i] + mem [ now] <= w)
car[i]+=mem [ now];
dfs( now+1,cnt );
car[i]-=mem[now];
}
car[cnt+1]=mem[now];
dfs( now+1,cnt+1);
car[cnt+1]=0;
}
int main()
{
cin>>n>>w;
for(int i=1;i<=n;i++)
cin>>mem[i];
sort(mem+1,mem+n+1,cmp);
ans=n;
dfs(1,0);
//cout<<ans;
//
return 0;
}