#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
constexpr char nl = '\n';

signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    int n, k;
    cin >> n >> k;
    vi a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    ll ans = 0;
    ll sm = 0;
    multiset<int> st;
    for (int i = 0; i < n; ++i) {
        if (st.size() == k - 1) {
            ans = max(ans, sm + a[i] + i + 1);
        }
        st.insert(i);
        sm += a[i];
        if (st.size() > k - 1) {
            sm -= *st.begin();
            st.erase(st.begin());
        }
    }
    cout << ans << nl;
}
