#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int maxn = 5e5 + 10;
vector<int> lst(maxn, 0);
int n, k;
int final_ans;




void perebor(int ind, vector<int> some, int cou) {
    if (ind >= n) {
        if (cou != k) {
            return;
        }
        vector<vector<int>> wow(cou + 1, vector<int>());
        for (int i = 0; i < n; i++) {
            wow[some[i]].push_back(lst[i]);
        }
        int ans = 0;
        for (int i = 0; i <= cou; i++) {
            int loc_ans = 0;
            for (int j = 0; j < wow[i].size(); j++) {
                loc_ans = max(loc_ans, wow[i][j] + j + 1);
            }
            ans += loc_ans;
        }
        final_ans = max(final_ans, ans);
    } else {
        for (int i = 0; i <= cou; i++) {
            some.push_back(i);
            if (i == cou) {
                perebor(ind + 1, some, cou + 1);
            } else {
                perebor(ind + 1, some, cou);
            }
            some.pop_back();
        }
    }
}


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(15);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> lst[i];
    }
    final_ans = 0;
    int rt = 0;
    vector<int> fun(n, 0);
    long long summ = 0;
    multiset<int> some;
    for (int i = 0; i < n; i++) {
        if (i < k - 1) {
            summ += lst[i];
            some.insert(lst[i]);
        } else {
            if (lst[i] > *some.begin()) {
                summ -= *some.begin();
                some.erase(some.begin());
                some.insert(lst[i]);
                summ += lst[i];
            }
        }
        if (i + 1 >= k - 1) {
            fun[i] = summ;
        }
    }
    for (int i = n - 1; i >= 1; --i) {
        int new_ans = lst[i] + n - rt + fun[i - 1];
        final_ans = max(final_ans, new_ans);
        rt++;
    }
    cout << final_ans << endl;
}
