Orderbook Omnishamble


The panic button has been hit! Your firm is currently swiftly retreating from the market after the orderbook glitched and you've been called to help with the recovery. You are an analyst estimating losses to claim, and to solve this issue, you are given:

  • A sorted (ascending) array of integers nn, representing the spread of last known orders.
  • A zero or positive integer kk, representing your estimated capital.
  • A positive integer cc, representing a range of risk values.

These represent the spread of the last known orders, estimated capital, and identify a range of risk values respectively. This risk value range is represented by the integer cc, representing that the range encompasses the nc\frac{n}{c}-th smallest number to the nc\frac{n}{c}-th highest number in the array nn. The integer kk, your estimated capital, shall be used for operations on the values in the report you are writing up.

Each operation will cost 1 unit of your theoretical capital, but in exchange, you may increment or decrement any value in nn by 1 (this will alter the risk range!). Your task is to maximize the risk range, namely, maximize the difference between the nc\frac{n}{c}-th highest number and the nc\frac{n}{c}-th smallest number in the array nn, making only kk operations.

Your company has told you, luckily, that the elements in the array nn will always be a multiple of cc, but that is all.

Good luck analyst, your company relies on you.

Input
  • The first line contains integers ∣n∣|n| (length of array), kk (capital), and cc (range).
  • The second line contains nn integers, representing the sorted array.
Output
  • A single integer containing the maximum risk range achievable with up to kk operations.
Constraints
  • 2≤c≤n2 \leq c \leq n
  • nmod  c=0n \mod c = 0
  • 2≤n≤1052 \leq n \leq 10^5
  • 0≤k≤1090 \leq k \leq 10^9
  • −105≤ni≤105-10^5 \leq n_i \leq 10^5

Example 1

Input 1
12 1 3
-8 -8 7 7 7 7 7 7 7 8 8 8
Output 1
1

Example 2

Input 2
4 3 2
1 1 1 1
Output 2
1

Example 3

Input 3
12 5 12
1 1 1 1 1 1 1 1 1 1 1 1
Output 3
5

Example 4

Input 4
6 0 3
5 7 9 12 13 15
Output 4
6

Example 5

Input 5
9 6 3
-5 1 1 4 4 4 4 6 7
Output 5
7

Example 6

Input 6
12 5 3
-7 -6 -3 -2 -2 -2 -2 -2 -2 -2 -2 -1
Output 6
3

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.