#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3,inline")
using ll = long long;
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const double pi = acos(-1);
const int N = 3e5 + 10, K = 18 + 1, SQ = 500, M = 1e9 + 7;
const long long INF = 1e15;
//#define TESTS
//#define INTERACTIVE
//#define COMMUNICATION
/*
* Remember who you are.
*/
void pre() {
}
pair<long double, int> dp[N][2];
int a[N], n, k;
bool ok(long double lambda) {
dp[0][0] = {0, 0}, dp[0][1] = {-INF, 0};
for (int i = 1; i <= n; ++i) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(make_pair(dp[i - 1][1].first + a[i],
dp[i - 1][1].second),
make_pair(dp[i - 1][0].first + a[i] - lambda,
dp[i - 1][0].second + 1));
}
return max(dp[n][0], dp[n][1]).second >= k;
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; ++i)
cin >> a[i];
long double l = 0, r = INF, eps = 1e-3;
while (l + eps < r) {
long double m = (l + r) / 2;
if (ok(m))
l = m;
else
r = m;
}
long double lambda = l;
ok(lambda);
cout << (ll)round(lambda * k + max(dp[n][0], dp[n][1]).first);
}
void solve2() {
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifdef CAROOT
auto start = std::chrono::steady_clock::now();
#ifndef INTERACTIVE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
#else
#endif
int tt = 1;
pre();
#ifdef COMMUNICATION
string run;
cin >> run;
#endif
#ifdef TESTS
cin >> tt;
#endif
for (int tc = 1; tc <= tt; ++tc) {
#ifdef COMMUNICATION
if (run == "first")
solve();
else
solve2();
#else
solve();
#endif
// cout << '\n';
}
#ifdef CAROOT
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
cerr << "Execution time: " << elapsed_seconds.count() << " seconds" << std::endl;
cerr << "Execution time in milliseconds: " << duration_ms.count() << " ms" << std::endl;
#endif
}