#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll bitovi(ll n)
{
    ll count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
ll dp(vector<ll> v, ll l, ll r){
    if((l+1)==r) return max(v[l]-v[r], v[r]-v[l]);
    else{
        ll rr=v[r], ll=v[l], res=LLONG_MIN;
        v[r]=rr-ll;
        res=max(res, dp(v, l+1, r));
        v[l]=ll-rr;
        res=max(res, dp(v, l, r-1));
        return res;
    }
}
int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  ll n, k;
  cin>>n>>k;
  vector<ll> a(n);
  for(ll i=0; i<n; i++){
    cin>>a[i];
  }
  if(n<=10){
    ll res=LLONG_MIN;
    for(ll mask=0; mask<(1<<n); mask++){
        if(bitovi(mask)==k){
            vector<ll> v;
            ll pom=mask, i=0;
            while(pom){
                if(pom&1) v.push_back(a[i]);
                i++;
                pom>>=1;
            }
            res=max(res, dp(v, 0, k-1));
        }
    }
    cout<<res;
  }
  else if(n==k){
    ll l=0, r=n-1;
    while(r-l>1){
        if(a[l]<a[r]){
            a[l]-=a[r];
            r--;
        }
        else if(a[r]<a[l]){
            a[r]-=a[l];
            l++;
        }
        else{
            if(a[r-1]>a[l+1]){
                a[l]-=a[r];
                r--;
            }
            else{
                a[r]-=a[l];
                l++;
            }
        }
    }
    cout<<max(a[l], a[r])-min(a[l], a[r]);
  }
  else{
    ll res=LLONG_MIN;
    for(ll mask=0; mask<(1<<n); mask++){
        if(bitovi(mask)==k){
            vector<ll> v;
            ll pom=mask, i=0;
            while(pom){
                if(pom&1) v.push_back(a[i]);
                i++;
                pom>>=1;
            }
            res=max(res, dp(v, 0, k-1));
        }
    }
    cout<<res;
  }
}