fork(15) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int w,n(0),f,s,max_c(0),max_w(0);
  8.  
  9. int main()
  10. {
  11. cin >> w;
  12. //cin >> n;
  13. vector<int>weight,cost;
  14. while(cin >> f >> s){
  15. ++n;
  16. weight.push_back(f);
  17. cost.push_back(s);
  18. }
  19. //for(int i = 0;i<n;++i){
  20. // cin >> f >> s;
  21. // weight.push_back(f);
  22. // cost.push_back(s);
  23. //}
  24. int matrix[n+1][w+1];
  25. for(int i = 0;i<=w;++i)matrix[0][i] = 0;
  26. for(int i = 0;i<=n;++i)matrix[i][0] = 0;
  27. for(int i = 1;i<=n;++i){
  28. for(int j = 1;j<=w;++j)if(weight[i-1] <= j){
  29. matrix[i][j] = max(matrix[i-1][j],matrix[i-1][j-weight[i-1]] + cost[i-1]);
  30. } else matrix[i][j] = matrix[i-1][j];
  31. }
  32. vector<int>answer;
  33. for(int i = n;;){
  34. for(int j = w;;){
  35. if(!matrix[i][j])break;
  36. if(matrix[i][j] != matrix[i-1][j]){
  37. answer.push_back(i);
  38. max_c += cost[i-1];
  39. max_w += weight[i-1];
  40. j -= weight[i-1];
  41. }
  42. --i;
  43. }
  44. break;
  45. }
  46. sort(answer.begin(),answer.end());
  47. cout << max_w << ' ' << max_c << '\n';
  48. for(int i = 0;i<answer.size();++i)cout << answer[i] << ' ';
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 15240KB
stdin
165
23 92
31 57
29 49
44 68
53 60
38 43
63 67
85 84
89 87
82 72
stdout
165 309
1 2 3 4 6