cswork / OAOpen practice workspace

SnowflakeOA #25

Efficient Cost

Estimated MediumJudge-ready

Topics (AI-labelled)DP Basics

Cut the entire integer array, keeping its original order, into non-empty contiguous segments, each of length ≤threshold; the cost of a segment is its maximum value. Find the minimum total cost over all segments. Negative integers are allowed, and segment lengths are not forced to equal the limit.

Input

The first line contains n threshold, and the second line contains the array. This site adds 1≤threshold≤n≤5000 and −10⁹≤element≤10⁹.

Output

Output the single integer required by the problem.

Example 1

Input:

6 3
1 3 4 5 2 6

Output:

10

Example 2

Input:

3 3
-1 -2 -3

Output:

-6

Example 3

Input:

4 3
10 1 1 10

Output:

20

Example notes

The answers to the three public examples are, in order: 10; -6; 20. The examples were rechecked by independent enumeration, state search, or direct simulation.

Editorial

Loading solution…