// Author : Udynamo
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, std::less<int>, rb_tree_tag,
tree_order_statistics_node_update> pbds;
#define int long long
#define mk make_pair
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define all(x) x.begin(),x.end()
using namespace std;
template<typename T>
istream& operator>>(istream &is, vector<T> &v) {
for (auto &x : v) is >> x;
return is;
}
const int INF = 1e18;
void UNsolve() {
int n, kk;
cin >> n >> kk;
vi a(n);
cin >> a;
auto solve_lambda = [&](int lmb){
pair<int, int> dp[n][2];
dp[0][0] = {0, 0};
dp[0][1] = {a[0] - lmb, 1};
for(int i = 1; i < n; i++){
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(mk(dp[i - 1][1].ff + a[i], dp[i - 1][1].ss),
mk(dp[i - 1][0].ff + a[i] - lmb, dp[i - 1][0].ss + 1));
}
return max(dp[n - 1][0], dp[n - 1][1]);
};
int l = 0, r = 1e18, ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
auto [val, cnt] = solve_lambda(mid);
if(cnt >= kk){
ans = mid;
l = mid + 1;
}else{
r = mid - 1;
}
}
auto [val, cnt] = solve_lambda(ans);
cout << val + kk * ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
UNsolve();
return 0;
}