#include <iostream>
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int long long
#define st first
#define nd second

/**
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
**/

int a[500'500];

int pow(int a, int n)
{
    int ans = 1;
    while (n)
    {
        if (n & 1)
        {
            ans *= a;
        }
        a *= a;
        n >>= 1;
    }
    return ans;
}

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

    int ans = k - 1;
    for (int i = 0; i < k - 1; ++i)
    {
        ans += a[n - 1 - i];
    }
    for (int i = 0; i < n - k + 1; ++i)
    {
        ans = max(ans, ans + a[i] + i);
    }

    cout << ans << '\n';
    return 0;
}
