#include<iostream>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<random>
#include<chrono>
#include<algorithm>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define int long long

signed main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    cout.precision(20);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> v(n);
    int ans = 0, cur = 0;
    multiset<int> st;
    for (int i = 0; i < n; i++) {
        cin >> v[i];
        if ((int)st.size() >= k - 1) {
            ans = max(ans, cur + v[i] + i - (k - 1) + 1);
        }
        cur += v[i] + 1;
        st.insert(v[i] + 1);
        if ((int)st.size() > k - 1) {
            cur -= *st.begin();
            st.erase(st.find(*st.begin()));
        }
    }
    cout << ans << '\n';
}#include<iostream>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<random>
#include<chrono>
#include<algorithm>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define int long long

signed main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    cout.precision(20);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> v(n);
    int ans = 0, cur = 0;
    multiset<int> st;
    for (int i = 0; i < n; i++) {
        cin >> v[i];
        if ((int)st.size() >= k - 1) {
            ans = max(ans, cur + v[i] + i - (k - 1) + 1);
        }
        cur += v[i] + 1;
        st.insert(v[i] + 1);
        if ((int)st.size() > k - 1) {
            cur -= *st.begin();
            st.erase(st.find(*st.begin()));
        }
    }
    cout << ans << '\n';
}