atcoder#AGC057B. [AGC057B] 2A + x

[AGC057B] 2A + x

Score : 700700 points

Problem Statement

You are given a sequence A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) of positive integers and a positive integer XX. You can perform the operation below any number of times, possibly zero:

  • Choose an index ii (1iN1\leq i\leq N) and a non-negative integer xx such that 0xX0\leq x\leq X. Change AiA_i to 2Ai+x2A_i+x.

Find the smallest possible value of $\max\{A_1,A_2,\ldots,A_N\}-\min\{A_1,A_2,\ldots,A_N\}$ after your operations.

Constraints

  • 2N1052\leq N\leq 10^5
  • 1X1091\leq X\leq 10^9
  • 1Ai1091\leq A_i\leq 10^9

Input

Input is given from Standard Input in the following format:

NN XX

A1A_1 A2A_2 \ldots ANA_N

Output

Print the smallest possible value of $\max\{A_1,A_2,\ldots,A_N\}-\min\{A_1,A_2,\ldots,A_N\}$ after your operations.

4 2
5 8 12 20
6

Let (i,x)(i, x) denote an operation that changes AiA_i to 2Ai+x2A_i+x. An optimal sequence of operations is

  • (1,0)(1,0), (1,1)(1,1), (2,2)(2,2), (3,0)(3,0)

which yields A=(21,18,24,20)A = (21, 18, 24, 20), achieving $\max\{A_1,A_2,A_3,A_4\}-\min\{A_1,A_2,A_3,A_4\} = 6$.

4 5
24 25 26 27
0

An optimal sequence of operations is

  • (1,5)(1,5), (1,5)(1,5), (2,5)(2,5), (2,1)(2,1), (3,2)(3,2), (3,3)(3,3), (4,0)(4,0), (4,3)(4,3)

which yields A=(111,111,111,111)A = (111,111,111,111), achieving $\max\{A_1,A_2,A_3,A_4\}-\min\{A_1,A_2,A_3,A_4\} = 0$.

4 1
24 25 26 27
3

Performing no operations achieves $\max\{A_1,A_2,A_3,A_4\}-\min\{A_1,A_2,A_3,A_4\} = 3$.

10 5
39 23 3 7 16 19 40 16 33 6
13