#P14665. [KenOI 2025] 序列题

    ID: 14525 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>贪心二分三分洛谷原创O2优化双指针 two-pointer洛谷比赛

[KenOI 2025] 序列题

Description

Given a sequence aa of nn positive integers. Now you can perform one of the following operations up to mm times:

  • Increase each number in the range [l,r][l,r] by 11.
  • Decrease each number in the range [l,r][l,r] by 11.

You can independently select ll and rr in each operation.

Let xx be the maximum value of the sequence, and yy be the minimum value after the operations.

Find the minimum possible value of xyx-y.

Input Format

The first line contains two integers, nn and mm.

The second line has nn numbers, and the ii-th\text{th} number is aia_i.

Output Format

One single number, the minimum possible value of xyx-y.

5 2
1 2 3 4 5
2

Hint

Example explanation

We can increase each number in [1,4][1,4] and [1,3][1,3] by 11 to make the sequence 3,4,5,5,53,4,5,5,5, xy=53=2x-y=5-3=2. It can be proven that no better solution exists.

Data Volume and Conventions

This problem is divided into subtasks. Your score in a subtask is the minimum score across all its test cases.

This problem uses subtask dependencies. You will not receive the score for a subtask unless you achieve full points on all its dependent subtasks.

Subtask nn \le mm \le Special Properties Score Dependencies
11 1010 none 1010 none
22 100100 2020 11
33 500500 2525 1,21,2
44 5×1035 \times 10^3 A 55 none
55 none 4040 1,2,3,41,2,3,4

Special property A: i[1,n),ai=ai+1\forall i \in [1,n),a_i=a_{i+1}.

For all of the cases, 1ain5×103 1 \le a_i \le n \le 5 \times 10^3.

Bonus: 1n,m2×1051 \le n,m \le 2 \times 10^5. Contestants who have solved all problems (AK) are welcome to continue exploring.