#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <iomanip>
using namespace std;
using ll = long long;
using ld = long double;
#define endl '\n'
vector<int> f;
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k; cin >> n >> k; vector<int> v(n);
	for (auto& i : v) cin >> i;
	vector<int> mx(n); mx[n - 1] = v[n - 1] + n - 1;
	for (int i = n - 2; i >= 0; i--) mx[i] = max(mx[i + 1], v[i] + i);
	vector<vector<ll>> dp(n + 1, vector<ll>(k + 1, -1e18)); dp[0][0] = 0;
	ll res = 0;
	for (int i = 0; i < n; i++) {
		if (!(i >= k - 1 && mx[i] == v[i] + i)) continue;
		int k1 = k - 1; int j = i; ll sum = v[i] + i + 1;
		while (k1) {
			int j1 = j - 1;
			for (; j1 >= 0 && v[j1] + j1 <= v[j] + j; j1--);
			int maxx = -1, mp = 0;
			for (int p = max(0, j1); p < j; p++) {
				if (v[p] > maxx && p >= k1 - 1) maxx = v[p], mp = p;
			}
			j = mp; k1--; sum += maxx;
		}
		int j1 = j - 1;
		for (; j1 >= 0 && v[j1] + j1 <= v[j] + j; j1--);
		if (j1 >= 0) continue;
		res = max(res, sum);
	}
	cout << res;
	return 0;
}
