// Created by rost on 06.03.25.

#include <bits/stdc++.h>

using ll = long long;
using ld = long double;
using namespace std;

const int INF = 1e9 + 7;

const int MAX_N = 5e5;
int a[MAX_N];

int n;

struct CMP {
    bool operator()(const pair<int, int> &a, const pair<int, int> &b) {
        return a.first > b.first;
    }
};

pair<ll, int> solve(int penalty) {
    vector<pair<ll, int>> dp(n);
    ll curMax = 0;
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        dp[i] = {a[i] + i + 1 - penalty + curMax, cnt + 1};

        if (dp[i].first - i > curMax || (dp[i].first - i == curMax && dp[i].second < cnt)) {
            curMax = dp[i].first - (i + 1);
            cnt = dp[i].second;
        }
    }

    ll ra = dp[n - 1].first;
    int rc = dp[n - 1].second;
    int curMaxE = 0;
    for (int i = n - 2; i >= 0; --i) {
        curMaxE = max(curMaxE, a[i + 1] + i + 2);

        if (dp[i].first + curMaxE - (i + 1) - penalty > ra ||
            (dp[i].first + curMaxE - (i + 1) - penalty == ra && dp[i].second + 1 < rc)) {
            ra = dp[i].first + curMaxE - (i + 1) - penalty;
            rc = dp[i].second + 1;
        }
    }
    return {ra, rc};
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif

    int k;
    cin >> n >> k;
    for (int i = 0; i < n; ++i)
        cin >> a[i];

    int l = 0, r = INF, m;
    while (r - l > 1) {
        m = l + (r - l) / 2;

        auto res = solve(m);

        if (res.second >= k) {
            l = m;
        } else {
            r = m;
        }
    }

    auto [res, c] = solve(l);
    cout << res + l * k << '\n';
}
