The algorithm for the knapsack problem does not pass all tests

Viewed 53

Good afternoon! I am starting to learn dynamic programming and trying to "I" problem on CodeForces. codeforces.com/gym/100135 Problem link: LINK Here is my code:

def read_ints():
return list(map(int, input().split()))

s, n = read_ints()
c = read_ints()

d = [[False for i in range(s + 1)] for j in range(n + 1)]
d[0][0] = True

for i in range(1, n + 1):
    for j in range(s + 1):
        d[i][j] = d[i - 1][j] or d[i - 1][j - c[i - 1]]

i = s
while not d[n][i]:
    i -= 1

print(i)

I use the typical way of solving knapsack problems. But this algorithm fails the fifth test. What could be the problem?

0 Answers
Related