#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 items numbered from to . The item has a positive weight of and a positive value of . The objective is to take zero or more items such that their total weight does not exceed 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 item to the item in sequential order. If the item can be taken such that the total weight does not exceed , 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 . So, he decides to run the greedy algorithm with various ranging from to a given upper limit 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 from to .
For example, let , , , and . In this example, the largest output that can be obtained is , e.g., with ; the greedy algorithm will take the , , and items with a total weight of and a total value of . Notice that if we set , then the greedy algorithm will take only the item with a total weight of and a total value of .
输入格式
Input begins with a line containing two integers (; ) representing the number of items and the upper limit, respectively. The second line contains integers () representing the weight of the item. The third line contains integers () representing the value of the 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 to be large enough such that all items can be taken, i.e. .
Explanation for the sample input/output #3
The largest output can be obtained with . The , , , and will be taken with a total weight of and a total value of .