All problems

Problem 966

Minimum Difficulty of a Job Schedule

Hard · 30 pts · Array, Dynamic Programming

Problem

Jobs must be done in the given order over exactly D days, with at least one job per day. The difficulty of a day is the largest difficulty among its jobs, and the difficulty of the schedule is the sum over the days. Find the minimum schedule difficulty, or -1 if there are fewer jobs than days. Let best[d][i] be the minimum for the first i jobs in d days, trying every possible set of jobs for the last day while tracking its maximum.

Input

The first line contains N and D. The second line contains the N job difficulties.

Output

Print the minimum difficulty, or -1.

Constraints

1 <= N <= 300; 1 <= D <= 10; 0 <= difficulty <= 1000

Example input

6 2
6 5 4 3 2 1

Example output

7

Explanation

With jobs 6 5 4 3 2 1 and D = 2, do 6 5 4 3 2 on day 1 (difficulty 6) and 1 on day 2 (difficulty 1), for a total of 7, the minimum.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Sorted Vowel Strings Next: Number of Music Playlists