// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x) long long
#define here cerr<<"---------------------------\n"
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
using namespace std;
using namespace __gnu_pbds;
#define maxn 200005
#define maxx 200005
#define mx 200000
#define f 100000
#define ceri(a,l,r) {for(ll i = l;i<=r;i++) cerr<<a[i]<< " ";cerr<<endl;}
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
ll n,k;
ll a[maxn];
ll t[4*maxx];
ll t2[4*maxx];
void upd(ll v,ll tl,ll tr,ll i,ll x,ll* t){
    if(tl==tr){
        t[v]+=x;
        return;
    }
    ll mid = (tl+tr)/2;
    if(i<=mid) upd(2*v,tl,mid,i,x,t);
    else upd(2*v+1,mid+1,tr,i,x,t);
    t[v] = t[2*v] + t[2*v+1];
}
ll sum(ll v,ll tl,ll tr,ll l,ll r,ll* t){
    //cerr<<tl<< " "<<tr<<" "<<l<< " "<<r<<endl;
    if(l>r) return 0;
    if(tl==l&&tr==r) return t[v];
    ll mid = (tl+tr)/2;
    return sum(2*v,tl,mid,l,min(mid,r),t) + sum(2*v+1,mid+1,tr,max(mid+1,l),r,t);
}
int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> n >> k;
    for(ll i = 1;i<=n;i++) cin >> a[i];
    for(ll i = 1;i<=n;i++) a[i]+=f;
    if(n==2){
        cout<<abs(a[1]-a[2])<<endl;
        return 0;
    }
    if(n==k){
        ll sum = 0;
        for(ll i = 1;i<=n;i++){
            sum+=a[i];
        }
        sum-=2*min(a[1],a[n]);
        cout<<sum-(k-2)*f<<endl;
        return 0;
    }
    ordered_set s;
    ll ans = -llinf;
    for(ll i = 1;i<=n;i++){
        if(t2[1]>=k-1){
            ll x = *s.find_by_order(i-k);
            ans = max(ans,sum(1,0,mx,x,mx,t) - x*(sum(1,0,mx,x,mx,t2)-k+1)-a[i]);
        }
        s.insert(a[i]);
        upd(1,0,mx,a[i],a[i],t);
        upd(1,0,mx,a[i],1,t2);
    }
    s.clear();
    fill(t,t+4*maxn-1,0);
    fill(t2,t2+4*maxx-1,0);
    reverse(a+1,a+n+1);
    for(ll i = 1;i<=n;i++){
        if(t2[1]>=k-1){
            ll x = *s.find_by_order(i-k);
            ans = max(ans,sum(1,0,mx,x,mx,t) - x*(sum(1,0,mx,x,mx,t2)-k+1)-a[i]);
        }
        s.insert(a[i]);
        upd(1,0,mx,a[i],a[i],t);
        upd(1,0,mx,a[i],1,t2);
    }
    cout<<ans-(k-2)*f<<endl;
	return 0;
}