#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define maxn 200050
#define inf 20000000050

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

int n, k;
int a[maxn];
set<pair<int,int>> p;
set<pair<int,int>> m;
set<pair<int,int>> ps;
set<pair<int,int>> ms;
vector<pair<int,int>> v;
vector<pair<ll,int>> r;
vector<ll> resp;

pair<int,int> maxx(set<pair<int,int>> s)
{
    return *s.rbegin();
}

pair<int,int> minn(set<pair<int,int>> s)
{
    return *s.begin();
}

ll resi()
{
    p.clear();
    m.clear();
    ps.clear();
    ms.clear();
    r.clear();
    resp.clear();
    for(int i=0; i<n; i++){
        r.pb({0,0});
        resp.pb(0);
    }
    for(int i=0; i<k; i++){
        m.insert(v[i]);
        r[0].first=r[0].first-v[i].first;
    }
    for(int i=k; i<n; i++){
        ms.insert(v[i]);
    }

    for(int i=0; i<n-1; i++){
        if(i>0){
            r[i]=r[i-1];
        }
       if(m.find({a[i],i})==m.end()){
            ms.erase(ms.lower_bound({a[i],i}));
            if(p.size()==0){
                pii x=maxx(m);
                int y=-x.first;
                if(a[i]>=y){
                    p.insert({a[i],i});
                    r[i].first+=a[i];
                    r[i].second++;
                    set<pii>::iterator it=m.end();
                    it--;
                    pii t=*it;
                    ms.insert(t);
                    r[i].first+=t.first;
                    m.erase(it);
                }
                else{
                    ps.insert({a[i],i});
                }
            }
            else if(m.size()==0){
                pii x=minn(p);
                int y=x.first;
                if(a[i]>=y){
                    pii t=*p.begin();
                    ps.insert(t);
                    r[i].first-=t.first;
                    p.erase(p.begin());
                    r[i].first+=a[i];
                    p.insert({a[i],i});
                }
                else{
                    ps.insert({a[i],i});
                }
            }
            else{
                pii x1=maxx(m);
                int y1=-x1.first;
                pii x2=minn(p);
                int y2=x2.first;
                if(a[i]>=min(y1,y2)){
                    if(y1<=y2){
                        set<pii>::iterator it=m.end();
                        it--;
                        pii t=*it;
                        ms.insert(t);
                        r[i].first+=t.first;
                        m.erase(it);
                    }
                    else{
                        pii t=*p.begin();
                        ps.insert(t);
                        r[i].first-=t.first;
                        r[i].second--;
                        p.erase(p.begin());
                    }
                    r[i].first+=a[i];
                    r[i].second++;
                    p.insert({a[i],i});
                }
                else{
                    ps.insert({a[i],i});
                }
            }
        }
        else{
            r[i].first+=a[i];
            m.erase(m.lower_bound({a[i],i}));
            ps.insert({a[i],i});
            if(ms.size()==0){
                pii x=maxx(ps);
                r[i].first+=x.first;
                r[i].second++;
                p.insert(x);
                set<pii>::iterator it=ps.end();
                it--;
                ps.erase(it);
            }
            else{
                if(maxx(ps).first>=-minn(ms).first){
                    pii x=maxx(ps);
                    r[i].first+=x.first;
                    r[i].second++;
                    p.insert(x);
                    set<pii>::iterator it=ps.end();
                    it--;
                    ps.erase(it);
                }
                else{
                    pii x=minn(ms);
                    r[i].first-=x.first;
                    m.insert(x);
                    ms.erase(ms.begin());
                }
            }
       }
       if(r[i].second==0){
           pii x=maxx(ps);
           pii y=maxx(m);
           resp[i]=r[i].first+x.first+y.first;
       }
       else if(r[i].second==k){
            pii x=minn(p);
            pii y=minn(ms);
            resp[i]=r[i].first-x.first-y.first;
       }
       else{
            resp[i]=r[i].first;
       }
    }
    ll res=-inf;
    for(int i=0; i<n-1; i++){
        res=max(res,resp[i]);
    }
    return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> k;
	for(int i=0; i<n; i++){
        cin >> a[i];
        v.pb({a[i],i});
	}
	sort(v.begin(),v.end());
	ll o1=resi();
	v.clear();
	for(int i=0; i<n; i++){
        a[i]=-a[i];
        v.pb({a[i],i});
	}
	sort(v.begin(),v.end());
	ll o2=resi();
	cout << max(o1,o2) << "\n";
}