fork(1) download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class Knapsack{
  7. public:
  8. long long maximumValue(vector<int>& weights, vector<int>& values, int n, int capacity) {
  9. vector<vector<long long>> dp(n+1, vector<long long>(capacity+1, 0));
  10. for(int i = 1; i <= n; ++i) {
  11. for(int j = 1; j <= capacity; ++j) {
  12. if(weights[i-1] > j) dp[i][j] = dp[i-1][j];
  13. else {
  14. dp[i][j] = max(dp[i-1][j], values[i-1]+dp[i-1][j-weights[i-1]]);
  15. }
  16. }
  17. }
  18. return dp[n][capacity];
  19. }
  20. };
  21.  
  22.  
  23. int main() {
  24. int n, c;
  25. cin >> n >> c;
  26. vector<int> weights(n), values(n);
  27.  
  28. for(int i = 0; i < n; ++i) {
  29. cin >> weights[i] >> values[i];
  30. }
  31.  
  32. Knapsack* obj = new Knapsack();
  33. cout << obj->maximumValue(weights, values, n, c);
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5256KB
stdin
3 8
3 30
4 50
5 60
stdout
90