#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #include<algorithm> #define N 1010 using namespace std; int dp[N], path[N][N], w[N]; int main() { int v, n; while(~scanf("%d", &v)) { scanf("%d", &n); memset(dp, 0, sizeof(dp)); memset(path, 0, sizeof(path)); for(int i = 1 ; i <= n ;i++) scanf("%d", &w[i]); for(int i = n ; i >= 0 ; i--) { for(int j = v ; j >= w[i] ; j--) { if(dp[j] <= dp[j - w[i]] + w[i]) { dp[j] = dp[j - w[i]] + w[i]; path[i][j] = 1; }//path记录 } } int j = v; for(int i = 1 ; i <= n ; i++) { if(path[i][j]) { printf("%d ", w[i]); j -= w[i]; } }//打印路径 printf("sum:%d\n", dp[v]); } return 0; }
时间: 2024-11-05 20:25:42