All problems

Problem 453

Partition Array for Maximum Sum

Medium · 20 pts · Array, Dynamic Programming

Problem

Split the array into contiguous parts, each of length at most K. After splitting, every element of a part is replaced by the maximum value of that part. Find the largest possible sum of the array after this change. Let best[i] be the answer for the first i elements; the last part ends at position i and has some length L between 1 and K, so try every L.

Input

First line: N and K. Second line: N integers.

Output

Print the maximum sum after partitioning.

Constraints

1 <= K <= N <= 500; 0 <= A[i] <= 1000000000

Example input

7 3
1 15 7 9 2 5 10

Example output

84

Explanation

With K = 3, 1 15 7 9 2 5 10 can be split as [1 15 7] [9] [2 5 10], becoming 15 15 15 9 10 10 10, whose sum is 84.

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

Previous: Russian Doll Envelopes Next: Ones and Zeroes