Under construction

Still being built. Things may change

Greedy Knapsack

Time limit1sMemory limit1024 MB

Summary
Given N items with weights and values, find the largest total value the greedy packing reaches over every capacity M from 1 to T.
Level

Hard8 of 10

Topics
Greedy, Simulation, Prefix sum
Solved
No attempts yet

Problem

Budi stumbled upon the classical 0-1 Knapsack Problem that he learned in his university class. There are NN items numbered from 1 to NN. Item ii has a positive weight WiW_i and a positive value ViV_i. The goal is to take zero or more items so that their total weight does not exceed MM and their total value is as large as possible.

Budi has not worked on this problem in a while and cannot remember how to solve it. So he devised a greedy algorithm. The algorithm processes the items one by one, from item 1 to item NN. If item ii can be taken without the total weight exceeding MM, it takes the item. Otherwise, it skips the item. The output is the total value of all taken items.

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

Budi knows this greedy algorithm may not find the optimal solution, but he does not mind for now. He noticed that the output depends on MM. So he runs the greedy algorithm for every MM from 1 to a given upper limit TT, and wants to find the largest output he can get.

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\}. The largest output is 3, for instance with M=6M = 6. The greedy algorithm takes items 2, 3, and 4, with a total weight of 1+2+3=61 + 2 + 3 = 6 and a total value of 1+1+1=31 + 1 + 1 = 3. If M=10M = 10, the algorithm takes only item 1, with a total weight of 10 and a total value of 1.

Your task is to find the largest output the greedy algorithm can produce when MM ranges from 1 to TT.

Input

The first line contains two integers NN and TT (1≤N≤100 0001 \le N \le 100\,000; 1≤T≤10101 \le T \le 10^{10}), the number of items and the upper limit. The second line contains NN integers WiW_i (1≤Wi≤100 0001 \le W_i \le 100\,000), the weight of each item. The third line contains NN integers ViV_i (1≤Vi≤100 0001 \le V_i \le 100\,000), the value of each item.

Output

Print one integer on a line, the largest output the greedy algorithm can obtain.

Examples3

  1. Example 1

    Input
    5 10
    10 1 2 3 4
    1 1 1 1 1
    
    Expected output
    3
    
  2. Example 2

    Input
    5 10000000000
    10 1 2 3 4
    30 2 15 7 11
    
    Expected output
    65
    
  3. Example 3

    Input
    5 20
    4 9 5 1 3
    203 175 131 218 304
    
    Expected output
    900