#include <bits/stdc++.h>

using namespace std;

#define int long long
using ld = long double;
using ll = long long;
using pii = pair<int, int>;

struct nmset
{
    map<int, int> cnt;
    int sz = 0;
    void insert(int el)
    {
        ++cnt[el];
        ++sz;
    }
    void erase(int el)
    {
        --cnt[el];
        --sz;
    }
    int mi()
    {
        return cnt.begin()->first;
    }
};

void solve()
{
    nmset se;
    int su = 0;
    int ans = 0;

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }

    if (k == 1)
    {
        for (int i = 0; i < n; ++i)
        {
            ans = max(ans, a[i] + i + 1);
        }
        cout << ans << '\n';
        return;
    }

    int s = k - 1;
    for (int i = 0; i < n; ++i)
    {
        if (se.sz == s)
        {
            int var = su + a[i] + i + 1;
            ans = max(ans, var);
        }

        if (se.sz < s)
        {
            se.insert(a[i]);
            su += a[i];
        }
        else
        {
            int mi = se.mi();
            if (a[i] > mi)
            {
                su -= mi;
                su += a[i];
                se.erase(mi);
                se.insert(a[i]);
            }
        }
    }
    cout << ans << '\n';
}

signed main()
{
#ifdef ROMALOCAL
    freopen("in.txt", "r", stdin);
#endif

    cin.tie(0);
    ios::sync_with_stdio(0);

    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}