// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#ifdef LOCAL
    #define debug(x) cout << #x << ": " << x << endl;
#else
    #define debug(x)
#endif
#define mt_seed chrono::high_resolution_clock::now().time_since_epoch().count()
#define time_now chrono::high_resolution_clock::now()
#define time_res(s, f) chrono::duration_cast<chrono::nanoseconds>(f - s).count()
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define file_in freopen("input.txt", "r", stdin);
#define file_out freopen("output.txt", "w", stdout);
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a.size())
#define pb push_back
#define ff first
#define sc second
#define int ll

using namespace std;
using namespace __gnu_pbds;

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using str = string;
using pii = pair<int, int>;
using ordered_set = tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;

template<typename T1, typename T2> istream& operator>>(istream &in, pair<T1, T2> &a) { in >> a.ff >> a.sc; return in; }
#ifdef LOCAL
    template<typename T1, typename T2> ostream& operator<<(ostream &out, const pair<T1, T2> &a) { out << "{" << a.ff << ' ' << a.sc << "}"; return out; }
#else
    template<typename T1, typename T2> ostream& operator<<(ostream &out, const pair<T1, T2> &a) { out << a.ff << ' ' << a.sc; return out; }
#endif
template<typename T> istream& operator>>(istream &in, vector<T> &a) { for (auto &el : a) in >> el; return in; }
template<typename T> ostream& operator<<(ostream &out, const vector<T> &a) { for (auto &el : a) out << el << " "; return out; }
template<typename T> ostream& operator<<(ostream &out, const set<T> &a) { for (auto &el : a) out << el << " "; return out; }
template<typename T> ostream& operator<<(ostream &out, const multiset<T> &a) { for (auto &el : a) out << el << " "; return out; }
template<typename T, typename comp> ostream& operator<<(ostream &out, const unordered_set<T, comp> &a) { for (auto &el : a) out << el << " "; return out; }
template<typename T> T max(T a, T b, T c) { return max(a, max(b, c)); }
template<typename T> T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); }
template<typename T> T min(T a, T b, T c) { return min(a, min(b, c)); }
template<typename T> T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); }
const ll LOL = mt_seed;
mt19937_64 genom(LOL); ll randint(ll l, ll r) { uniform_int_distribution<ll> random_distrib_2_0(l, r); return random_distrib_2_0(genom); }
const ll RANDOM = genom(); struct chash { ll operator()(ll x) { return x ^ RANDOM; } };

signed main() {
    fast
#ifdef LOCAL
    file_in
#endif

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    cin >> a;
    multiset<int> st;
    int sum = 0;
    int ans = 0;
    // --k;
    for (int i = 0; i < n; ++i) {
        if (sz(st) < k - 1) {
            st.insert(a[i]);
            sum += a[i];
        }
        if (sz(st) >= k) {
            sum -= *st.begin();
            st.erase(st.begin());
        }
        ans = max(ans, sum + i + 1 + a[i]);
        st.insert(a[i]);
        sum += a[i];
    }
    cout << ans << '\n';

    return 0;
}
