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

#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>


using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int dlt = 100001;
int n,k;
int bit[300005][2];
int niz[200005];
ll sm[300005][2];
int sz1,sz2;

void add(int x, ll val, int idx){
    while(x<=200001){
        bit[x][idx]++;
        sm[x][idx]+=val;
        x += (x&-x);
    }
}
int numquery(int x, int idx){
    int ans = 0;
    while(x>0){
        ans += bit[x][idx];
        x -= (x&-x);
    }
    return ans;
}
ll sumquery(int x, int idx){
    ll ans = 0;
    while(x > 0){
        ans += sm[x][idx];
        x -= (x&-x);
    }
    return ans;
}
void obrisi(int x, int val, int idx){
    while(x<=200001){
        bit[x][idx]--;
        sm[x][idx]-=val;
        x += (x&-x);
    }
}

int kti(int x, int idx){
    int suma = 0;
    int pos = 0;

    for(int i=18; i>=0; i--){
        if((pos + (1<<i) < 200001) && suma + bit[pos + (1<<i)][idx]<x){
            suma += bit[pos + (1<<i)][idx];
            pos += (1<<i);
        }
    }
    pos++;
    return pos;
}
ll sumdoktog(int x, int idx){
    ll koji = kti(x,idx);
    ll kolko = numquery(koji,idx);
    ll ans = sumquery(koji,idx);
    return ans - (kolko-x)*(koji-dlt);
}
ll raz(int idx){
    int levo = kti(sz1-(k-1)+idx,0);
    int desno = kti(sz2-idx,1);
    return desno-levo;
}


ll mini = 1e6;



ll radi(){
    ff(i,1,n){
        add(-100000+dlt,100000,0);
        add(-100000+dlt,-100000,1);
    }
    ff(i,1,n){
        add(-niz[i]+dlt,-niz[i],1);
    }
    ll ans = mini;
    ff(i,1,n-1){
        obrisi(-niz[i]+dlt,-niz[i],1);
        add(niz[i]+dlt,niz[i],0);
         sz1 = numquery(100000+dlt,0);
         sz2 = numquery(100000+dlt,1);
        ll p1 = sumdoktog(sz1,0) - sumdoktog(sz1-(k-1),0);
        ll p2 = sumdoktog(sz2,1) - sumdoktog(sz2-1,1);
        ans = max(ans, p1+p2);
        if(k == 2){
            continue;
        }
        int l = 1;
        int r = k-2;
        if(raz(1)<=0){
            continue;
        }
        while(l < r){
            int mid = (l+r+1)/2;
            if(raz(mid)>0){
                l = mid;
            }
            else r = mid - 1;
        }
        ll oduzimam = sumdoktog(sz1-(k-1)+l,0)-sumdoktog(sz1-(k-1),0);
        ll dodajem = sumdoktog(sz2-1,1)-sumdoktog(sz2-1-l,1);
        ans = max(ans, p1+p2-oduzimam+dodajem);
    }
    ff(i,0,300000){
        bit[i][0] = bit[i][1] = sm[i][0] = sm[i][1] = 0;
    }
    return ans;
}

ll brut(){
    ll res = mini;
    ff(i,1,n-1){
        vector<ll> p1;
        vector<ll> p2;
        ff(j,1,i)p1.pb(niz[j]);
        ff(j,i+1,n)p2.pb(niz[j]);
        ff(j,1,n){
            p1.pb(-100000);
            p2.pb(100000);
        }
        sort(p1.begin(), p1.end());
        sort(p2.begin(), p2.end());
        reverse(p1.begin(), p1.end());
        ll tr = 0;
        ff(j,1,k-1){
            ff(r,0,j-1)tr += p1[r];
            ff(r,0,(k-j)-1)tr -= p2[r];
//            cout << p1[0] << " " << p2[0] << endl;
//            cout << tr << "\n";
            res = max(res, tr);
            tr = 0;
        }
        //res = max(res,tr);
    }
    return res;

}


int main()
{
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(0);

    mini *= mini;
    mini = -mini;
    cin >> n >> k;
    ff(i,1,n){cin >> niz[i];}
//    n = rnd();
//    n %= 1000;
//    if(n<0)(n+=(1000));
//    if(n == 1)n+=5;
//
//    k = rnd();
//    k %= (n-1);
//    if(k<0)(k+=(n-1));
//    k++;
//    cout << n << " " << k << "\n";
//    //return 0;
//    ff(i,1,n){
//        niz[i] = (rnd())%200000;
//        if(niz[i] < 0)niz[i] += 200000;
//        niz[i] -= 100000;
//    }
    ll ans = radi();
    reverse(niz+1,niz+n+1);
    ans = max(ans,radi());
    cout << ans << "\n";

    return 0;
//    ll pom = brut();
//    reverse(niz+1,niz+n+1);
//    pom = max(pom,brut());
//    cout << pom << "\n";

    return 0;
}