#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;

using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;

ll LNF = 1e18 + 1;


template <typename T>
istream& operator>>(istream & in, vector<T> & a) {
    for (T & x : a) in >> x;
    return in;
}

template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2> & a) {
    in >> a.first >> a.second;
    return in;
}


string SEP = " ";

template <typename T>
ostream& operator<<(ostream & out, vector<T> a) {
    for (T x : a) out << x << SEP;
    return out;
}

template <typename T>
ostream& operator<<(ostream & out, multiset<T> a) {
    for (T x : a) out << x << SEP;
    return out;
}

template <typename T1, typename T2>
ostream& operator<<(ostream & out, pair<T1, T2> a) {
    out << a.first << SEP << a.second << SEP;
    return out;
}


template <typename T1, typename T2>
bool MA(T1 & a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T1, typename T2>
bool MI(T1 & a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}


ll MD(ll a, ll M) {
    if (a >= 0) {
        if (a < M)
            return a;
        a -= M;
        if (a < M)
            return a;
        return a % M;
    }
    a += M;
    if (a >= 0)
        return a;
    return ((a % M) + M) % M;
}


ll BP(ll a) {
    return a * a;
}

ll BP(ll a, ll n) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        ll x = BP(a, n / 2);
        return x * x;
    }
    return BP(a, n - 1) * a;
}

ll BP(ll a, ll n, ll M) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        ll x = BP(a, n / 2, M);
        return MD(x * x, M);
    }
    return MD(BP(a, n - 1, M) * a, M);
}







void CASE(ll CASE_ID) {
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    cin >> a;

    ll ans = 0;
    multiset<ll> ord;
    ll sum = 0;
    for (ll i = 0; i < k - 1; ++i) {
        ord.insert(a[i]);
        sum += a[i];
    }
    ans = sum + a[k - 1];
//    cout << ord << ' ' << ans << '\n';
    for (ll i = k - 1; i + 1 < n; ++i) {
        ord.insert(a[i]);
        sum += a[i];
        sum -= *ord.begin();
        ord.erase(ord.begin());
        MA(ans, sum + a[i + 1] + i + 2);
//        cout << ord << ' ' << sum + a[i + 1] << ' ' << ans << '\n';
    }
    cout << ans;
}


void IOF() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

signed main() {
    IOF();

    bool MULTITEST = (ll)0;
    ll T = 1;
    if (MULTITEST)
        cin >> T;
    for (ll i = 0; i < T; ++i) {
        CASE(i);
        cout << '\n';
    }

    exit('U' ^ 'U');
}

// Powered by: Mi Notes
