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
from collections import defaultdict
def main():
input = sys.stdin.read().split()
ptr = 0
N, X, Y = map(int, input[ptr:ptr+3])
ptr +=3
max_sat = []
for _ in range(N):
K = int(input[ptr])
ptr +=1
sweets = []
for _ in range(K):
a, b, c = map(int, input[ptr:ptr+3])
ptr +=3
sweets.append( (a, b, c) )
dp = [-1]*(Y+1)
dp[0] =0
for a, b, c in sweets:
k =1
rem = c
while rem >0:
take = min(k, rem)
aa = a * take
bb = b * take
for y in range(Y, aa-1, -1):
if dp[y - aa] != -1:
if dp[y] < dp[y -aa] + bb:
dp[y] = dp[y -aa] + bb
rem -= take
k *=2
max_sat.append(dp)
# Read distance matrix
dist = []
for _ in range(N):
row = list(map(int, input[ptr:ptr+N]))
ptr +=N
dist.append(row)
# Floyd-Warshall
for k in range(N):
for i in range(N):
for j in range(N):
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]
# DP state: key is (mask, u), value is dict of y_sum -> (transport_cost, satisfaction)
dp = defaultdict(dict)
# Initial state: mask 0 (only town 0 visited), current town 0
for y in range(Y+1):
if max_sat[0][y] != -1:
dp_key = (0, 0)
if y not in dp[dp_key] or (0 < dp[dp_key][y][0] or (0 == dp[dp_key][y][0] and max_sat[0][y] > dp[dp_key][y][1])):
if dp_key not in dp:
dp[dp_key] = {}
dp[dp_key][y] = (0, max_sat[0][y])
max_result = 0
# Check initial state (only town 0)
for (mask, u), y_dict in dp.items():
for y_sum, (tc, sat) in y_dict.items():
total_cost = tc + dist[u][0] + y_sum
if total_cost <= X and y_sum <= Y:
if sat > max_result:
max_result = sat
# Process transitions
for _ in range(N*N):
new_dp = defaultdict(dict)
updated = False
for (mask, u), y_dict in dp.items():
for y_sum, (tc, sat) in y_dict.items():
# Try to return to 0
total_tc = tc + dist[u][0]
if total_tc + y_sum <= X and y_sum <= Y and sat > max_result:
max_result = sat
# Try to visit other towns
for v in range(N):
if v == u:
continue
# Check if already in mask (mask is towns visited except u)
# Assuming mask does not include u
# So, the new mask is mask | (1 << v)
new_mask = mask | (1 << v)
# Calculate new transport cost
new_tc = tc + dist[u][v]
if new_tc > X:
continue
# Now, for town v, consider all possible y_v
for y_v in range(Y - y_sum +1):
if max_sat[v][y_v] == -1:
continue
new_y = y_sum + y_v
if new_y > Y:
continue
new_sat = sat + max_sat[v][y_v]
new_state = (new_mask, v)
existing = new_dp.get(new_state, {}).get(new_y, (float('inf'), -1))
if new_tc < existing[0] or (new_tc == existing[0] and new_sat > existing[1]):
if new_state not in new_dp:
new_dp[new_state] = {}
new_dp[new_state][new_y] = (new_tc, new_sat)
updated = True
# Merge new_dp into dp
for key, y_dict in new_dp.items():
if key not in dp:
dp[key] = {}
for y, val in y_dict.items():
if y not in dp[key] or val[0] < dp[key][y][0] or (val[0] == dp[key][y][0] and val[1] > dp[key][y][1]):
dp[key][y] = val
if not updated:
break
# Check all states
for (mask, u), y_dict in dp.items():
for y_sum, (tc, sat) in y_dict.items():
total_cost = tc + dist[u][0] + y_sum
if total_cost <= X and y_sum <= Y and sat > max_result:
max_result = sat
print(max_result)
if __name__ == "__main__":
main()