#include <iostream>
#include <set>
#include <algorithm>

#define ll long long

using namespace std;

ll n,k;
ll a[200005];

multiset<ll> rused;
multiset<ll> runus;
multiset<ll> lused;

int g=-1;
ll sumleft=0;
ll sumright=0;
ll maxsum=-1000000000000;

void ispis()
{
    cout<<endl<<"----------------------------------"<<endl;
    cout<<g<<endl;
    cout<<"rused"<<endl;
    for(multiset<ll>::iterator i = rused.begin(); i!=rused.end(); i++)
        cout<<*i<<" ";
    cout<<endl;
    cout<<"runus"<<endl;
    for(multiset<ll>::iterator i = runus.begin(); i!=runus.end(); i++)
        cout<<*i<<" ";
    cout<<endl;
    cout<<"lused"<<endl;
    for(multiset<ll>::iterator i = lused.begin(); i!=lused.end(); i++)
        cout<<*i<<" ";
    cout<<endl;
    cout<<endl;
    cout<<"sumleft "<<sumleft<<endl;
    cout<<"sumright "<<sumright<<endl;
    cout<<"maxsum "<<maxsum<<endl;
    cout<<endl;
}

bool move_right()
{
    g++;
    auto fg=runus.find(a[g]);
    if(fg!=runus.end())
        runus.erase(fg);
    else
    {
        rused.erase(rused.find(a[g]));
        sumright-=a[g];
        if(runus.empty()){
            return false;
        }
        ll e=*runus.rbegin();
        rused.insert(e);
        sumright+=e;
        runus.erase(--runus.end());
    }
    return true;
}

void prepr()
{
    g=-1;
    sumleft=0;
    sumright=0;
    rused.clear();
    runus.clear();
    lused.clear();
    for(int i=0; i<n; i++)
    {
        runus.insert(a[i]);
    }
    int k1=k;
    while(k1)
    {
        k1--;
        ll e = *runus.rbegin();
        rused.insert(e);
        runus.erase(--runus.end());
        sumright+=e;
    }
}

void run()
{
    //ispis();
    while(g<n)
    {
        if(move_right())
        {
            ll e2 = *rused.begin();
            if(!lused.empty())
            {
                ll e1 = *lused.rbegin();
                if(a[g]<e1)
                {
                    lused.insert(a[g]);
                    sumleft+=a[g];
                    if(-e1>=e2)
                    {
                        rused.erase(rused.begin());
                        runus.insert(e2);
                        sumright-=e2;
                    }
                    else
                    {
                        lused.erase(--lused.end());
                        sumleft-=e1;
                    }
                }
                else if(a[g]>e2)
                {
                    rused.erase(rused.begin());
                    runus.insert(e2);
                    lused.insert(a[g]);
                    sumleft+=a[g];
                    sumright-=e2;
                }
                else
                    continue;
            }
            else if(-a[g]>e2)
            {
                rused.erase(rused.begin());
                runus.insert(e2);
                lused.insert(a[g]);
                sumleft+=a[g];
                sumright-=e2;
            }
            else
            {
                if(rused.empty())
                    break;
                maxsum=max(maxsum, sumright-sumleft-a[g]-e2);
                //ispis();
                continue;
            }
        }
        else
        {
            lused.insert(a[g]);
            sumleft+=a[g];
        }
        if(rused.empty())
            break;
        maxsum=max(maxsum, sumright-sumleft);
        //ispis();
    }

}

int main()
{
    cin>>n>>k;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }

    prepr();
    run();

    reverse(a, a+n);
    prepr();
    run();

    cout<<maxsum;

    return 0;
}