import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto nk = readln.split.to!(int[]);
    auto N = nk[0];
    auto K = nk[1];
    auto HS = new long[](N);
    long[] hc;
    bool[long] hcf;
    auto rev_h = new long[](N);
    foreach (i, h; readln.split.to!(long[])) {
        HS[i] = h;
        if (h !in hcf) {
            hcf[h] = true;
            hc ~= h;
        }
        foreach (j; 0..hc.length) if (hc[j] == h) {
            rev_h[j] = h;
            break;
        }
    }
    int zero;
    if (0 !in hcf) {
        zero = hc.length.to!int;
        hc ~= 0;
        rev_h ~=  0;
    } else {
        foreach (i; 0..hc.length) if (hc[i] == 0) {
            zero = i.to!int;
            break;
        }
    }
    
    auto DP = new long[][][](N, K+1, hc.length);
    long solve(int i, int k, int p) {
        if (i == N) return hc[p];
        if (DP[i][k][p] == 0) {
            long min_c = long.max;
            foreach (j; 0..hc.length) {
                if (k == 0 && HS[i] != rev_h[j]) continue;
                auto d = hc[p] - hc[j];
                if (d < 0) d = 0;
                min_c = min(min_c, solve(i+1, k - (HS[i] == rev_h[j] ? 0 : 1), j.to!int) + d);
            }
            DP[i][k][p] = min_c;
        }
        return DP[i][k][p];
    }
    writeln(solve(0, K, zero));
}