//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
//#pragma GCC target("avx, avx2, tune=native")
#define int long long
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define pi pair<int, int>


using namespace std;

signed main() {
    ios::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];
    }
    multiset<int> st;
    int s = 0, res = 0;
    for (int i = 0; i < n; ++i) {
        while (st.size() > k - 1) {
            s -= *st.begin();
            st.erase(st.begin());
        }
        st.insert(a[i]);
        s += a[i];
        if (i >= k - 1) {
            res = max(res, s + i + 1);
        }
    }
    cout << res << "\n";
}
