#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>

using namespace std;
using ll = long long;
using ld = long double;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using oset = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;

 #define int ll

signed main() {
    int n, k;
    cin >> n >> k;

    vector<int> a(n);
    int maxi = 0, maxvl = 0, maxvr = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        if (a[maxi] <= a[i]) {
            maxi = i;
        }
    }

    int res = maxi + 1;
    oset<pair<int, int>> now;
    for (int i = 0; i <= maxi; ++i) {
        now.insert({a[i], i});
    }

    int maxs = 0;
    for (int i = 0; i < k and i < now.size(); ++i) {
        maxs += now.find_by_order(i)->first;
    }
    if (now.size() >= k)
        res += maxs;

    for (int i = maxi + 1; i < n; ++i) {
        pair<int, int> nx = {a[i], i};
        now.insert(nx);
        if (i + 1 <= k) maxs += a[i];
        if (i + 1 < k) continue;
        if (i + 1 == k) {
            res = maxs + i + 1;
            continue;
        }

        if (now.order_of_key(nx) < k) {
            maxs -= now.find_by_order(k)->first;
            maxs += a[i];
            res = max(res, maxs + i + 1);

        } else {
            res = max(res, maxs - now.find_by_order(k - 1)->first + a[i] + i + 1);
        }
    }

    cout << res;
}
