fork(3) download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. int M, N, p;
  5. int memo[5000];
  6.  
  7.  
  8. int solve()
  9. {
  10.  
  11. cin >> M >> N;
  12. int i, j, jmax = 0;
  13.  
  14. memset(memo, 0, sizeof(memo));
  15. memo[0] = 1;
  16. for(i = 0; i < N; i++) {
  17. cin >> p;
  18. for(j=M; j>=p; j--){
  19. if(memo[j-p]) {
  20. memo[j]=1;
  21. if(j>jmax)
  22. jmax = j;
  23. }
  24. }
  25. }
  26. return jmax;
  27. }
  28.  
  29. int main(){
  30. std::cout<<solve();
  31. return 0;
  32. }
  33.  
  34.  
Success #stdin #stdout 0s 4636KB
stdin
9
4
2
3
5
11
stdout
8