Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Uses python3
- import sys
- def optimal_weight(W, w):
- n=len(w)
- mat_n=[[0 for x in range(0,W+1)] for y in range(0,n+1)]
- for ob in range(0,n):
- for we in range(1,W+1):
- if w[ob]<=we:
- mat_n[ob+1][we]=max(mat_n[ob][we],w[ob]+mat_n[ob][we-w[ob]])
- else:
- mat_n[ob+1][we]=mat_n[ob][we]
- return mat_n[n][W]
- if __name__ == '__main__':
- input_n = sys.stdin.read()
- W, n, *w = list(map(int, input_n.split()))
- print(optimal_weight(W, w))
Add Comment
Please, Sign In to add comment