fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. int n;
  7. cin >> n; // jumlah item
  8. vector<int> weight(n), value(n);
  9. for (int i = 0; i < n; i++) cin >> weight[i];
  10. for (int i = 0; i < n; i++) cin >> value[i];
  11. int W;
  12. cin >> W;
  13.  
  14. vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
  15.  
  16. for (int i = 1; i <= n; i++) {
  17. for (int w = 1; w <= W; w++) {
  18. if (weight[i - 1] <= w)
  19. dp[i][w] = max(value[i - 1] + dp[i - 1][w - weight[i - 1]], dp[i - 1][w]);
  20. else
  21. dp[i][w] = dp[i - 1][w];
  22. }
  23. }
  24.  
  25. cout << dp[n][W]; // hasil akhir
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 5244KB
stdin
8
3 10 6 7 9 10 7 5
1 10 8 1 7 8 9 18
35
stdout
46