#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define sz(x) (int) x.size()
#define pb push_back
#define all(x) x.begin(), x.end()

using namespace std;
using pii = pair<ll, ll>;
using tii = tuple<ll, ll, ll>;
string printBin(ll m) { string a = ""; for(int i=63; i>=0; i--) { if(m & (1<<i)) {a += '1';} else {a += '0';} } return a; }

int main() {
    ll n, b; 
    while(cin >> n >> b) {
        vector<ll> a(n+1), pref(n+1, 0);

        for(int i=1; i<=n; i++) cin >> a[i], pref[i] = pref[i-1] + a[i];

        priority_queue<pii> maior;
        priority_queue<pii, vector<pii>, greater<>> menor;

        ll ans = 0;
        for(int i=1; i<=b; i++) {
            maior.push({a[i], i});
            menor.push({a[i], i});
        }

        ans += pref[b] - menor.top().fr - maior.top().fr;

        for(int i=b+1; i<=n; i++) {
            maior.push({a[i], i});
            menor.push({a[i], i});

            ans += pref[i] - pref[i-b];

            while(maior.top().sc <= i-b) maior.pop();
            ans -= maior.top().fr;

            while(menor.top().sc <= i-b) menor.pop();
            ans -= menor.top().fr;
        }

        cout << ans << endl;
    }
}