-
Notifications
You must be signed in to change notification settings - Fork 0
/
santasknapsack.py
47 lines (40 loc) · 1.04 KB
/
santasknapsack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import math
#read n, m and k
s = input()
line1 = list(map(int, s.split()))
if len(line1)==3:
[n,m,k] = line1
else:
print('Wrong input, try again')
k_ = []
w_ji = []
h_ji = []
for j in range(k):
s = input()
line = list(map(int, s.split()))
k_.append(line[0])
rest = line[1:]
wj, hj = rest[0::2], rest[1::2]
w_ji.append(wj)
h_ji.append(hj)
k_, w_ji, h_ji
c = [[0 for l in range(m+1)]for i in range(n+1)]
def knapsack():
for j in range(0,k):
for i in range(1,k_[j]+1):
if j != 0:
sum_ = 0
for h in range(j):
sum_ = sum_ + k_[h]
index = i + sum_
else:
index = i
for l in range(0,m+1):
if l == 0:
c[index][l] = 0
elif w_ji[j][i-1] <= l:
c[index][l] = max(c[index-1][l],c[index-i][l-w_ji[j][i-1]]+h_ji[j][i-1])
else:
c[index][l] = c[index-1][l]
knapsack()
print(str(c[n][m]) + "\\n")