PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000122. Chia dãy đã sắp xếp (Array Splitting)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chia dãy đã sắp xếp (Array Splitting)

Array Splitting

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a nondecreasing array a1,…,ana_1,\ldots,a_n, split it into exactly kk nonempty contiguous subarrays. The cost of a subarray is its maximum minus its minimum. Minimize the total cost.

Input

The first line has n,kn,k. The next line has nn nondecreasing integers aia_i.

Output

Print the minimum total splitting cost.

Subtasks

  • Subtask 1 (20 points): 1≤k≤n≤251\le k\le n\le 25; 1≤a1≤a2≤⋯≤an≤1091\le a_1\le a_2\le\cdots\le a_n\le10^9.
  • Subtask 2 (30 points): 1≤k≤n≤15001\le k\le n\le 1500; 1≤a1≤a2≤⋯≤an≤1091\le a_1\le a_2\le\cdots\le a_n\le10^9.
  • Subtask 3 (50 points): 1≤k≤n≤3000001\le k\le n\le 300000; 1≤a1≤a2≤⋯≤an≤1091\le a_1\le a_2\le\cdots\le a_n\le10^9.

Examples

Input

6 3
4 8 15 16 23 42

Output

12

Explanation

Split into [4,8,15,16][4,8,15,16], [23][23] and [42][42] with costs 12,0,012,0,0. These cuts remove the two largest adjacent gaps 1919 and 77.