#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 25;
const int MAXC = 10005;
const int INF = 1<<30;
int A[MAXN];
int n, c;
int d[MAXN][MAXC];
int s[MAXN][MAXC];
void print1(int i, int j)
{
if(i==n+1) return;
if(s[i][j]) {
printf("%d ", A[i]);
print1(i+1, j-A[i]);
} else {
print1(i+1, j);
}
}
void dp1()
{
for(int i=n; i>=1; i--) {
for(int j=0; j<=c; j++) {
d[i][j] = d[i+1][j];
s[i][j] = 0;
if(j >= A[i] && d[i][j] < d[i+1][j-A[i]] + A[i]) {
d[i][j] = d[i+1][j-A[i]] + A[i];
s[i][j] = 1;
}
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while(scanf("%d%d", &c, &n) == 2) {
for(int i=1; i<=n; i++) {
scanf("%d", &A[i]);
}
memset(d, 0, sizeof(d));
dp1();
print1(1, c);
printf("sum:%d\n", d[1][c]);
}
}
UVa 624 - CD
最新推荐文章于 2018-04-02 21:08:29 发布