Created
October 6, 2017 19:19
-
-
Save eupp/d0cf28241c7fae5b435e0d3b49d792ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Uses python3 | |
import sys | |
import math | |
# W - вместимость рюкзака | |
# w - набор весов | |
def optimal_weight(W, w): | |
n = len(w) | |
table_res = [[math.inf for x in range(0, n+1)] for y in range(0, W+1)] | |
for i in range(n + 1): | |
table_res[0][i] = 0 | |
for j in range(W + 1): | |
table_res[j][0] = 0 | |
for j in range(1, n + 1): | |
for s in range(1, W + 1): | |
if w[j - 1] <= s: | |
table_res[s][j] = max(table_res[s][j-1], | |
table_res[s - w[j-1]][j-1] + w[j-1]) | |
else: | |
table_res[s][j] = table_res[s][j-1] | |
return table_res[W][n] | |
if __name__ == '__main__': | |
W, n = list(map(int, input().split())) | |
w = list(map(int, input().split())) | |
print(optimal_weight(W, w)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment