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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.NoSuchElementException;

public class Main {

	void run(int N, int K, long[] H) {
		HashSet<Long> set = new HashSet<Long>();
		HashMap<Long, Integer> map = new HashMap<Long, Integer>();
		for (long h : H) {
			set.add(h);
		}
		set.add(0L);
		long[] hs = new long[set.size()];
		int p = 0;
		for (long h : set) {
			hs[p++] = h;
		}
		Arrays.sort(hs);
		for (int i = 0; i < hs.length; ++i)
			map.put(hs[i], i);
		long[][][] dp = new long[N + 1][set.size()][K + 1];
		for (int i = 0; i < N; ++i)
			for (int j = 0; j < set.size(); ++j)
				for (int k = 0; k <= K; ++k)
					dp[i][j][k] = Long.MAX_VALUE / 3;
		dp[0][map.get(H[0])][0] = H[0];
		for (int j = 0; j < set.size() && 1 <= K; ++j) {
			dp[0][j][1] = Math.min(dp[0][j][1], hs[j]);
		}
		for (int i = 1; i < N; ++i) {
			// Kを使用
			int me = map.get(H[i]);
			for (int k = 0; k + 1 <= K; ++k) {
				long mi = dp[i - 1][set.size() - 1][k];
				for (int j = set.size() - 1; j >= map.get(H[i]); --j) {
					mi = Math.min(mi, dp[i - 1][j][k]);
					dp[i][j][k + 1] = Math.min(dp[i][j][k + 1], mi);
				}

				mi = 0;
				for (int j = 0; j <= map.get(H[i]); ++j) {
					if (dp[i - 1][(int) mi][k] + Math.max(0, hs[j] - hs[(int) mi]) >= dp[i - 1][j][k])
						mi = j;
					dp[i][j][k + 1] = Math.min(dp[i][j][k + 1],
							dp[i - 1][(int) mi][k] + Math.max(0, hs[j] - hs[(int) mi]));
				}
			}
			// Kを使用しない
			for (int k = 0; k <= K; ++k) {
				for (int j = 0; j < set.size(); ++j) {
					dp[i][me][k] = Math.min(dp[i][me][k], dp[i - 1][j][k] + Math.max(0, hs[me] - hs[j]));
				}
			}
		}
		long ans = Long.MAX_VALUE / 3;
		for (int k = 0; k <= K; ++k) {
			for (int j = 0; j < set.size(); ++j) {
				ans = Math.min(ans, dp[N - 1][j][k]);
			}
		}
		System.out.println(ans);
	}

	public static void main(String[] args) throws FileNotFoundException {
		FastScanner sc = new FastScanner();
		int N = sc.nextInt();
		int K = sc.nextInt();
		long[] H = new long[N];
		for (int i = 0; i < N; ++i)
			H[i] = sc.nextLong();
		new Main().run(N, K, H);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}

class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}

	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}

	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
		return hasNextByte();
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}

	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) nl;
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}