#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define dec(x, y) fixed << setprecision((y)) << (x)
#define xx first
#define yy second
#define srt(v) sort((v).begin(), (v).end())
#define srtr(v) sort((v).rbegin(), (v).rend())
#define pb push_back
#define popb pop_back
#define sz(a) (int)(a).size()
#define len(a) (int)(a).length()
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n, k;
ll a[200002], pref[200002];
multiset<ll> l, r;
int main() {
    ios;
    cin >> n >> k;
    for(int i=1; i<=n; i++) cin >> a[i];
    if(k==n) {
        for(int i=1; i<=n; i++) {
            pref[i]=pref[i-1]+a[i];
        }
        ll maxi=-2000000000000LL;
        for(int i=1; i<=n-1; i++) {
            maxi=max(maxi, max(pref[i]-(pref[n]-pref[i]), (pref[n]-pref[i])-pref[i]));
            //maxi=max(maxi, pref[i]-(pref[n]-pref[i]));
        }
        cout << maxi;
    }
    else { /// n<=1000
        ll maxi=-2000000000000LL;
        for(int i=1; i<=n; i++) {
            r.emplace(a[i]);
        }
        for(int i=1; i<=n-1; i++) {
            l.emplace(a[i]);
            auto nes=r.find(a[i]); r.erase(nes);
            vector<int> koji;
            ll suma=0LL;
            auto it=r.begin();
            for(int j=1; it!=r.end() && j<=n-i; it++, j++) {
                koji.pb(*it);
                suma+=*it;
            }
            ll suma1=0LL;
            it=prev(l.end());
            for(int j=max(1, k-sz(koji)), bla=sz(koji)-1; j<=min(i, k-1) && bla>=0; it--, j++, bla--) { /// j + (k-j) = k
                suma1+=*it;
                maxi=max(maxi, suma1-suma);
                suma-=koji[bla];
                if(it==l.begin()) break;
            }
        }
        cout << maxi;
    }
    return 0;
}