Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def optimal_weight(W, golds):
- d = [[True] + [False] * W]
- for i in range(len(golds)):
- d.append(d[-1][:])
- for w in range(golds[i], W + 1):
- d[-1][w] = d[-2][w] or d[-2][w - golds[i]]
- d = d[-1:]
- for w in range(W, -1, -1):
- if d[-1][w]:
- return w
- W, n = map(int, input().split())
- w = list(map(int, input().split()))
- print(optimal_weight(W, w))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement