#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long

template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct Node {
    int sum;
    Node (int sum): sum(sum) {}
    Node () {}
};

struct Segment_tree {
    int n;
    vector<Node> t;

    //void insert
};


signed main() {
    int n, k;
    cin >> n >> k;
    vector<int> vec(n);
    for (auto &i: vec) cin >> i;
    vector<int> dp(n);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> qq;
    dp[0] = vec[0] + 1;
    qq.push({vec[0] + 1, -1});
    for (int i = 1; i < k; i++) {
        dp[i] = vec[i] + 1 + dp[i - 1];
        qq.push({vec[i] + 1, -1});
    }
    int unused = 0;
    for (int i = k; i < n; i++) {
        auto [mn, col] = qq.top();
        col = abs(col);
        qq.pop();
        if (vec[i] + col + 1 + unused > mn) {
            qq.push({vec[i] + col + 1 + unused, -(col + 1)});
            dp[i] = dp[i - 1] - mn + vec[i] + col + 1 + unused;
            unused = 0;
        }
        else {
            unused++;
            qq.push({mn, -col});
            dp[i] = dp[i - 1];
        }
    }
    cout << dp[n - 1];
    return 0;
}
