The Château de Calberte is an ancient castle in France that fell into ruin after the knowledge of its construction was lost. It is high summer and you are volunteering at the Château to help historians repair its battlements.
The Château's battlements have sections numbered
to
in order, where each section has been assigned a height
. Each day, you will choose two sections
and
and increase the heights of all sections between them by
, inclusive. If you can volunteer for up to
days, what is the minimum sum of absolute differences between all adjacent heights that can be achieved by the end?
Constraints
Subtask 1 [10%]
Subtask 2 [30%]
Subtask 3 [60%]
No further constraints.
Input Specification
The first line of input will contain two space-separated integers, and
.
The next line of input will contain space-separated integers,
.
Output Specification
Output one integer, the minimum sum of absolute differences between all adjacent heights.
Sample Input 1
3 3
3 1 6
Sample Output 1
2
Explanation for Sample Output 1
On the first and second days, choose and
. On the third day, choose
and
. The resulting heights are
so the sum is
.
Sample Input 2
10 12
10 9 8 2 1 7 4 10 5 6
Sample Output 2
6
Comments