c++代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
const int M = 110;
int n, m;
bool f[N][M];
int w[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
sort(w + 1, w + n + 1, greater <int> ());
f[0][0] = true;
for (int i = 1; i <= n; i ++ )
{
for (int j = 0; j <= m; j ++ )
{
f[i][j] = f[i - 1][j];
if (j >= w[i]) f[i][j] = max(f[i][j], f[i - 1][j - w[i]]);
}
}
if (!f[n][m]) puts("No Solution");
else
{
while (n)
{
if (m && f[n - 1][m - w[n]])
{
cout << w[n] << ' ' ;
m -= w[n];
}
n -- ;
}
}
return 0;
}·