#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define pii pair<int, int>
#define f first
#define s second
#define ld long double

using namespace std;
using namespace __gnu_pbds;
using oset = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd(time(0));

struct VEC
{
    int x, y;
};

VEC operator +(VEC a, VEC b)
{
    return {a.x+b.x, a.y+b.y};
}
VEC operator -(VEC a, VEC b)
{
    return {a.x-b.x, a.y-b.y};
}
int operator *(VEC a, VEC b)
{
    return a.x*b.x + a.y*b.y;
}
int operator ^(VEC a, VEC b)
{
    return a.x*b.y - a.y*b.x;
}

void solve()
{
    int n, k; cin >> n >> k;
    int a[n]; vector<pii> val;
    for(int i=0; i<n; ++i)
    {
        cin >> a[i];
        val.push_back({a[i], i});
    }
    sort(val.begin(), val.end(), greater<pii>());
    int ans = 0;
    int mx[n] = {0}; pii b[k];
    pii smp = {0, 0};
    fill(b, b+k, smp);
    for(int i = 0; i<k; ++i)
    {
        //ans += val[i].f;
        mx[val[i].s] = 1;
    }
    int cur = 0, local_ = 0, el = 0;
    for(int i=0; i<n; ++i)
    {
        if(cur == k)
        {
            ++el;
            local_ = max(local_, a[i]+el);
        }
        ++b[cur].s;
        if(mx[i])
        {
            b[cur].f = a[i] + b[cur].s;
            ++cur;
        }
    }
    sort(b, b+k);
    int d = 0;
    for(int i=0; i<k; ++i)
    {
        d = max(d, b[i].s + local_ - b[i].f);
    }
    for(int i = 0; i<k; ++i) ans+=b[i].f;
    ans += d;
    cout << ans << "\n";
    return;
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1;
    //freopen("in.txt", "r", stdin); int t; cin >> t;
    //freopen("out.txt", "w", stdout);
    while(t--)
        solve();
    return 0;
}
