#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) __builtin_popcountll(a)
#define bithigh(a) 63-__builtin_clzll(a)
#define lg bithigh
ll highpow(ll a) { return 1LL << (ll)lg(a); }

using namespace std;

const ll LINF = 4e18;
const int mxN = 2e5+10, INF = 2e9, mod = (1 ? 1e9+7 : 998244353);
ll n, m, dp[2][mxN];
vector<ll> a;

ll Calc(){

    multiset<ll> s[2];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < m-1; i++){
        s[0].insert(a[i]);
        s[1].insert(a[i]);
        dp[0][m-2] += a[i];
        dp[1][m-2] += a[i];
    }
    for (int i = m-1; i < n; i++){
        ll x = *s[0].begin();
        if (x < a[i]){
            dp[0][i] = dp[0][i-1] - x + a[i];
            s[0].erase(s[0].find(x));
            s[0].insert(a[i]);
        }
        else dp[0][i] = dp[0][i-1];
        x = *s[1].rbegin();
        if (x > a[i]){
            dp[1][i] = dp[1][i-1] - x + a[i];
            s[1].erase(s[1].find(x));
            s[1].insert(a[i]);
        }
        else dp[1][i] = dp[1][i-1];
    }
    ll ans = -LINF;
    for (int i = m-1; i < n; i++){
        smax(ans, dp[0][i-1] - a[i]);
        smax(ans, a[i] - dp[1][i-1]);
    }
    return ans;
}

void Solve(){

    cin >> n >> m;
    a = vector<ll>(n);
    rv(a);
    ll ans = Calc();
    reverse(all(a));
    smax(ans, Calc());
    cout << ans << en;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    //cin >> t;
    while (t--)
        Solve();

    return 0;
}