#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

//#pragma GCC optimize("Ofast")

#define int long long

struct SegmentTree {
    vector <int> t;
    int n;

    SegmentTree(int n) {
        this->n = n;
        t.resize(4 * n);
    }

    void add(int v, int l, int r, int pos, int x) {
        t[v] += x;
        if (l + 1 == r) {
            return;
        }
        int m = (l + r) / 2;
        if (pos < m) {
            add(v * 2 + 1, l, m, pos, x);
        } else {
            add(v * 2 + 2, m, r, pos, x);
        }
    }

    void add(int pos, int x) {
        add(0, 0, n, pos, x);
    }

    int get(int v, int l, int r, int ql, int qr) {
        if (qr <= l || r <= ql) return 0;
        if (ql <= l && r <= qr) return t[v];
        int m = (l + r) / 2;
        return
            get(v * 2 + 1, l, m, ql, qr) +
            get(v * 2 + 2, m, r, ql, qr);
    }

    int get(int ql, int qr) {
        return get(0, 0, n, ql, qr);
    }
};

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, k;
	cin >> n >> k;
	vector <int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
    if (k == 1) {
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans = max(ans, a[i] + 1 + i);
        }
        cout << ans;
        return 0;
    }
    if (k == 2) {
        int pmx = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans = max(ans, pmx + a[i] + i + 1);
            pmx = max(pmx, a[i]);
        }
        cout << ans;
        return 0;
    }
	vector <int> ind(n);
	map <int, vector <int>> mapa;
	for (int i = 0; i < n; i++) {
		mapa[a[i]].push_back(i);
	}
	int T = 0;
	for (auto& [x, vec] : mapa) {
		for (auto i : vec) {
			ind[i] = T++;
		}
	}
	//multiset <int> st;
	int answer = a[0] + 1;
	SegmentTree derosum(n);
    derosum.add(ind[0], a[0]);
	ordered_set.insert(-ind[0]);

	for (int i = 1; i < n; i++) {
		int curr = i + 1 + a[i];
		int r = -*ordered_set.find_by_order(k - 2);

		curr += derosum.get(r, n);
		answer = max(answer, curr);
		derosum.add(ind[i], a[i]);
        ordered_set.insert(-ind[i]);
	};
	cout << answer;
}
