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

using namespace std;
using namespace __gnu_pbds;

#define ll long long
const ll MAXN=2e5+7,INF=1e9,LINF=1e18;
const double EPS=1e-9;
#define fi first
#define se second
#define pll pair<ll,ll>
#define pii pair<int,int>
#define mid (l+r)/2
#define sz(a) int((a).size())
#define all(a) a.begin(),a.end()
#define mod 1000000007LL
#define pb push_back
#define endl "\n"
#define PRINT(x) cerr<<#x<<'-'<<x<<endl<<flush;
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pb push_back
#define pf push_front
#define ppf pop_front
#define ppb pop_back
#define PRINTvec(x) { cerr<<#x<<"-"; for(int i=0;i<sz(x);i++) cerr<<x[i]<<" "; cerr<<endl; }

ll m,q,x,y,z,l,r,res=-LINF;
int n,k;
ifstream in;
ofstream out;

#ifdef ONLINE_JUDGE
    #define in cin
    #define out cout
#endif

ll seg[4*MAXN];
vector<int> a;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set_less;

ordered_set_less pozl,pozr;

typedef tree<
int,
null_type,
greater<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set_greater;

ordered_set_greater negl,negr;

ll ppozl[MAXN],ppozr[MAXN],pnegl[MAXN],pnegr[MAXN];

void solve(){
    in>>n>>k;
    a.resize(n+1);
    for(int i=1;i<=n;i++) in>>a[i];
    for(int i=1;i<=n;i++){
        pozr.insert(a[i]);
        negr.insert(a[i]);
    }
    for(int i=1;i<n;i++){
        pozl.insert(a[i]);
        negl.insert(a[i]);
        pozr.erase(a[i]);
        negr.erase(a[i]);
        for(int j=1;j<=min(i,k-1);j++){
            int jthpoz=*(pozl.find_by_order(j-1));
            int jthneg=*(negl.find_by_order(j-1));
            ppozl[j]=ppozl[j-1]+jthpoz;
            pnegl[j]=pnegl[j-1]+jthneg;
        }
        for(int j=1;j<=min(n-i,k-1);j++){
            int jthpoz=*(pozr.find_by_order(j-1));
            int jthneg=*(negr.find_by_order(j-1));
            ppozr[j]=ppozr[j-1]+jthpoz;
            pnegr[j]=pnegr[j-1]+jthneg;
        }
        for(int j=1;j<=min(i,k-1);j++){
            res=max({res,ppozl[j]-ppozr[k-j],ppozl[j]-pnegr[k-j],pnegl[j]-ppozr[k-j],pnegl[j]-pnegr[k-j]});
            res=max({res,-ppozl[j]+ppozr[k-j],-ppozl[j]+pnegr[k-j],-pnegl[j]+ppozr[k-j],-pnegl[j]+pnegr[k-j]});
        }
    }
    cout<<res<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
        in.open("D:\\UROS\\Programiranje\\input.in",ios::in);
        out.open("D:\\UROS\\Programiranje\\output.out",ios::trunc | ios::out);
    #endif
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);'
    int t;
    ///cin>>t;
    t=1;
    while(t--){
        solve();
    }
    return 0;
}