dp[i][j]表示i可以构成j则为true:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 1000 + 7;
int n, k;
bool dp[maxn][maxn];
int main() {
scanf("%d%d", &n, &k);
memset(dp, 0, sizeof(dp));
dp[0][0] = true;
while(n--) {
int t; scanf("%d", &t);
for(int i = k; i >= 0; --i) {
for(int j = 0; j <= k; ++j) {
if(dp[i][j]) dp[i+t][j] = dp[i+t][j+t] = true;
}
}
}
int j = 0, a[maxn];
for(int i = 0; i <= k; ++i) if(dp[k][i]) a[j++] = i;
printf("%d\n", j);
for(int i = 0; i < j; ++i) {
if(!i) printf("%d", a[i]);
else printf(" %d", a[i]);
}
printf("\n");
}