Tomorrow is finally the day of the excursion to Maizu Elementary School. Gatcho, who attends Maizu Elementary School, noticed that he had forgotten to buy tomorrow's sweets because he was so excited. Gaccho wants to stick to sweets so that he can enjoy the excursion to the fullest.
Gaccho gets a X yen allowance from his mother and goes to buy sweets. However, according to the rules of elementary school, the total amount of sweets to bring for an excursion is limited to Y yen, and if it exceeds Y yen, all sweets will be taken up by the teacher.
There are N towns in the school district where Gaccho lives, and each town has one candy store. Each town is assigned a number from 1 to N, and Gaccho lives in town 1. Each candy store sells several sweets, and the price per one and the satisfaction level that you can get for each purchase are fixed. However, the number of sweets in stock is limited. Also, Gaccho uses public transportation to move between the two towns, so to move directly from town to town , you only have to pay yen. It takes.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
At first, Gaccho is in Town 1. Gaccho should make sure that the sum of the total cost of moving and the price of the sweets you bought is within X yen, and the total price of the sweets you bought is within Y yen. I'm going to buy sweets. You must have arrived at Town 1 at the end. Gaccho wants to maximize the total satisfaction of the sweets he bought. Find the total satisfaction when you do the best thing.
Constraints
The
input satisfies the following conditions.
1leqNleq14
1leqXleq10000
1leqYleqmin(1000,X)
1leqKleq300
1leqaileq1000
1leqbileq1000
1leqcileq1000
0leqdi,jleq10000
di,i=0
Input
All inputs are given as integers in the following format:
NXY
Information on candy stores in town 1
Information on candy stores in town 2
...
Information on candy stores in town Nd1,1d1,2 ... d1,Nd2,1d2,2 ... d2,N
...
dN,1dN,2 ... dN,N
On the first line, the number of towns N, the amount of money you have X, and the maximum amount you can use to buy sweets Y are given, separated by blanks.
From the second line, information on each N candy store is given.
In the following N row and N column, the amount di,j required to go back and forth between town i and town j is given, separated by blanks.
Information on candy stores in each town is given in the following format.
Ka1b1c1a2b2c2
...
aKbKcK
The first line gives K, the number of types of sweets sold at the candy store. In the following K line, the price of one candy ai, the satisfaction level bi per candy, and the number of stocks ci are given, separated by blanks.
Output
Output the maximum value of the total satisfaction level on one line.
import sys
import math
def main():
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr]); ptr +=1
X = int(input[ptr]); ptr +=1
Y = int(input[ptr]); ptr +=1
towns = []
for _ in range(N):
K = int(input[ptr]); ptr +=1
items = []
for __ in range(K):
a = int(input[ptr]); ptr +=1
b = int(input[ptr]); ptr +=1
c = int(input[ptr]); ptr +=1
items.append( (a, b, c) )
towns.append(items)
d = []
for _ in range(N):
row = list(map(int, input[ptr:ptr+N]))
ptr += N
d.append(row)
max_sat = []
used_cap = []
for items in towns:
dp_knapsack = [0]*(Y+1)
uc = [0]*(Y+1)
for a, b, c in items:
k = 1
remaining = c
while remaining > 0:
take = min(k, remaining)
for cap in range(Y, a*take-1, -1):
if dp_knapsack[cap - a*take] + b*take > dp_knapsack[cap]:
dp_knapsack[cap] = dp_knapsack[cap - a*take] + b*take
uc[cap] = uc[cap - a*take] + a*take
remaining -= take
k *= 2
max_sat.append(dp_knapsack)
used_cap.append(uc)
INF = -math.inf
dp = [ [ [INF]*(X+1) for _ in range(Y+1)] for __ in range(N+1) ]
dp[1][0][0] = 0
for ts in range(X+1):
for sc in range(Y+1):
for i in range(1, N+1):
current = dp[i][sc][ts]
if current == INF:
continue
rem_cap = min(Y - sc, X - ts)
if rem_cap >= 0:
town_idx = i - 1
cap = min(rem_cap, Y)
add_sat = max_sat[town_idx][cap]
add_cap = used_cap[town_idx][cap]
new_sc = sc + add_cap
new_ts_buy = ts + add_cap
if new_sc <= Y and new_ts_buy <= X:
if dp[i][new_sc][new_ts_buy] < current + add_sat:
dp[i][new_sc][new_ts_buy] = current + add_sat
for j in range(1, N+1):
move_cost = d[i-1][j-1]
new_ts_move = ts + move_cost
if new_ts_move > X:
continue
if dp[j][sc][new_ts_move] < current:
dp[j][sc][new_ts_move] = current
max_ans = 0
for sc in range(Y+1):
for ts in range(X+1):
if sc <= Y and ts <= X:
max_ans = max(max_ans, dp[1][sc][ts])
print(max_ans)
if __name__ == '__main__':
main()