Max's Anger Contest Series 1 P5 - Slacking Subsequences

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 4.0s
Memory limit: 1G

Author:
Problem types

Since Max is wasting his time in his List I Communication credit lecture, he decided to start taking subsequences of arrays instead!

Specifically, he has N integers, Ai, and wants to take a subsequence of length K, but to make his life even harder, his List I Communication credit professor adds a restriction: he must find the subsequence that has a length of K with the minimum sum of the absolute difference between consecutive terms.

That is, he must minimize |B1B2|+|B2B3|++|BK1BK|, where Bi is the ith element of his subsequence and |AB| denotes the absolute difference between A and B.

Can you find the minimum sum of absolute differences for every possible subsequence of length K?

Constraints

2N50000

2Kmin(100,N)

1Ai50000

Subtask 1 [40%]

2N1000

Subtask 2 [60%]

No additional constraints.

Input Specification

The first line will contain two integers, N and K, the array's length and the subsequence's length, respectively.

The second line will contain N integers, Ai, the elements of the array.

Output Specification

Output the minimum sum of absolute differences between consecutive elements for any subsequence of length K.

Sample Input 1

Copy
6 2
1 5 6 2 1 3

Sample Output 1

Copy
0

Explanation for Sample 1

It can be proven that the minimal sum is achieved with the subsequence [A1=1,A5=1].

Sample Input 2

Copy
6 4
3 2 6 2 7 9

Sample Output 2

Copy
6

Explanation for Sample 2

It can be proven that the minimal sum is achieved with the subsequence [A1=3,A2=2,A4=2,A5=7].


Comments

There are no comments at the moment.