题目链接
代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 20;
int cat[N], car[N];
int ans, n, m;
bool cmp(int a, int b)
{
return a > b;
}
void Dfs(int index, int sum)
{
if (sum >= ans) return;
if (index == n)
{
ans = min(ans, sum);
return;
}
for (int i = 1; i <= sum; i++)
{
if (car[i] + cat[index] <= m)
{
car[i] += cat[index];
Dfs(index + 1, sum);
car[i] -= cat[index];
}
}
car[sum + 1] = cat[index];
Dfs(index + 1, sum + 1);
car[sum + 1] = 0;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> cat[i];
sort(cat, cat + n, cmp);
ans = n;
Dfs(0, 0);
cout << ans << endl;
return 0;
}