#include <bits/stdc++.h>
#include "ext/pb_ds/assoc_container.hpp"

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;

#define all(a) a.begin(), a.end()

using ordered_set = tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;

void solve();

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
#ifdef _GLIBCXX_DEBUG
    freopen("input", "r", stdin);
#endif
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
}

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int &i: a)
        cin >> i;
    multiset<int> s;
    ll sum = 0;
    for (int i = 0; i + 1 < k; i++) {
        s.insert(a[i]);
        sum += a[i];
    }
    ll ans = 0;
    for (int i = k - 1; i < n; i++) {
        ans = max(ans, sum + a[i] + i + 1);
        if (*s.begin() < a[i]) {
            sum -= *s.begin();
            s.erase(s.begin());
            s.insert(a[i]);
            sum += a[i];
        }
    }
    cout << ans << '\n';
}