//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 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;
    set<pii> st;
    long long cur_sm = 0, ans = 0;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        if (sz(st) < k - 1) {
            st.insert({x, i});
            cur_sm += x;
        } else {
            ans = max(ans, cur_sm + x + i + 1);
            auto it = st.begin();
            if (it->first < x) {
                cur_sm -= it->first;
                st.erase(it);
                cur_sm += x;
                st.insert({x, 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();
    }
}