fork download
  1. class Object:
  2.  
  3. def __init__(self, w, c):
  4.  
  5. self.w = w
  6.  
  7. self.c = c
  8.  
  9. def dynamic_programming_in_action( arr, N, Gmax ):
  10.  
  11. Profit = [[ 0 for j in range(Gmax+1 ) ] for i in range(0, N +1 )]
  12.  
  13. for i in range( 1, N + 1 ):
  14.  
  15. for j in range(1, Gmax+1):
  16.  
  17. if arr[i].w <= j:
  18.  
  19. Profit[i][j] = max(arr[i].c + Profit[i-1][j-arr[i].w], Profit[i-1][j])
  20.  
  21. else:
  22.  
  23. Profit[i][j] = Profit[i-1][j]
  24.  
  25. print(Profit[N][Gmax])
  26.  
  27. def fn():
  28.  
  29. content = [x.strip() for x in input().split()]
  30.  
  31. num_of_objects = int(content[0])
  32.  
  33. Gmax = int(content[1])
  34.  
  35. arr = [0]
  36.  
  37. for i in range(1,num_of_objects + 1):
  38.  
  39. content = [x.strip() for x in input().split()]
  40.  
  41. w = int(content[ 0 ])
  42.  
  43. c = int(content[ 1 ])
  44.  
  45. arr.append(Object( w, c ))
  46.  
  47. dynamic_programming_in_action( arr, num_of_objects, Gmax )
  48.  
  49. fn()
Success #stdin #stdout 0.03s 9688KB
stdin
6 10
3 7
3 4
1 2
1 9
2 4
1 5
stdout
29