fork download
  1. #include <bits/stdc++.h>
  2. #define int long long
  3.  
  4. using namespace std;
  5.  
  6. int W, n;
  7. int v[100111];
  8. int w[100111];
  9.  
  10. int maxValue(int W, int pos) {
  11. if (W == 0 || pos == 0) {
  12. return 0;
  13. }
  14.  
  15. if (w[pos] > W) {
  16. return maxValue(W, pos-1);
  17. }
  18.  
  19. return max(maxValue(W, pos-1), v[pos] + maxValue(W - w[pos], pos-1));
  20. }
  21.  
  22. int32_t main() {
  23. ios_base::sync_with_stdio(false);
  24. cin.tie(NULL);
  25.  
  26. cin >> n >> W;
  27. for (int i = 1; i <= n; i += 1) {
  28. cin >> w[i] >> v[i];
  29. }
  30.  
  31. cout << maxValue(W, n);
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty