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

using namespace __gnu_pbds;
using namespace std;
using ordered_set = tree<long long, null_type, less_equal<>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ld = long double;
using str = string;
#define all(a) a.begin(), a.end()
#define len(a) int(a.size())
#define pb push_back
#define pbb pop_back
#define vc vector
#define rev reverse
#define F first
#define S second

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    multiset<ll> cnt;
    ll ans = 0;
    ll cur_sum = 0;
    for (int i = 0; i < n; i++) {
        ans = max(ans, cur_sum + a[i] + (i + 1));
        if (len(cnt) + 1 <= k - 1) {
            cnt.insert(a[i]);
            cur_sum += a[i];
        } else {
            if (!cnt.empty()) {
                ll var = *cnt.begin();
                if (var < a[i]) {
                    cur_sum -= var;
                    cnt.erase(cnt.find(var));
                    cnt.insert(a[i]);
                    cur_sum += a[i];
                }
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
