#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
    int n, k;
    cin >> n >> k;
    vector<int>a(n);
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    vector<int>pm(n);
    multiset<int>s;
    pm[0] = 0;
    int as = 0;
    for(int i = 1; i < n; i++)
    {
        as += a[i - 1];
        s.insert(a[i - 1]);
        if(s.size() > k - 1)
        {
            as -= *s.begin();
            s.erase(s.begin());
        }
        pm[i] = as;
    }
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        ans = max(ans, pm[i] + i + 1 + a[i]);
    }
    cout << ans << endl;
}