fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. long long N, W;
  7.  
  8. long long w[110], v[110];
  9.  
  10. long long dp[110][100010];
  11.  
  12. long long f(int pos, long long cur) {
  13. if(cur > W) return -1000000000;
  14. if(pos == N) return 0;
  15. if(dp[pos][cur] == -1) {
  16. long long ans = -1000000000;
  17. //tomar
  18. ans = max(ans, f(pos + 1, cur + w[pos]) + v[pos]);
  19. //no tomar
  20. ans = max(ans, f(pos + 1, cur));
  21. dp[pos][cur] = ans;
  22. }
  23.  
  24. return dp[pos][cur];
  25. }
  26.  
  27. int main () {
  28. #ifndef ONLINE_JUDGE
  29. freopen("input.txt", "r", stdin);
  30. freopen("output.txt", "w", stdout);
  31. #endif
  32. cin >> N >> W;
  33. for(int i = 0; i < N; i++)
  34. cin >> w[i] >> v[i];
  35. memset(dp, -1, sizeof dp);
  36. cout << f(0, 0) << "\n";
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0.02s 89596KB
stdin
Standard input is empty
stdout
0