#P15609. [ICPC 2021 Jakarta R] Greedy Knapsack

[ICPC 2021 Jakarta R] Greedy Knapsack

题目描述

Budi stumbled upon the classical 0-1 Knapsack Problem that he has learned from his class in university. There are NN items numbered from 11 to NN. The ithi^{th} item has a positive weight of WiW_i and a positive value of ViV_i. The objective is to take zero or more items such that their total weight does not exceed MM while their total value is maximum.

It has been a while since the last time Budi works on this problem, and he couldn't remember how to solve it. So, Budi devises a greedy algorithm in an attempt to solve this problem. The algorithm goes like this. Each item is processed one by one from the 1st1^{st} item to the NthN^{th} item in sequential order. If the ithi^{th} item can be taken such that the total weight does not exceed MM, then you should take that item; otherwise, ignore it. Output the total value of all taken items.

The greedy algorithm can be presented with the following pseudocode.

GreedyKnapsack(W[1..N], V[1..N], M):
    total_value = 0
    total_weight = 0
    for i = 1 to N:
        if total_weight + W[i] <= M:
            total_weight = total_weight + W[i]
            total_value = total_value + V[i]
    return total_value

Of course, Budi realized that such a greedy algorithm might not produce the optimum solution, but at this point, he doesn't care. Budi noticed that the output of such a greedy algorithm is sensitive to MM. So, he decides to run the greedy algorithm with various MM ranging from 11 to a given upper limit TT and determines what is the largest output that he can get.

Your task in this problem is to help Budi to determine the largest output that he can get from the greedy algorithm by varying the input MM from 11 to TT.

For example, let N=5N = 5, T=10T = 10, W1..5={10,1,2,3,4}W_{1..5} = \{10, 1, 2, 3, 4\}, and V1..5={1,1,1,1,1}V_{1..5} = \{1, 1, 1, 1, 1\}. In this example, the largest output that can be obtained is 33, e.g., with M=6M = 6; the greedy algorithm will take the 2nd2^{nd}, 3rd3^{rd}, and 4th4^{th} items with a total weight of 1+2+3=61 + 2 + 3 = 6 and a total value of 1+1+1=31 + 1 + 1 = 3. Notice that if we set M=10M = 10, then the greedy algorithm will take only the 1st1^{st} item with a total weight of 1010 and a total value of 11.

输入格式

Input begins with a line containing two integers NN TT (1N1000001 \leq N \leq 100\,000; 1T10101 \leq T \leq 10^{10}) representing the number of items and the upper limit, respectively. The second line contains NN integers WiW_i (1Wi1000001 \leq W_i \leq 100\,000) representing the weight of the ithi^{th} item. The third line contains NN integers ViV_i (1Vi1000001 \leq V_i \leq 100\,000) representing the value of the ithi^{th} item.

输出格式

Output contains an integer in a line representing the largest output that can be obtained by the presented greedy algorithm.

5 10
10 1 2 3 4
1 1 1 1 1
3
5 10000000000
10 1 2 3 4
30 2 15 7 11
65
5 20
4 9 5 1 3
203 175 131 218 304
900

提示

Explanation for the sample input/output #2

You can set MM to be large enough such that all items can be taken, i.e. M20M \geq 20.

Explanation for the sample input/output #3

The largest output can be obtained with M=17M = 17. The 1st1^{st}, 2nd2^{nd}, 4th4^{th}, and 5th5^{th} will be taken with a total weight of 4+9+1+3=174 + 9 + 1 + 3 = 17 and a total value of 203+175+218+304=900203 + 175 + 218 + 304 = 900.