#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> using namespace std; #define INF 0x7fffffff int n,m,ans,a[20],rest[20]; void dfs(int x,int num) { if(num>=ans) return ; if(x==n+1) { ans=min(ans,num); return ; } for(int i=1;i<=num;i++) { if(a[x]+rest[i]<=m) { rest[i]+=a[x]; dfs(x+1,num); rest[i]-=a[x]; } } rest[num+1]+=a[x]; dfs(x+1,num+1); rest[num+1]-=a[x]; return ; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); ans=INF; dfs(1,1); printf("%d\n",ans); return 0; }
时间: 2024-10-01 08:31:40