//#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using ll = long long;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

//.find_by_order(1); по индексу
//.order_of_key(1);

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

void solve();

int32_t main() {
#ifdef LOCAL
    freopen("Input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
}

constexpr int MAXN = 1e3 + 3;
constexpr ll INF = 1e18;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    priority_queue<ll> st;
    ll sum = 0;
    for (int i = 0; i < k - 1; ++i) {
        st.push(-a[i]);
        sum += a[i];
    }
    ll ans = 0;
    for (int i = k - 1; i < n; ++i) {
        ans = max(ans, a[i] + sum + (i + 1));
        if (a[i] > -st.top()) {
            sum -= -st.top();
            st.pop();
            st.push(-a[i]);
            sum += a[i];
        }
    }
    cout << ans << '\n';
}