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

#pragma GCC optimize("O3")
#define ll long long
#define vec vector
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pii pair < int, int >
#define f first
#define s second

using namespace std;

template <typename T>
bool umn (T &a, T b) {return a > b ? (a = b, 1) : 0; }
template <typename T>
bool umx (T &a, T b) {return a < b ? (a = b, 1) : 0; }

const int N = 1e6 + 5;
const int oo = 2e9;
const ll OO = 1e18;
const int mod = 1e9 + 7;

int add (int x, int y) {
    x += y;
    if (x >= mod)
        x -= mod;
    return x >= 0 ? x : x + mod;
}

int mul (int x, int y) {
    return (1ll * x * y) % mod;
}

int bpow (int a, int b) {
    if (b == 0)
        return 1;
    if (b % 2)
        return mul(bpow(a, b - 1), a);
    int t = bpow(a, b / 2);
    return mul(t, t);
}

using namespace __gnu_pbds;

template <typename T>
using oset = tree < T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update >;

int a[N], seg[4 * N];

void update (int v, int l, int r, int i, int x) {
    if (l == r) {
        seg[v] += x;
    } else {
        int m = l + r >> 1;
        if (i <= m)
            update(v << 1, l, m, i, x);
        else
            update(v << 1 | 1, m + 1, r, i, x);
        seg[v] = seg[v << 1] + seg[v << 1 | 1];
    }
}

int get (int v, int l, int r, int x) {
    if (l == r)
        return l;

    int m = l + r >> 1;
    if (seg[v << 1] >= x)
        return get(v << 1, l, m, x);
    else
        return get(v << 1 | 1, m + 1, r, x - seg[v << 1]);

}
int n, k, f = 0;

int get (int x) {
    return get(1, 0, N - 1, x);
}

void update (int i, int x) {
    update(1, 0, N - 1, i, x);
}


void slv () {
    cin >> n >> k;

//    oset < pii >  st;
    ll sumof = 0;
    vec < int > u, b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        b[i] = a[i];
        u.push_back(a[i]);
        u.push_back(a[i] - 1);
//        st.insert({a[i], f++});
    }
    ll best = 0;
    if (k == 2) {
        int le = 0, best1 = 0;
        vec < int > ri(n);
        ri[n - 1] = a[n - 1];
        for (int i = n - 2; i >= 0; --i) {
            ri[i] = max(ri[i + 1], a[i]);
        }
        for (int i = 0; i < n; ++i) {
            umx(best1, a[i] + i + k + max(le, (i + 1 < n ? ri[i + 1] : 0)));

            umx(le, a[i] - 1);
        }
        cout << best1;

        return;
    }
    sort(all(b));
    for (int i = 1; i < k; ++i)
        sumof += b[n - i];
//    for (int i = 1; i < k; ++i)
//        sumof += (*st.find_by_order(sz(st) - i)).f;
    sort(all(u));
    u.erase(unique(all(u)), u.end());
    for (int i = 0; i < n; ++i) {
        a[i] = lower_bound(all(u), a[i]) - u.begin();
        update(1, 0, N - 1, a[i], 1);
    }


    for (int i = 0; i < n; ++i) {
        ll res = u[a[i]] + i + k;
        umx(best, res);
        if (k == 1) continue;

        pii kek = {a[i], 0};
        int el = get(n - k + 2);
        bool qq = 0;
        if (el <= a[i]) {
            sumof -= u[a[i]];
            qq = 1;
        }
        update(a[i], -1);
        int last = get(n - k + 1);
        if (qq)
            sumof += u[last];
//        auto it = st.end();
//        for (int j = 0; j < k - 1; ++j) {
//            --it;
//            res += *it;
//        }
        umx(best, res + sumof);
//        cout << i << " " << sumof << "\n";
        if (a[i] - 1 > last) {
            sumof -= u[last];
            sumof += u[a[i] - 1];
        }
        update(a[i] - 1, 1);
//        st.insert({a[i] - 1, f++});
    }
    cout << best;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) {
        slv();
    }
}
