cswork / OAOpen practice workspace

MicrosoftOA #41

Maximizing Difference in Array Elements with Moves

Estimated HardJudge-ready

Topics (AI-labelled)Binary SearchPrefix & RangesGreedy

Each operation adds 1 to or subtracts 1 from any one element, at most K times in total. N is divisible by 3; maximize the N/3-th largest element minus the N/3-th smallest element.

This site uses a standard input/output protocol.

Input

The first line contains N K; the second line contains N integers. N is a multiple of 3 in 3..150000, K is in 0..500000000, and the elements are in −300000000..300000000.

Output

Output a single integer, the answer.

Example 1

Input:

6 3
6 6 6 6 6 6

Output:

1

Example 2

Input:

3 2
1 2 3

Output:

4

Example 3

Input:

12 1
8 8 8 7 7 7 7 7 7 7 -8 -8

Output:

1

Example notes

Example 1: both 6s must change to move the quantile by 1, so 3 steps can only increase the gap by 1. Example 2: the original gap is 2; lowering 1 to −1 makes the gap 4. Example 3: changing one 7 to 8 moves the upper quantile from 7 to 8 while the lower quantile stays at 7, for a gap of 1.

Editorial

Loading solution…