//author: лелутик :3
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <bitset>
#include <time.h>
#include <cmath>
#include <cassert>
#include <random>
using namespace std;
#define int long long
#define line cout << "\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define pb push_back
#define ppb pop_back
#define sz(a) (int) a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pii pair<int, int>


void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    multiset<int> st;
    int cur_sm = 0, ans = 0;
    for (int i = 0; i < n; i++) {
        if (sz(st) < k - 1) {
            st.insert(arr[i]);
            cur_sm += arr[i];
        } else {
            ans = max(ans, cur_sm + arr[i] + i + 1);
            if (*st.begin() < arr[i]) {
                cur_sm -= *st.begin();
                st.erase(st.begin());
                cur_sm += arr[i];
                st.insert(arr[i]);
            }
        }
    }
    cout << ans; line;
}

signed main() {
    fast;


#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


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