#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Node {
    ll cnt, sum;
};

Node merge(Node &a, Node &b) {
    Node res{};
    res.cnt = a.cnt + b.cnt;
    res.sum = a.sum + b.sum;
    return res;
}

struct SegmentTree {
    vector<Node> tree;
    vector<ll> arr;
    ll n;

    explicit SegmentTree(vector<ll> &a) : arr(a) {
        n = a.size();
        tree.assign(4 * n, {0, 0});
    }

    void update(ll v, ll l, ll r, ll ind) {
        if (ind < l || r <= ind) {
            return;
        }
        if (l == r - 1) {
            tree[v].cnt++;
            tree[v].sum += arr[l];
            return;
        }
        ll m = (l + r) / 2;
        update(2 * v + 1, l, m, ind);
        update(2 * v + 2, m, r, ind);
        tree[v] = merge(tree[2 * v + 1], tree[2 * v + 2]);
    }

    void update(ll ind) {
        update(0, 0, n, ind);
    }

    ll get(ll v, ll l, ll r, ll k) {
        if (l == r - 1) {
            return tree[v].sum;
        }
        ll m = (l + r) / 2;
        if (tree[2 * v + 2].cnt >= k) {
            return get(2 * v + 2, m, r, k);
        } else {
            return get(2 * v + 1, l, m, k - tree[2 * v + 2].cnt) + tree[2 * v + 2].sum;
        }
    }

    ll get(ll k) {
        return get(0, 0, n, k);
    }
};

int main() {
#ifdef DEBUG_Q
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    if (k == 1) {
        ll ans = 0;
        for (ll i = 0; i < n; i++) {
            ans = max(ans, a[i] + i + 1);
        }
        cout << ans;
        return 0;
    }
    vector<ll> a1 = a;
    sort(a1.begin(), a1.end());
    a1.resize(unique(a1.begin(), a1.end()) - a1.begin());
    map<ll, ll> mp;
    for (ll i = 0; i < a1.size(); i++) {
        mp[a1[i]] = i;
    }
    ll ans1 = 0;
    SegmentTree tree(a1);
    for (ll i = 0; i < k - 1; i++) {
        tree.update(mp[a[i]]);
    }
    for (ll i = k - 1; i < n; i++) {
        ll ans = tree.get(k - 1);
        ans += a[i] + i + 1;
        ans1 = max(ans1, ans);
        tree.update(mp[a[i]]);
    }
    cout << ans1;
}
