This page is still under construction.

Parts of this page are still being built. What you see may change.

Mountain Trek Route

Time limit2sMemory limit64 MB

Summary
Add at most k unit blocks onto circular steps to maximize the drop in total up-and-down height around the loop.
Level

Hard8 of 10

Topics
Greedy, Heap, Union-find
Solved
No attempts yet

Problem

A circular mountain cycling trek route was built in the countryside near Almaty. It starts and finishes at the same point. The route is modeled as nn steps of equal width. Step ii is horizontal and sits at height aia_i meters above sea level. Two neighboring steps may have the same height. The difficulty of the route is the total of every climb and every descent along the closed loop.

difficulty=∣a1−a2∣+∣a2−a3∣+⋯+∣an−1−an∣+∣an−a1∣\text{difficulty} = |a_1 - a_2| + |a_2 - a_3| + \cdots + |a_{n-1} - a_n| + |a_n - a_1|

The route as built is too hard for tourists. To lower the difficulty you can use kk blocks. A block is as wide as a step and one meter high. You can put a block on a step or on another block, and you do not have to use every block.

Find the largest possible decrease of the difficulty.

Input

The first line contains the number of steps nn and the number of blocks kk. (2≤n≤1062 \le n \le 10^6, 1≤k≤1091 \le k \le 10^9)

The second line contains the heights a1,a2,…,ana_1, a_2, \dots, a_n. (0≤ai≤1090 \le a_i \le 10^9)

Output

Print the largest possible decrease of the difficulty on one line.

Hint

In the first example the difficulty of the route is 6: three descents of height 1, and one climb of height 3 that leads from the last step back to the first one. Putting one block on the third step and two blocks on the last step lowers the difficulty by 4. Using all five blocks gives the same answer, and no arrangement lowers the difficulty further.

Examples3

  1. Example 1

    Input
    4 5
    4 3 2 1
    
    Expected output
    4
    
  2. Example 2

    Input
    3 2
    1 2 1
    
    Expected output
    2
    
  3. Example 3

    Input
    7 1000
    4 3 3 2 3 2 1
    
    Expected output
    8