#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(1e5)+7;
const ll inf = ll(1e18)+7;
int n , k , a[maxN];
ll pre_dp[maxN] , cur_dp[maxN];
void solve(){
cin >> n >> k;
for (int i = 1 ; i <= n ; i++) cin >> a[i];
cur_dp[0] = +inf;
for (int i = 1 ; i <= n ; i++){
cur_dp[i] = min(cur_dp[i - 1] , 1ll * a[i]);
}
for (int t = 2 ; t <= k ; t++){
for (int i = 1 ; i <= n ; i++){
pre_dp[i] = cur_dp[i];
cur_dp[i] = -inf;
}
deque<int> dq;
for (int i = t ; i <= n ; i++){
while (dq.empty() == 0 && a[dq.back()] >= a[i]){
int j = dq.back();
pre_dp[i - 1] = max(pre_dp[i - 1] , pre_dp[j - 1]);
cur_dp[i] = max(cur_dp[i] , pre_dp[j - 1] + 1ll * a[i]);
dq.pop_back();
}
if (dq.empty() == 0){
int j = dq.back();
cur_dp[i] = max(cur_dp[i] , pre_dp[j - 1] + 1ll * a[j]);
cur_dp[i] = max(cur_dp[i] , cur_dp[j]);
}
cur_dp[i] = max(cur_dp[i] , pre_dp[i - 1] + 1ll * a[i]);
dq.push_back(i);
}
}
cout << cur_dp[n] << "\n";
}
#define name "N"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUoKSkKI2RlZmluZSB1bnE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LSAxXSk7CiAgICAgICAgICAgICAgICBjdXJfZHBbaV0gPSBtYXgoY3VyX2RwW2ldICwgcHJlX2RwW2ogLSAxXSArIDFsbCAqIGFbaV0pOwogICAgICAgICAgICAgICAgZHEucG9wX2JhY2soKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoZHEuZW1wdHkoKSA9PSAwKXsKICAgICAgICAgICAgICAgIGludCBqID0gZHEuYmFjaygpOwogICAgICAgICAgICAgICAgY3VyX2RwW2ldID0gbWF4KGN1cl9kcFtpXSAsIHByZV9kcFtqIC0gMV0gKyAxbGwgKiBhW2pdKTsKICAgICAgICAgICAgICAgIGN1cl9kcFtpXSA9IG1heChjdXJfZHBbaV0gLCBjdXJfZHBbal0pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGN1cl9kcFtpXSA9IG1heChjdXJfZHBbaV0gLCBwcmVfZHBbaSAtIDFdICsgMWxsICogYVtpXSk7CiAgICAgICAgICAgIGRxLnB1c2hfYmFjayhpKTsKICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IGN1cl9kcFtuXSA8PCAiXG4iOwp9CgojZGVmaW5lIG5hbWUgIk4iCgppbnQgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiAgICBpZiAoZm9wZW4obmFtZSIuSU5QIiAsICJyIikpewogICAgICAgIGZyZW9wZW4obmFtZSIuSU5QIiAsICJyIiAsIHN0ZGluKTsKICAgICAgICBmcmVvcGVuKG5hbWUiLk9VVCIgLCAidyIgLCBzdGRvdXQpOwogICAgfQogICAgaW50IHQgPSAxOyAvL2NpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgc29sdmUoKTsKICAgIHJldHVybiAwOwp9Cgo=