#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

#pragma GCC optimize("O3,Ofast,fast-math,unroll-loops")
#pragma GCC target("avx,avx2,fma,sse,sse2,sse3,sse4,sse4.1,sse4.2")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long


//ordered_set
//ordered_set<int> A;
//A.find_by_order(1);
//A.order_of_key(1);

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int res = 0;
    int n, k;
    cin >> n >> k;
    ordered_set<int> A;
    vector<pair<int, int>>b(n);
    vector<int>a(n);
    vector<int>c(n);
    int x;
    int cnt_del = 0;
    if (k == 1) {
        for (int i = 1; i <= n; ++i) {
            cin >> x;res=max(res, x+i);
        }
        cout<<res;
        return 0;
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i].first;
        a[i] = b[i].first;
        b[i].second = i;
    }
    sort(b.begin(), b.end());
    for (int i = 0; i < n; ++i) {
        //cout << b[i].first <<" "<<b[i].second<<"::";
        c[b[i].second] = i;
    }
//    cout<<"\n";
//    for (auto i : c) {
//        cout << i << " ";
//    }
//    cout<<"\n";
    int bs = 0;
    for (int i = 0; i < n; ++i) {
        x = a[i];
//        for (int j = 0; j < A.size(); ++j) {
//            cout << b[*A.find_by_order(j)].first <<" ";
//        }
//        cout<<"\n";
        if (i >= (k-1)) {
            int cr = x + bs + i + 1;
            res=max(res, cr);
        }
        if (i < (k - 1)) {
            //cout<<"append " << c[i] << "\n";
            bs += x;
            A.insert(c[i]);
        } else {
            if (c[i] > *A.find_by_order(0) && a[i] != b[*A.find_by_order(0)].first) {
                bs -= b[*A.find_by_order(0)].first;
                bs += x;
                A.erase(*A.find_by_order(0));
                A.insert(c[i]);
            }
        }
    }
    cout << res;
}
