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

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)
#define sum_overflow(a,b) __builtin_add_overflow_p ((a), (b), (__typeof__ ((a) + (b))) 0)
#define mul_overflow(a,b) __builtin_mul_overflow_p ((a), (b), (__typeof__ ((a) + (b))) 0)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;

using namespace __gnu_pbds;
template <class T> using oset = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
template <class T> using omset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;







//Note to self: Check for overflow

const int offset=1e5,k=(1<<18);
li val[2][550001]; int sz[2][550001];
int l[550001],r[550001];

void Add(bool c, int p, int s)
{
    if (s+offset>r[p] || s+offset<l[p]) return;
    val[c][p]+=s,sz[c][p]++;
    if (p<k) Add(c,2*p,s),Add(c,2*p+1,s);
}

void Rmv(bool c, int p, int s)
{
    if (s+offset>r[p] || s+offset<l[p]) return;
    val[c][p]-=s,sz[c][p]--;
    if (p<k) Rmv(c,2*p,s),Rmv(c,2*p+1,s);
}

li SumMaxK(bool c, int p, int m)
{
    if (p>=k) return (sz[c][p]==0?0:val[c][p]/sz[c][p]*m);
    if (sz[c][2*p+1]>m) return SumMaxK(c,2*p+1,m);
    return val[c][2*p+1]+SumMaxK(c,2*p,m-sz[c][2*p+1]);
}

li SumMaxK(int p, int m)
{
    if (p>=k) return (sz[0][p]+sz[1][p]==0?0:(val[0][p]+val[1][p])/(sz[0][p]+sz[1][p])*m);
    if (sz[0][2*p+1]+sz[1][2*p+1]>m) return SumMaxK(2*p+1,m);
    return val[0][2*p+1]+val[1][2*p+1]+SumMaxK(2*p,m-sz[0][2*p+1]-sz[1][2*p+1]);
}

li SumMinK(bool c, int p, int m)
{
    if (p>=k) return (sz[c][p]==0?0:val[c][p]/sz[c][p]*m);
    if (sz[c][2*p]>m) return SumMinK(c,2*p,m);
    return val[c][2*p]+SumMinK(c,2*p+1,m-sz[c][2*p]);
}

li SumMinK(int p, int m)
{
    if (p>=k) return (sz[0][p]+sz[1][p]==0?0:(val[0][p]+val[1][p])/(sz[0][p]+sz[1][p])*m);
    if (sz[0][2*p]+sz[1][2*p]>m) return SumMinK(2*p,m);
    return val[0][2*p]+val[1][2*p]+SumMinK(2*p+1,m-sz[0][2*p]-sz[1][2*p]);
}

int a[200005];
li pre[200005],suf[200005];

void solvexd(int n)
{
    li cur=0;
    ff(i,0,n) cur+=a[i],pre[i]=cur;
    cur=0;
    bff(i,0,n) cur+=a[i],suf[i]=cur;
    li ans=0;
    ff(i,0,n-1) ans=max(ans,max(pre[i]-suf[i+1],suf[i+1]-pre[i]));
    cout<<ans;
}

int main()
{
    FAST;

    ff(i,0,k) l[i+k]=i,r[i+k]=i;
    bff(i,1,k) l[i]=l[2*i],r[i]=r[2*i+1];

    int n,m; cin>>n>>m;
    ff(i,0,n) cin>>a[i];
    if (n==m) return solvexd(n),0;
    li ans=0;

    bff(i,0,n) Add(1,1,-a[i]);
    ff(i,0,n-1)
    {
        Add(0,1,a[i]),Rmv(1,1,-a[i]);

        ///+++---
        li levo=SumMaxK(0,1,m),desno=SumMaxK(1,1,m),oba=SumMaxK(1,m);
        if (oba==levo) ans=max(ans,SumMaxK(0,1,m-1)+SumMaxK(1,1,1));
        else if (oba==desno) ans=max(ans,SumMaxK(0,1,1)+SumMaxK(1,1,m-1));
        else ans=max(ans,oba);

        ///---+++
        levo=SumMinK(0,1,m),desno=SumMinK(1,1,m),oba=SumMinK(1,m);
        if (oba==levo) ans=max(ans,-SumMinK(0,1,m-1)-SumMinK(1,1,1));
        else if (oba==desno) ans=max(ans,-SumMinK(0,1,1)-SumMinK(1,1,m-1));
        else ans=max(ans,-oba);
    }

    cout<<ans;

    /*while (true)
    {
        string t; cin>>t;
        int x; cin>>x;
        if (t=="add") Add(0,1,x);
        else if (t=="rmv") Rmv(0,1,x);
        else cout<<SumMinK(0,1,x)<<","<<SumMaxK(0,1,x)<<endl;
    }*/
}

//Note to self: Check for overflow

/*

10 6
1 2 3 4 5 6 7 8 9 10

10 6
-1 2 3 -4 -5 -6 7 8 -9 10

10 10
1 1 1 1 1 1 1 1 -1 1

10 9
1 1 1 1 1 1 1 1 -1 1

10 6
1 1 1 1 -1 1 1 1 1 1

5 3
-100000 50000 100000 -13456 12

5 3
-100000 50000 -100000 -13456 12

4 3
-100000 50000 -100000 -13456

*/