#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using P = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rep(x,s,e) for (auto x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e)?x++:x--))
#define sz(x) (int)x.size()
const char nl = '\n';
const int N = 3e5+10;
int b[N], sum[N];
int cep[N], sag[N];
int find(int x) {
if (b[x] == x)return x;
return b[x] = find(b[x]);
}
void solve() {
int n, k; cin >> n >> k;
rep(i, 1, n+1)b[i] = i;
vector<int> a(n);
for (auto &i: a)cin >> i;
vector<int> A, B;
int p = 0, m = 0;
rep(i, 0, n) {
if (a[i] < 0) {
if (p > 0)A.push_back(p), p = 0;
m += a[i];
} else {
if (m < 0) {
if (sz(A))B.push_back(m);
m = 0;
}
p += a[i];
}
}
if (p)A.push_back(p);
int res = accumulate(all(A), 0ll);
//sort(all(B));
set<int> av;
int need = sz(A)-k;
//cout << res << " " << need << nl;
if (need > 0) {
multiset<P> ms;
rep(i, 0, sz(B)) {
ms.insert(P(-B[i], i)),
av.insert(i);
}
rep(i, 0, sz(A)) {
ms.insert(P(A[i], -i-1));
cep[i] = sag[i] = i;
sum[i] = A[i];
}
while (need--) {
auto c = *ms.begin();
res -= c.first;
//cout << c.first << nl;
if (c.second >= 0) { // delete negative between
int x = c.second;
av.erase(av.find(x));
ms.erase(ms.find(P(sum[find(x)], -x-1)));
ms.erase(ms.find(P(sum[find(x+1)], -x-2)));
cep[x+1] = cep[x];
b[x] = b[x+1], sum[x+1] += sum[x]-c.first;
ms.insert(P(sum[x+1], -x-2));
}
else { // delete whole positive
int x = find(-c.second-1);
int y = -1;
auto lb = av.lower_bound(cep[x]);
if (lb != av.begin()) {
y = *--lb;
if (y)ms.erase(ms.find(P(-B[y-1], y-1)));
}
//if (y)ms.erase(ms.find(P(-B[y-1], y-1)));
if (lb != av.end())ms.erase(ms.find(P(-B[*lb], *lb)));
if (~y)av.erase(av.find(y));
if (lb != av.end())av.erase(lb);
//cout << B[z] << nl;
}
ms.erase(ms.begin());
}
}
cout << res << nl;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}