#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <unordered_set>
#include <fstream>
#include <algorithm>

using ll = long long;
#define int long long
#define endl '\n'
 #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unrool-loops")

using namespace std;

const int N = 100000 + 1, mod = 1e9 + 7;
int n, m, k;
int st = 1;


void solve() {
	//cin >> n >> k;
    n = 8; k = rand() % n + 1;
    multiset<int> ss;
    int sum = 0;
	vector<int> a(n);
	for (auto &x : a) {
        x = rand() % n + 1;
        //cin >> x;
    }
    int ans = 0;
    for (int i = 0; i < k - 1; i++) {
        ss.insert(a[i]); sum += a[i];
    }
    for (int i = k - 1; i < n; i++) {
        ans = max(ans, i + 1 + sum + a[i]);
        if (ss.size() > 0 && *ss.begin() < a[i]) {
            sum -= *ss.begin();
            ss.erase(ss.begin());
            ss.insert(a[i]);
            sum += a[i];
        }
    }
    cout << ans << endl;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cout.tie(0);
	cin.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
	int t = 100; // cin >> t;
	while (t--) {
		solve();
	}
}