forked from shunr/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcco10p4.py
More file actions
30 lines (27 loc) · 663 Bytes
/
cco10p4.py
File metadata and controls
30 lines (27 loc) · 663 Bytes
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
import sys
def input():
return sys.stdin.readline().strip()
t, n = int(input()), int(input())
bin = [[] for i in range(t)]
for i in range(n):
part = [int(k) for k in input().split()]
bin[part[2]-1].append((part[0], part[1]))
b = int(input())
dp = {}
def meme(cash, part):
tup = (cash, part)
if part >= t:
return 0
if tup in dp.keys():
return dp[tup]
good = False
for x in bin[part]:
if cash >= x[0]:
v = meme(cash-x[0], part + 1)
if tup not in dp.keys() or v + x[1] > dp[tup]:
dp[tup] = v + x[1]
good = True
if not good:
dp[tup] = 0
return dp[tup]
print(meme(b, 0))