import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!int).array;

    auto B = (A ~ 0).dup.sort().uniq.array;
    int[int] comp;
    foreach (i, b; B) comp[b] = i.to!int;
    auto M = B.length.to!int;

    auto dp = new long[][][](N+1, K+1, M+1);
    foreach (i; 0..N+1) foreach (j; 0..K+1) dp[i][j][] = 1L << 59;
    foreach (k; 0..M) dp[0][0][k] = B[k];

    foreach (i; 0..N) foreach (j; 0..K+1) foreach (k; 0..M) {
        int a = comp[A[i]];
        if (a > k) {
            dp[i+1][j][a] = min(dp[i+1][j][a], dp[i][j][k] + B[a] - B[k]);
            if (j < K) dp[i+1][j+1][k] = min(dp[i+1][j+1][k], dp[i][j][k]);
        } else {
            dp[i+1][j][a] = min(dp[i+1][j][a], dp[i][j][k]);
            if (j < K) dp[i+1][j+1][k] = min(dp[i+1][j+1][k], dp[i][j][k]);
        }
    }

    long ans = 1L << 59;
    foreach (j; 0..K+1) foreach (k; 0..M) ans = min(ans, dp[N][j][k]);
    ans.writeln;
}