#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    ll ans = 0;
    multiset<int> st;
    ll sums = 0;
    for (int i = 0; i < n; ++i) {
        if (st.size() == k - 1) {
            ans = max(ans, sums + a[i] + k - 1 + (i + 1 - (k - 1)));
        }
        st.emplace(a[i]);
        sums += a[i];
//        cout << sums << endl;
        if (st.size() == k) {
            sums -= *st.begin();
            st.erase(st.begin());
        }
    }
    cout << ans << '\n';

    return 0;
}