CCCGuide

Problem S4: Minecarts

2026 · S4 · Original problem · DMOJ

Problem Description

Steve has NN minecarts labelled from 11 to NN lined up on a track in a row from left to right. Initially, there are aia_i gems in the ii-th minecart.

Steve wants the minecarts to be arranged in a row such that the number of gems in each minecart is non-decreasing from left to right. To do this, he plans to build a side track to the right of all of the minecarts that splits off from the main track. Steve can move minecarts that are on the left of the side track into the side track, allowing other minecarts on its left to move past it on the main track. Minecarts moved into the side track can be moved back into the main track one at a time: a minecart on the side track will move back to the left of the side track and to the right of all other minecarts which are on the left of the side track. The last minecart moved into the side track must be the first minecart to be moved back out: that is, the side track follows a last-in, first-out method. The side track can be used any number of times. Finally, once a minecart is moved to the right of the side track, it can no longer be moved to the left. Below is an example sequence of moves that can be made with 55 minecarts beginning from the initial position:

A horizontal main track with an intersecting side track. Five carts are in a row on the main track, to the left of the side track. The carts are numbered 1 through 5 from left to right.

Carts 1 through 3 remain on the main track to the left of the side track. Carts 4 and 5 are on the side track, with 4 closest to the main track.

Cart 1 remains on the main track to the left of the side track. Carts 4 and 5 are on the side track, with 4 closest to the main track. Carts 2 and 3 are on the main track to the right of the side track.

Carts 1 and 4 are on the main track to the left of the side track. Cart 5 is on the side track. Carts 2 and 3 are on the main track to the right of the side track.

Steve has KK spare gems, and he can distribute any number of them into the minecarts that are empty. Steve has yet to build the side track, and he wants to limit the side track capacity to save work. A side track with a specific capacity can only store up to that many minecarts. For example, if the side track had a capacity of 11, we would not be able to make the moves in the diagrams above, which would require a capacity of at least 22. Given that Steve places his spare gems into the minecarts optimally, what is the minimum capacity of the side track that needs to be built?

Input Specification

The first line of input will consist of two space-separated integers NN and KK (1≤N≤3×105, 0≤K≤1012)(1 \leq N \leq 3\times10^5,~ 0 \leq K \leq 10^{12}).

The next line contains NN integers a1,…,ana_1,\dots,a_n (0≤ai≤106)(0\leq a_i\leq 10^6), where the ii-th integer indicates the number of gems in the ii-th minecart.

The following table shows how the available 1515 marks are distributed:

MarksBounds on NNBounds on KK
221≤N≤5 0001\leq N \leq 5\,000K=0K = 0
221≤N≤5 0001\leq N \leq 5\,000K=1012K = 10^{12}
221≤N≤5 0001\leq N \leq 5\,0000≤K≤10120\leq K \leq 10^{12}
331≤N≤3×1051\leq N \leq 3\times10^5K=0K = 0
331≤N≤3×1051\leq N \leq 3\times10^5K=1012K = 10^{12}
331≤N≤3×1051\leq N \leq 3\times10^50≤K≤10120\leq K \leq 10^{12}

Output Specification

Output a single integer, the minimum capacity of the side track that needs to be built given that Steve distributes up to KK gems among the empty minecarts optimally.

Sample Input 1

4 14 
5 0 4 0

Output for Sample Input 1

1

Explanation of Output for Sample Input 1

One optimal distribution is to put 66 of the spare gems into minecart 22 and 77 of the spare gems into minecart 44. Note that this distribution does not use all of the spare gems.

We can then build a side track of capacity 11. We can then arrange the minecarts in non-decreasing order as follows:

  1. Move minecart 44 past the side track

  2. Move minecart 33 into the side track

  3. Move minecart 22 past the side track

  4. Move minecart 11 past the side track

  5. Move minecart 33 out of the side track

  6. Move minecart 33 past the side track

Sample Input 2

4 8
5 0 4 0

Output for Sample Input 2

2

Explanation of Output for Sample Input 2

One optimal distribution is to put all 88 spare gems into minecart 44.

Then we can build a side track of capacity 22. We can then arrange the minecarts in non-decreasing order as follows:

  1. Move minecart 44 past the side track

  2. Move minecart 33 into the side track

  3. Move minecart 22 into the side track

  4. Move minecart 11 past the side track

  5. Move minecart 22 out of the side track

  6. Move minecart 33 out of the side track

  7. Move minecart 33 past the side track

  8. Move minecart 22 past the side track

Sample Input 3

4 123456789 
40 30 20 10

Output for Sample Input 3

3

Explanation of Output for Sample Input 3

Since there are no empty minecarts, there is only one possible distribution of spare gems: no spare gems are used at all.

Then we can build a side track of capacity 33. We can then arrange the minecarts in non-decreasing order as follows:

  1. Move minecart 44 into the side track

  2. Move minecart 33 into the side track

  3. Move minecart 22 into the side track

  4. Move minecart 11 past the side track

  5. Move minecart 22 out of the side track and then past the side track

  6. Move minecart 33 out of the side track and then past the side track

  7. Move minecart 44 out of the side track and then past the side track