#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <deque>
#include <string>

#include <algorithm>
#include <functional>
#include <bitset>
#include <functional>
#include <chrono>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1000000007ll;
const i64 smod = 998244353ll;
const i64 inf = 10000000000000007ll;

const double eps = 1e-10;

const i64 MAXN = 303;

i64 n, K;

i64 a[MAXN];
i64 dp[MAXN][MAXN][MAXN];

map<i64, i64> w;
i64 z[MAXN];

pair<i64, i64> postmin[MAXN];
pair<i64, i64> premin[MAXN];

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed);

    cin >> n >> K;
    set<i64> q;
    for (i64 i = 1; i <= n; i++) {
        cin >> a[i];
        q.insert(a[i]);
    }

    q.insert(0);

    i64 mi = 0;
    for (auto t : q) {
        w[t] = ++mi;
    }

    for (auto t : w) {
        z[t.second] = t.first;
    }


    for (i64 k = 0; k <= K; k++) {
        for (i64 i = 0; i <= n; i++) {
            for (i64 hc = 0; hc <= mi + 1; hc++) {
                dp[k][i][hc] = inf;
            }
        }
    }


    premin[0] = { inf, 0 };
    premin[mi + 1] = { inf, 0 };

    postmin[0] = { inf, 0 };
    postmin[mi + 1] = { inf, 0 };


    dp[0][0][1] = 0;


    /*
    for (auto hc : q) {
    i64 k = hc == a[1] ? 0 : 1;
    dp[k][1][hc] = hc;
    dp[k][1][hc] = hc;
    }
    */

    /*
    for (i64 i = 1; i <= n; i++) {
    for (auto hc : q) {
    for (auto hp : q) {
    for (i64 tk = 0; tk <= min(i - 1, K); tk++) {
    i64 k = tk + (hc == a[i] ? 0 : 1);
    i64 t = dp[tk][i - 1][hp] + max(0ll, hc - hp);
    dp[k][i][hc] = min(dp[k][i][hc], t);
    }
    }
    }
    }
    */

    for (i64 i = 1; i <= n; i++) {
        for (i64 k = 0; k <= min(i - 1, K); k++) {
            for (i64 j = mi; j >= 1; j--) {
                pair<i64, i64> t = { dp[k][i - 1][j], j };
                if (postmin[j + 1].first < t.first) {
                    postmin[j] = postmin[j + 1];
                }
                else {
                    postmin[j] = t;
                }
            }

            for (i64 j = 1; j <= mi; j++) {
                pair<i64, i64> t = { dp[k][i - 1][j] + z[mi] - z[j], j };
                if (premin[j - 1].first < t.first) {
                    premin[j] = premin[j - 1];
                }
                else {
                    premin[j] = t;
                }
            }

            for (i64 hc = 1; hc <= mi; hc++) {
                i64 f = z[hc] == a[i] ? k : k + 1;

                i64 postj = postmin[hc].second;
                i64 postv = dp[k][i - 1][postj];

                i64 prej = premin[hc].second;
                i64 prev = dp[k][i - 1][prej] + z[hc] - z[prej];

                dp[f][i][hc] = min(postv, prev);
            }
        }
    }

    i64 R = inf;
    for (i64 hc = 1; hc <= mi; hc++) {
        R = min(R, dp[K][n][hc]);
    }
    cout << R << endl;

    return 0;
}


Measured memory_bytes: 217344