小猫爬山
作者:
木可柯
,
2024-05-29 15:27:36
,
所有人可见
,
阅读 8
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 20;
int n,m;
int a[N],sum[N];
int ans = 2e9;
bool cmp(int a, int b)
{
return a > b;
}
void dfs(int u, int res)
{
if(res >= ans) return;
if(u > n) {
ans = res;
return;
}
for(int i = 1; i <= res; i++) {
if(sum[i] + a[u] <= m) {
sum[i] += a[u];
dfs(u + 1, res);
sum[i] -= a[u];
}
}
sum[res + 1] += a[u];
dfs(u + 1, res + 1);
sum[res + 1] -= a[u];
}
int main () {
cin >> n >> m;
for (int i = 1;i <= n;i++) cin >> a[i];
sort (a + 1,a + 1 + n, cmp);
dfs (1, 1);
cout << ans << endl;
return 0;
}