[CF2290G2] 环湖步道(困难版)(Lakeside Trail - Hard)
Problem Statement
(This is the Hard Version of the problem; only the constraints on , , and are larger.)
A circular lakeside trail consists of segments numbered through clockwise, with initial heights (segment is adjacent to segment ). The steepness is ().
You have units of dirt to raise segments (never lower). Find the minimum possible steepness after spending at most units of dirt.
Input
The first line contains () — the number of test cases.
Each test case starts with and (, ), followed by integers ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print a single integer — the minimum possible steepness.
Example
6
4 3
1 5 2 8
5 5
2 9 4 1 6
3 0
1 4 7
3 100
1 10 1
5 5
4 1 1 1 1
6 14
3 6 2 4 1 5
5
4
6
0
2
1
Note
- Constraints: , , , , .
- Fast I/O is recommended due to the large input size.
- Source: Codeforces 2290G2 · Lakeside Trail (Hard Version)