#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>

#define out(x) std::cout << x << ' ';
#define oute(x) std::cout << x << '\n';
#define enter std::cout << '\n';
#define debout(x) std::cout << #x << x << '\n';
#define debvout(x) std::cout << #x << " : "; for (auto i: x) std::cout << x << ' '; std::cout << '\n';
#define all(x) x.begin(), x.end()
using ll = long long;

std::vector<std::vector<ll>> ind, ansind;
std::vector<ll> a;
ll ans = 0;

void rec(ll i, ll k) {
    if (i == a.size()) {
        ll sum = 0;
        for (ll j = 0; j < k; j++) {
            ll mx = 0;
            if (ind[j].empty()) return;
            for (ll p = 0; p < ind[j].size(); p++) mx = std::max(mx, a[ind[j][p]] + p + 1);
            sum += mx;
        }
        if (sum > ans) ans = sum, ansind = ind;
        return;
    }
    for (ll j = 0; j < k; j++) {
        ind[j].push_back(i);
        rec(i + 1, k);
        ind[j].pop_back();
    }
}

void s1nmeee() {
    ll n, k;
    std::cin >> n >> k;
    a.assign(n, 0);
    ans = 0;
    std::vector<std::pair<ll, ll>> b;
    for (ll i = 0; i < n; i++) {
        std::cin >> a[i];
        b.emplace_back(a[i], i);
    }
    if (k == 1) {
        for (ll i = 0; i < n; i++) ans = std::max(ans, a[i] + i + 1);
        out(ans)
            return;
    }
    std::sort(all(b));
    std::vector<bool> used(n);
    ll min_ind = n - 1, max_ind = 0;
    for (ll i = n - 1; i > n - 1 - (k); i--)
        ans += b[i].first + 1, used[b[i].second] = true, min_ind = std::min(min_ind, b[i].second), max_ind = std::max(max_ind, b[i].second);
    ans += max_ind - k + 1;
    ll ind = n - 1;
    for (ll i = n - 2; i > max_ind; i--) {
        if (a[i] > a[ind] + (ind - i)) ind = i;
    }
    if (!used[n - 1]) {
        ll sum = ans;
        for (ll i = 0; i < n; i++) {
            if (used[i]) {
                ans = std::max(ans, sum - (a[i] + 1) + (a[ind] + (ind - max_ind + 1)));
            }
        }
    }
    
    out(ans);
    /*ans = 0;
    ind.assign(k, {});
    ansind.clear();
    rec(0, k);
    oute(ans);
    for (ll p = 0; p < k; p++) {
        for (auto i: ansind[p]) out(i)
        enter
    }*/
}

signed main() {
    std::cin.tie(nullptr), std::ios::sync_with_stdio(false);
    ll TT = 1;
    // std::cin >> TT;
    while (TT--) {
        s1nmeee();
        std::cout << '\n';
    }
}