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. Optime = [0 for j in range(Gmax+1)]
  12.  
  13. for i in range(1, N + 1):
  14.  
  15. for j in range(Gmax, 1, -1):
  16.  
  17. if j >= arr[i].w:
  18.  
  19. Optime[ j ] = max(Optime[ j ], arr[i].c + Optime[j-arr[i].w])
  20.  
  21. print( Optime[ Gmax ] )
  22.  
  23. def fn():
  24. content = [x.strip() for x in input().split()]
  25.  
  26. num_of_objects = int(content[0])
  27.  
  28. Gmax = int(content[1])
  29.  
  30. arr = [0]
  31.  
  32. for i in range(1,num_of_objects + 1):
  33.  
  34. content = [x.strip() for x in input().split()]
  35.  
  36. w = int(content[ 0 ])
  37.  
  38. c = int(content[ 1 ])
  39.  
  40. arr.append(Object( w, c ))
  41.  
  42. dynamic_programming_in_action( arr, num_of_objects, Gmax )
  43.  
  44. fn()
Success #stdin #stdout 0.04s 9652KB
stdin
5 20
2 3
4 5
5 8
3 4
9 10
stdout
26