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, A_i, 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 |B_1 - B_2| + |B_2 - B_3| + \dots + |B_{K-1} - B_K|, where B_i is the i^\text{th} element of his subsequence and |A - B| 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

2 \le N \le 50\,000

2 \le K \le \min(100, N)

1 \le A_i \le 50\,000

Subtask 1 [40%]

2 \le N \le 1\,000

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, A_i, 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

6 2
1 5 6 2 1 3

Sample Output 1

0

Explanation for Sample 1

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

Sample Input 2

6 4
3 2 6 2 7 9

Sample Output 2

6

Explanation for Sample 2

It can be proven that the minimal sum is achieved with the subsequence [A_1 = 3, A_2 = 2, A_4 = 2, A_5 = 7].


Comments


  • 0
    maxcruickshanks  commented on Jan. 4, 2023, 7:35 p.m.

    Since the original data were weak, an additional test case was added to each subtask, and all submissions were rejudged.