cswork / OAOpen practice workspace

SalesforceOA #39

Split Array Largest Sum

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. Note that this is not the similarly named problem of minimizing the largest segment sum. 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 full original bounds are kept: 0≤n,k≤300, 0≤a[i]≤100000. 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 2
1 2 3 4 5

Output:

6

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: 6; 0; -1. They have been checked by independent enumeration or direct simulation.

Editorial

Loading solution…