-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oving6
49 lines (34 loc) · 1.28 KB
/
Oving6
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
48
49
from sys import stdin
DP = {}
def max_value(bills, paper_width, paper_height):
if paper_width < paper_height:
paper_width, paper_height = paper_height, paper_width
pair = (paper_width, paper_height)
if pair in DP:
return DP[pair]
max_val = bills[pair] if pair in bills else 0
for x in range(1, paper_width):
max_val = max(max_val,(max_value(bills, x, paper_height) + max_value(bills, paper_width-x, paper_height)))
for y in range(1, paper_height):
max_val = max(max_val, (max_value(bills, paper_width, y) + max_value(bills, paper_width, paper_height-y)))
DP[pair] = max_val
return max_val
def main():
bills = {}
for triple in stdin.readline().split():
dim_value = triple.split(':', 1)
dim = dim_value[0].split('x', 1)
width = int(dim[0][1:])
height = int(dim[1][:-1])
value = int(dim_value[1])
if width < height:
width, height = height, width
pair = (width, height)
if pair in bills:
value = max(value, bills[pair])
bills[pair] = value
for line in stdin:
paper_width, paper_height = [int(x) for x in line.split('x', 1)]
print(max_value(bills, paper_width, paper_height))
if __name__ == "__main__":
main()