//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <deque>
#include <chrono>
#include <cassert>
#include <bitset>
#include <random>
using namespace std;
typedef long long li;
typedef long double ld;
const li MAX = 3e5 + 20;
li inf = (li)1e15;
li mod = (li)998244353;
li dp1[MAX], dp2[MAX], p[MAX], cool[MAX];
li n, k, a[MAX];
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<li> cur;
for (int i = 1; i <= n; i++) {
while (cur.size() && a[cur.back()] < a[i]) cur.pop_back();
if (cur.size()) p[i] = cur.back();
cur.push_back(i);
}
li cur_max = a[1];
dp1[0] = inf;
dp2[0] = inf;
for (int i = 1; i <= n; i++) {
cur_max = max(cur_max, a[i]);
dp1[i] = cur_max;
}
for (int j = 2; j <= k; j++) {
vector<li> cur = { 0 };
for (int i = 1; i <= n; i++) {
dp2[i] = inf;
li uk1 = -1, uk2 = cur.size();
while (uk2 - uk1 > 1) {
li mid = (uk2 + uk1) / 2;
if (cur[mid] >= p[i]) uk2 = mid;
else uk1 = mid;
}
if (uk2 != cur.size()) dp2[i] = min(dp2[i], dp1[cur[uk2]] + a[i]);
while (cur.size() && dp1[cur.back()] > dp1[i]) cur.pop_back();
cur.push_back(i);
}
for (int i = 1; i <= n; i++) {
dp2[i] = min(dp2[i], dp2[p[i]]);
}
for (int i = 0; i <= n; i++) dp1[i] = dp2[i];
}
cout << dp1[n];
}
int main() {
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ios::sync_with_stdio(0);
li q;
q = 1;
while (q--) solve();
return 0;
}
Ly8jcHJhZ21hIEdDQyBvcHRpbWl6ZSgiT2Zhc3QiKQovLyNwcmFnbWEgR0NDIHRhcmdldCgic3NlLHNzZTIsc3NlMyxzc3NlM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2hfYmFjayhpKTsKICAgIH0KICAgIGxpIGN1cl9tYXggPSBhWzFdOwogICAgZHAxWzBdID0gaW5mOwogICAgZHAyWzBdID0gaW5mOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgY3VyX21heCA9IG1heChjdXJfbWF4LCBhW2ldKTsKICAgICAgICBkcDFbaV0gPSBjdXJfbWF4OwogICAgfQogCiAgICBmb3IgKGludCBqID0gMjsgaiA8PSBrOyBqKyspIHsKICAgICAgICB2ZWN0b3I8bGk+IGN1ciA9IHsgMCB9OwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgICAgICBkcDJbaV0gPSBpbmY7CiAgICAgICAgICAgIGxpIHVrMSA9IC0xLCB1azIgPSBjdXIuc2l6ZSgpOwogICAgICAgICAgICB3aGlsZSAodWsyIC0gdWsxID4gMSkgewogICAgICAgICAgICAgICAgbGkgbWlkID0gKHVrMiArIHVrMSkgLyAyOwogICAgICAgICAgICAgICAgaWYgKGN1clttaWRdID49IHBbaV0pIHVrMiA9IG1pZDsKICAgICAgICAgICAgICAgIGVsc2UgdWsxID0gbWlkOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICh1azIgIT0gY3VyLnNpemUoKSkgZHAyW2ldID0gbWluKGRwMltpXSwgZHAxW2N1clt1azJdXSArIGFbaV0pOwogICAgICAgICAgICB3aGlsZSAoY3VyLnNpemUoKSAmJiBkcDFbY3VyLmJhY2soKV0gPiBkcDFbaV0pIGN1ci5wb3BfYmFjaygpOwogICAgICAgICAgICBjdXIucHVzaF9iYWNrKGkpOwogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICAgICAgZHAyW2ldID0gbWluKGRwMltpXSwgZHAyW3BbaV1dKTsKICAgICAgICB9CiAKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8PSBuOyBpKyspIGRwMVtpXSA9IGRwMltpXTsKICAgIH0KICAgIGNvdXQgPDwgZHAxW25dOwp9CiAKaW50IG1haW4oKSB7CiAgICBtdDE5OTM3XzY0IHJuZyhjaHJvbm86OnN0ZWFkeV9jbG9jazo6bm93KCkudGltZV9zaW5jZV9lcG9jaCgpLmNvdW50KCkpOwogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBsaSBxOwogICAgcSA9IDE7CiAgICB3aGlsZSAocS0tKSBzb2x2ZSgpOwogCiAgICByZXR1cm4gMDsKfQ==