#include <bits/stdc++.h>

using ll = long long;
using namespace std;
int mod = 1e9 + 7;
struct mod_int {
    int v = 0;
    mod_int(int nv = 0) {
        v = nv;
        if (v < 0 || v >= mod) {
            v = (v % mod + mod) % mod;
        }
    }
};
mod_int operator + (mod_int v1, mod_int v2) {
    return (v1.v + v2.v >= mod ? v1.v + v2.v - mod : v1.v + v2.v);
}
mod_int operator -(mod_int v1, mod_int v2) {
    return (v1.v - v2.v < 0 ? v1.v - v2.v + mod : v1.v - v2.v);
}
mod_int operator *(mod_int v1, mod_int v2) {
    return (1LL * v1.v * v2.v) % mod;
}
bool operator == (mod_int v1, mod_int v2) {
    return v1.v == v2.v;
}
bool operator < (mod_int v1, mod_int v2) {
    return v1.v < v2.v;
}

//void solve() {
//    ll n, q;
//    cin >> n >> q;
//    vector <vector <ll>> gr(n);
//    vector <ll> prev(n, - 1);
//    for (ll i = 1; i < n; ++i) {
//        ll p;;
//        cin >> p;
//        --p;
//        prev[i] = p;
//        gr[p].push_back(i);
//    }
//    vector <ll> val(n);
//    for (ll i = 0; i < n; ++i) {
//        cin >> val[i];
//    }
//    set <pair <ll, ll>> now{};
//    now.emplace(- val[0], 0);
//    vector <ll> ind(n);
//    ll time = 0;
//    vector <ll> p{};
//    while (!now.empty()) {
//        ll num = now.begin()->second;
//        p.push_back(num);
//        ind[num] = time;
//        ++time;
//        now.erase(now.begin());
//        for (auto next : gr[num]) {
////            cout << next + 1 << ' ' << num << '\n';
//            now.insert({- val[next], next});
//        }
//    }
////    for (auto num : p) {
////        cout << num + 1 << ' ';
////    }
////    cout << '\n';
//    vector <ll> ans = ind;
//    vector <ll> counter(n, 0);
//    for (ll num : p) {
//        ll cur = prev[num], last = num;
//        while (cur != - 1) {
//            for (auto next : gr[cur]) {
//                if (next != last) {
////                    cout << num + 1 << " " << next + 1 << '\n';
//                    ans[num] = min(ans[num], ind[num] - counter[next]);
//                }
//            }
//            cur = prev[cur];
//            last = prev[last];
//        }
//        cur = num;
//        while (cur != - 1) {
//            ++counter[cur];
//            cur = prev[cur];
//        }
//    }
//    for (ll i = 0; i < q; ++i) {
//        ll num;
//        cin >> num;
//        --num;
//        cout << ans[num] << ' ';
//    }
//    cout << '\n';
//}
void solve() {
    ll n, k;
    cin >> n >> k;
    vector <ll> val(n);
    for (ll i = 0; i < n; ++i) {
        cin >> val[i];
    }
    if (k == 1) {
        ll ans = 0;
        for (ll i = 0; i < n; ++i) {
            ans = max(ans, val[i] + i + 1);
        }
        cout << ans << '\n';
        return;
    }
    ll ans = 0;
    ll sum = 0;
    multiset <ll> now{};
    for (ll i = 0; i < k - 1; ++i) {
        now.insert(val[i]);
        sum += val[i];
    }
    for (ll i = k - 1; i < n; ++i) {
//        cout << sum << " " << val[i] << ' ' << i << '\n';
        ans = max(ans, i + 1 + sum + val[i]);
        auto it = now.lower_bound(val[i]);
        if (it != now.begin()) {
            it = now.begin();
            sum -= *it;
            now.erase(it);
            sum += val[i];
            now.insert(val[i]);
        }
    }
    cout << ans << '\n';
}
signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t = 1;
//    cin >> t;
    for (int i = 0; i < t; ++i) {
        solve();
    }
}
