#include <bits/stdc++.h>
struct dyn_data {
int value, first, second;
bool operator < (const dyn_data& other) const {
return value > other.value;
}
};
#define MAXN 10005
int N, K, Val[MAXN];
dyn_data V[MAXN];
int Modulo(int X) {
return (X-1)%N + 1;
}
std::ifstream In ("ferma.in");
std::ofstream Out("ferma.out");
void Citire() {
In >> N >> K;
}
bool Mark[MAXN];
void Rezolvare() {
for (int i=1, X; i<=N; ++i) {
In >> X;
Val[i] = X;
if (V[i-1].value > 0) {
V[i] = {V[i-1].value + X, V[i-1].first, i};
continue;
} V[i] = {X, i, i};
}
int idx = 1;
while (Val[idx] > 0 && idx < V[N].first) idx ++;
idx--;
if (idx) V[N] = {V[N].value + V[idx].value, V[N].first, idx + N};
std::sort(V+1, V+N+1);
// Euristica ce nu merge de fapt..
int Ans = 0;
for (int i=1, cnt=0, X, Y; i<=N && cnt<K; ++i) {
X = V[i].first;
Y = V[i].second;
if (!Mark[X]) {
cnt++;
Ans = std::max(Ans, Ans + V[i].value);
if (Y > N) {
for (int i=X; i<=N; ++i)
Mark[i] = true;
for (int i=1; i<=Y-N; ++i)
Mark[i] = true;
}
else Mark[X] = Mark[Y] = true;
}
} Out << Ans << '\n';
}
int main()
{
Citire();
Rezolvare();
return 0;
}