Advertisement
Egor_1425

Untitled

May 24th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.41 KB | None | 0 0
  1. def optimal_weight(W, golds):
  2.     d = [[True] + [False] * W]
  3.     for i in range(len(golds)):
  4.         d.append(d[-1][:])
  5.         for w in range(golds[i], W + 1):
  6.             d[-1][w] = d[-2][w] or d[-2][w - golds[i]]
  7.         d = d[-1:]
  8.     for w in range(W, -1, -1):
  9.         if d[-1][w]:
  10.             return w
  11.    
  12.  
  13. W, n = map(int, input().split())
  14. w = list(map(int, input().split()))
  15. print(optimal_weight(W, w))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement