cswork / OA打开练习工作台

AmazonOA #50

Minimize System Cost

估计中等可在线判题

考点(AI 标注)前缀和区间双指针滑窗

Remove exactly k contiguous elements; the remaining elements keep their original order, and the cost is the sum of the absolute differences between adjacent elements. Find the minimum cost.

输入格式

The first line contains n k (1≤k<n≤200000), and the second line contains n values (1..10⁹).

输出格式

Output a single integer answer.

样例 1

输入:

5 3
3 9 4 2 16

输出:

6

样例 2

输入:

2 1
1 9

输出:

0

样例 3

输入:

4 1
1 4 2 8

输出:

5

样例说明

Example 1: removing the last three elements leaves [3,9] with cost 6, smaller than the other two windows. Example 2: whichever element is removed, only one element remains, so the cost is 0. Example 3: removing the trailing 8 leaves [1,4,2] with cost 3+2=5; removing 4 gives 1+6=7, and no other option is better.