Guest User

Untitled

a guest
May 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. # Uses python3
  2. import sys
  3.  
  4. def optimal_weight(W, w):
  5. n=len(w)
  6. mat_n=[[0 for x in range(0,W+1)] for y in range(0,n+1)]
  7. for ob in range(0,n):
  8. for we in range(1,W+1):
  9. if w[ob]<=we:
  10. mat_n[ob+1][we]=max(mat_n[ob][we],w[ob]+mat_n[ob][we-w[ob]])
  11. else:
  12. mat_n[ob+1][we]=mat_n[ob][we]
  13. return mat_n[n][W]
  14.  
  15. if __name__ == '__main__':
  16. input_n = sys.stdin.read()
  17. W, n, *w = list(map(int, input_n.split()))
  18. print(optimal_weight(W, w))
Add Comment
Please, Sign In to add comment