fork download
  1. #include<stdio.h>
  2.  
  3. int max(int n, int W, int w[n], int v[n], int weight, int i){
  4.  
  5. if (i == n || weight > W) {
  6. return 0;
  7. }
  8.  
  9. if(weight + w[i] <= W){
  10. int in = v[i] + max(n, W, w, v, weight + w[i], i + 1);
  11. int ex = max(n, W, w, v, weight, i + 1);
  12. return (in > ex) ? in : ex;
  13. }
  14.  
  15. else return max(n, W, w, v, weight, i + 1);
  16. }
  17.  
  18. int main(){
  19. int n, W;
  20. scanf("%d %d", &n, &W);
  21. int w[n], v[n];
  22. for(int i = 0; i < n; i++){
  23. scanf("%d %d", &w[i], &v[i]);
  24. }
  25. printf("%d", max(n, W, w, v, 0, 0));
  26. }
Success #stdin #stdout 0s 5284KB
stdin
5 6
3 1
1 3
2 3
3 5
3 5
stdout
11