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

using namespace std;
using namespace __gnu_pbds;

void fast_in_out() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

#define int long long
#define all(x) (x).begin(), (x).end()
using ll = long long;
using ull = long long;
using ld = long double;
typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 5e5;
int a[N], n, k;
multiset<int> st;
int sm = 0;

void solve() {
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    int ans = 0;
    for (int i = 0; i < k - 1; ++i) {
        st.insert(a[i]);
        sm += a[i];
    }
    for (int i = k - 1; i < n; ++i) {
        ans = max(ans, sm + a[i] + i + 1);
        if (k > 1 && *st.begin() < a[i]) {
            sm += a[i] - *st.begin();
            st.insert(a[i]);
            st.extract(st.begin());
        }
    }
    cout << ans;
}

signed main() {
    fast_in_out();
    int qwe = 1;
//    cin >> qwe;
    while (qwe--) {
        solve();
    }
    return 0;
}
