cswork / OAOpen practice workspace

SalesforceOA #33

Minimize Total Input Cost (for LTMS)

Estimated HardJudge-ready

Topics (AI-labelled)Scheduling DP

Keeping the input order, split the array into exactly k non-empty contiguous segments, where the cost of each segment is its maximum element; find the minimum total cost over all segments. Each segment corresponds to one week of activities. Added by this site: output −1 when there is no solution, and output 0 when an empty array is split into 0 segments.

Input

The input gives n k and n non-negative integers. The original has no numeric bounds; this site adds 0≤n,k≤300, 0≤a[i]≤10^9. The cases k>n, n=0, and k=0 are not excluded and are handled by the protocol above.

Output

Output one integer answer.

Example 1

Input:

5 3
1000 500 2000 8000 1500

Output:

9500

Example 2

Input:

0 0

Output:

0

Example 3

Input:

2 3
1 2

Output:

-1

Example notes

The outputs of the three public examples are, in order: 9500; 0; -1. They have been checked by independent enumeration or direct simulation.

Editorial

Loading solution…