Problem:
Score : 600 points

Problem Statement

We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.

The current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.

Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).

Different values can be chosen for different columns.

Then, you will create the modified artwork by repeating the following operation:

Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)

Find the minimum number of times you need to perform this operation.

Constraints

1 \leq N \leq 300

0 \leq K \leq N

0 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 H_2 ... H_N

Output

Print the minimum number of operations required.

Sample Input 1

4 1
2 3 4 1

Sample Output 1

3

For example, by changing the value of H_3 to 2, you can create the modified artwork by the following three operations:

Paint black the 1-st through 4-th squares from the left in the 1-st row from the bottom.

Paint black the 1-st through 3-rd squares from the left in the 2-nd row from the bottom.

Paint black the 2-nd square from the left in the 3-rd row from the bottom.

Sample Input 2

6 2
8 6 9 1 2 1

Sample Output 2

7

Sample Input 3

10 0
1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000

Sample Output 3

4999999996

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

public static class P
{
    public static void Main()
    {
        var k = Console.ReadLine().Split().Select(int.Parse).Last();
        var heights = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var heightsList = heights.Concat(Enumerable.Repeat(0, 1)).Distinct().OrderBy(x => x).ToArray();
        var heightsDic = heightsList.Select((Elem, Count) => new { Elem, Count }).ToDictionary(x => x.Elem, x => x.Count);

        //dp[今残ってる変えれる回数][今の連鎖してる高さ] := 回数

        var sloped = Enumerable.Repeat(0, k + 1).Select(_ => Enumerable.Repeat(1L << 60, heightsDic.Count).ToArray()).ToArray();
        var flat = Enumerable.Repeat(0, k + 1).Select(_ => Enumerable.Repeat(1L << 60, heightsDic.Count).ToArray()).ToArray();
        sloped[0][0] = 0;
        flat[0][0] = 0;
        var accumedSloped = sloped.Select(_ => new long[heightsDic.Count]).ToArray();
        var accumedDP = flat.Select(_ => new long[heightsDic.Count]).ToArray();
        for (int i = 0; i < sloped.Length; i++)
            MakeFrontAccumlateTable(accumedSloped[i], sloped[i]);
        for (int i = 0; i < flat.Length; i++)
            MakeBackAccumlateTable(accumedDP[i], flat[i]);
        foreach (var height in heights)
        {
            for (int i = 0; i < sloped.Length; i++)
                for (int j = 0; j < sloped[i].Length; j++)
                    sloped[i][j] = 1L << 60;
            for (int i = 0; i < flat.Length; i++)
                for (int j = 0; j < flat[i].Length; j++)
                    flat[i][j] = 1L << 60;

            var heightInd = heightsDic[height];
            
            for (int nextCount = 0; nextCount <= k; nextCount++)
            {
                var val = Min(accumedSloped[nextCount][heightInd] + height, accumedDP[nextCount][heightInd]);
                sloped[nextCount][heightInd] = val - height;
                flat[nextCount][heightInd] = val;
            }

            for (int nextCount = 1; nextCount <= k; nextCount++)
                for (int nextHeightInd = 0; nextHeightInd < heightsDic.Count; nextHeightInd++)
                {
                    var val = Min(accumedSloped[nextCount - 1][nextHeightInd] + heightsList[nextHeightInd], accumedDP[nextCount - 1][nextHeightInd]);
                    if (val - heightsList[nextHeightInd] < sloped[nextCount][nextHeightInd])
                        sloped[nextCount][nextHeightInd] = val - heightsList[nextHeightInd];
                    if (val < flat[nextCount][nextHeightInd])
                        flat[nextCount][nextHeightInd] = val;
                }

            for (int i = 0; i < sloped.Length; i++)
                MakeFrontAccumlateTable(accumedSloped[i], sloped[i]);                
            for (int i = 0; i < flat.Length; i++)
                MakeBackAccumlateTable(accumedDP[i], flat[i]);
        }
        Console.WriteLine(accumedDP.Min(x => x.Min()));
    }

    static void MakeFrontAccumlateTable(long[] target, long[] hoge)
    {
        target[0] = hoge[0];
        for (int i = 1; i < target.Length; i++)
            target[i] = Min(target[i - 1], hoge[i]);
    }
    static void MakeBackAccumlateTable(long[] target, long[] hoge)
    {
        target[target.Length - 1] = hoge[hoge.Length - 1];
        for (int i = target.Length - 2; i >= 0; i--)
            target[i] = Min(target[i + 1], hoge[i]);
    }
}