-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
22 lines (17 loc) · 885 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import sys
items, capacity = sys.stdin.readline().split()
weights = sys.stdin.readlines()
#index - the item that was last placed on the truck
#weight - the current weight of the truck
def recursive(index, weight):
if weight > int(capacity):
return 0 #don't run the function if the truck is overloaded
maximum = weight #maximum weight that can be added to the truck
for i in range(index + 1, len(weights)): #only looks at items after the last one to prevent duplicates
#this function calls itself to find the maximum possible weight
#that can be added to the current weight using only the remaining items
maximum = max(maximum, recursive(i, weight + int(weights[i])))
return maximum
#we call recursive(-1, 0) to indicate that the truck is empty
#it will return the maximum possible weight using all the items
print(recursive(-1, 0))