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

#define f first
#define s second
#define pb push_back
#define pf push_front
#define in insert
#define oo 1e18
#define nl '\n'
#define orderedSet tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC poison

typedef long double ld;
typedef int64_t int64;
typedef uint64_t uint64;
typedef uint32_t uint;

using namespace std;
using namespace __gnu_pbds;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define int int64

main() {
    cin.tie(0); ios::sync_with_stdio(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    int a[n];
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    int pr[n], sum = 0;
    multiset <int> st;
    for (int i = 0; i < n; ++i) {
        sum += a[i];
        st.in(a[i]);
        if (st.size() > k - 1) {
            sum -= *st.begin();
            st.erase(st.begin());
        }
        pr[i] = (st.size() < k - 1 ? -oo : sum + i + 1);
    }
    int mx = -oo, ans = -oo;
    for (int i = n - 1; i >= 0; --i) {
        if (pr[i] == -oo)
            break;
        if (mx > -oo)
            ans = max(ans, pr[i] + mx - i);
        mx = max(mx, a[i] + i);
    }
    cout << ans;
}
