-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxprofit.py
43 lines (31 loc) · 1.25 KB
/
maxprofit.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
def max_profit(n, m, k, planets):
max_profit = 0
for buy_planet in range(n):
for sell_planet in range(n):
if buy_planet == sell_planet:
continue
total_profit = 0
items_to_buy = []
for item_type in range(m):
aij = planets[buy_planet][item_type][0]
bij = planets[sell_planet][item_type][1]
cij = planets[buy_planet][item_type][2]
if bij > aij:
items_to_buy.append((bij - aij, min(cij, k)))
items_to_buy.sort(reverse=True, key=lambda x: x[0])
remaining_capacity = k
for profit_per_item, quantity in items_to_buy:
if remaining_capacity == 0:
break
max_quantity = min(quantity, remaining_capacity)
total_profit += profit_per_item * max_quantity
remaining_capacity -= max_quantity
max_profit = max(max_profit, total_profit)
return max_profit
n, m, k = map(int, input().split())
planets = []
for _ in range(n):
planet_name = input().strip()
items = [tuple(map(int, input().split())) for _ in range(m)]
planets.append(items)
print(max_profit(n, m, k, planets))