// 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;

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

    int resI = 0;
    for (int i = 0; i < n; ++i) {
        if (dp[i].first > dp[resI].first ||
            (dp[i].first == dp[resI].first && dp[i].second < dp[resI].second))
            resI = i;
    }
    return dp[resI];
}

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';
}
