// Created by rost on 06.03.25.

#include <bits/stdc++.h>

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

const int MAX_N = 5e5;

int n;
int a[MAX_N + 1];

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

        if (dp[i].first - i > prefMax ||
            (dp[i].first - i == prefMax && dp[i].second > prefCnt)) {
            prefMax = dp[i].first - i;
            prefCnt = dp[i].second;
        }
    }

    ld res = dp[n].first;
    int resCnt = dp[n].second;

    int sufMax = a[n] + n;
    for (int i = n - 1; i > 0; --i) {
        if (dp[i].first + sufMax - i - penalty > res ||
            (dp[i].first + sufMax - i - penalty == res && dp[i].second + 1 > resCnt)) {
            res = dp[i].first + sufMax - i - penalty;
            resCnt = dp[i].second + 1;
        }

        if (a[i] + i > sufMax)
            sufMax = a[i] + i;
    }
    return {res, resCnt};
}

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 = 1; i <= n; ++i)
        cin >> a[i];

    ld l = -1e12, r = 1e12, lambda;
    for (int i = 0; i < 100; ++i) {
        lambda = (l + r) / 2;

        auto res = solve(lambda);

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

    auto res = solve(r);
    cout << ll(res.first + r * k) << '\n';
}