#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define allv(x) (x).begin(), (x).end()
#define allv_rev(x) (x).rbegin(), (x).rend()
#define lolset(x) memset(x, 0, sizeof (x))
#define lolfill(x, v) fill(x, x + sizeof(x) / sizeof(*x), v)
#define sz(x) ((int)(x).size())
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
template<typename T, typename T2>
bool smin(T &a, T2 const& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename T2>
bool smax(T &a, T2 const& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;


template<typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (T &i : v)
        is >> i;
    return is;
}




#ifdef DEBUG
const ll MAXN = 0, INF = 1e6;
#else
const ll MAXN = 0, INF = 1e9 + 1e6 * 228;
#define cerr if (0) cerr
#endif



signed one() {
    int n, k;
    cin >> n >> k;
    multiset<ll> s;
    vector<ll> a(n);
    cin >> a;
    ll sum_s = 0;
    for (int i = 0; i < k - 1; i++) {
        s.insert(a[i]);
        sum_s += a[i];
    }
    ll ans = 0;
    for (int i = k - 1; i < n; i++) {
        if (i > k - 1) {
            s.insert(a[i - 1]);
            sum_s += a[i - 1];
        }
        while (sz(s) > k - 1) {
            sum_s -= *s.begin();
            s.erase(s.begin());
        }
        smax(ans, sum_s + a[i] + i + 1);
    }
    cout << ans << endl;
    return 0;
}



signed main() {
#ifndef DEBUG
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    signed t;
    t = 1;
    while (t--) {
        signed retcode = one();
        if (retcode) return retcode;
    }
    return 0;
}
