#P5424. [USACO19OPEN] Snakes G

[USACO19OPEN] Snakes G

Problem Description

Legend says that thousands of years ago, Saint Patrick got rid of all the snakes in MooLand. However, the snakes have now returned! Saint Patrick’s Day is on March 17 every year, so Bessie wants to celebrate it by completely clearing MooLand of all snakes.

Bessie is equipped with a net to catch NN groups of snakes lined up in a row (1N4001 \leq N \leq 400). Bessie must catch all the snakes in each group in the order the groups appear in the line. Each time Bessie finishes catching one group of snakes, she puts them into cages, and then starts catching the next group with an empty net.

A net of size ss means that Bessie can catch any group containing gg snakes, where gsg \leq s. However, whenever Bessie uses a net of size ss to catch a group of gg snakes, it wastes sgs - g space. Bessie may choose the initial size of her net freely, and she may change the net size KK times (1K<N1 \leq K < N).

Please tell Bessie the minimum possible total wasted space after she catches all groups of snakes.

Input Format

The first line of input contains NN and KK.

The second line contains NN integers a1,,aNa_1, \ldots, a_N, where aia_i (0ai1060 \leq a_i \leq 10^6) is the number of snakes in the ii-th group.

Output Format

Output one integer: the minimum total wasted space for Bessie to catch all the snakes.

6 2
7 9 8 2 3 2
3

Hint

Bessie can set her net to have an initial size of 77. After she catches the first group of snakes, she changes her net size to 99, keeps this size until she catches the 33-rd group, and then changes the net size to 33. The total wasted space is (77)+(99)+(98)+(32)+(33)+(32)=3(7-7)+(9-9)+(9-8)+(3-2)+(3-3)+(3-2)=3.

Translated by ChatGPT 5