#include "bits/stdc++.h"
#pragma GCC optimize("unroll-loops", "Ofast")
using namespace std;
#define ll long long
const int inf = 2e9;
const ll INF = 2e18;

//#define int ll
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
typedef tree<int, null_type, std::less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;

random_device dev;
mt19937 rnd(dev());

struct fenv {
    vector<ll> t;

    fenv(int n) {
        t.assign(n, 0);
    }

    void add (int ind, ll x) {
        for (; ind < t.size(); ind = (ind | (ind + 1)))
            t[ind] += x;
    }

    ll get (int ind) {
        ll res = 0;
        for (; ind >= 0; ind = (ind & (ind + 1)) - 1)
            res += t[ind];
        return res;
    }
};

template<typename T1, typename T2>
istream& operator >> (istream &in, pair<T1, T2> &a) {
    in >> a.first >> a.second;
    return in;
}

template<typename T1, typename T2>
ostream& operator << (ostream &out, pair<T1, T2> &a) {
    out << a.first << " " << a.second;
    return out;
}

template<typename T>
istream& operator >> (istream &in, vector<T> &a) {
    for (int i = 0; i < a.size(); i++)
        in >> a[i];
    return in;
}

template<typename T>
ostream& operator << (ostream &out, vector<T> &a) {
    for (auto &i: a)
        cout << i << " ";
    return out;
}

ll msMx(multiset<ll> &ms) {
    auto it = ms.end();
    it--;
    return *it;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int k, n;
    cin >> n >> k;
    vector<ll> a(n);
    cin >> a;
    vector<ll> sufMx(n + 1, 0);
    for (int i = n - 1; i >= 0; i--)
        sufMx[i] = max(a[i], sufMx[i + 1]);
    ll mxAns = 0;
    ll curSum = 0;
    multiset<ll> ms;
    for (int i = 0; i < k; i++) {
        curSum += a[i];
        ms.insert(a[i]);
    }
    if (msMx(ms) > sufMx[k])
        mxAns = k + curSum;

    for (int i = k; i < n; i++) {
        curSum += a[i] - *ms.begin();
        ll last = *ms.begin();
        ms.erase(ms.begin());
        ms.insert(a[i]);
        if (msMx(ms) > sufMx[i + 1])
            mxAns = max(mxAns, i + 1 + curSum);

        if (a[i] < last) {
            ms.erase(ms.find(a[i]));
            ms.insert(last);
            curSum += last - a[i];
        }
    }

    cout << mxAns << "\n";
}
