#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

#define int int64_t
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ld long double

const int maxn = 2e5;
const int LOG = 30;
const int N = 2e3;
const ld eps = 1e-12;

inline void solve() {
    int n, k;
    cin >> n >> k;
    int sm = 0;
    multiset<int> st;
    int ans = 0;
    for(int i = 0; i < n; i++) {
        int x;
        cin >> x;
        ans = max(ans, x + sm + (i + 1));
        st.insert(x);
        sm += x;
        while(st.size() > (k - 1)) {
            sm -= *st.begin();
            st.erase(st.begin());
        }
    }
    cout << ans << '\n';
    return;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int tt = 1;
    //cin >> tt;

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    while(tt--) {
        solve();
    }
    return 0;
}
