Pour juice between N bottles of capacity C, each pour filling or emptying one, to maximize the sum of prices over all final amounts.
Hard8Dynamic programmingGreedyMathNo attempts yetTime limit2sMemory limit512 MBThere are N bottles, and every bottle has the same maximum capacity of C liters. Bottle i holds Bi liters of kiwi juice.
Dotori sells this juice to buy a new laptop. The price of one bottle is set only by how much juice is in it. A price is fixed for every amount from 0 liters up to C liters, and a fuller bottle is not always worth more. One bottle holding 0 liters can be worth more than one bottle filled to C liters.
Instead of selling the bottles as they are, Dotori pours juice between them to push the total higher. The pouring rule is this. Pick two different bottles A and B, then pour from A into B without stopping until A is empty or B is full. For example, if C=10, bottle A holds 5 liters and bottle B holds 7 liters, then after the pour A holds 3 liters and B holds 10 liters. Under the same capacity, if A holds 3 liters and B holds 4 liters, then after the pour A holds 0 liters and B holds 7 liters.
Dotori may pour as many times as he wants. He is sure every bottle sells, so the money he receives is the sum of the prices of all N bottles. Find the largest amount he can receive.
The first line contains the number of bottles N and the maximum capacity C. (1≤N≤15, 1≤C≤49)
The second line contains B1,B2,…,BN, the amount of juice in each bottle. (0≤Bi≤C)
The third line contains P0,P1,…,PC, the price for each amount, in order from 0 liters up to C liters. (0≤Pi≤106)
Print the maximum amount of money Dotori can receive, on one line.