fork download
  1. #include <cstdio>
  2. using namespace std;
  3.  
  4. short d[210][110];
  5. short arr[110];
  6.  
  7. short max (short a, short b) {
  8. return a<b ? b : a;
  9. }
  10. int main() {
  11. int n;
  12. while (scanf("%d", &n)>0){
  13. int k;
  14. scanf("%d", &k);
  15. for (int i=1; i<=k; i++) scanf("%d", &arr[i]);
  16. for (int i=1; i<=k; i++)
  17. for (int j=0; j<=n; j++)
  18. if (arr[i]<=j) d[j][i]=max(d[j][i-1], arr[i]+d[j-arr[i]][i-1]);
  19. else d[j][i]=d[j][i-1];
  20. printf("sum:%d\n", d[n][k]);
  21. }
  22. return 0;
  23. }
Success #stdin #stdout 0s 3144KB
stdin
5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2
stdout
sum:5
sum:10
sum:19
sum:55
sum:45