#include <bits/stdc++.h>

using namespace std;

int main()
{
    int total=0,i=0,n,k;
    cin>>n>>k;
    vector<int> a;
    vector<int> b;
     vector<int> c;
    while(i<n){
         int temp;
    cin>> temp;
        a.push_back(temp);
        i++;
    }
    if(k>2){
        sort(a.begin(),a.end());
        b.push_back(a[0]);
        k--;
        i=n-1;
        while(i>n-k-1){
            b.insert(b.begin(),a[i]);
            i--;
        }

    }
    else{
        sort(a.begin(),a.end());
        b.push_back(a[0]);
        b.push_back(a[n-1]);
    }
    i=0;

    i=1;
    int o=b.size();
    while(i<b.size()){
        if(o==2){
            if(b[0]>=b[1]){
                total=b[0]-b[1];
                cout<<total<<endl;
                return 0;
            }
            else{
                total=b[1]-b[0];
                cout<<total<<endl;
                 return 0;
            }

        }
        else if(o>2){
        if(b[0]>=b[b.size()-1]){
            b[0]=b[b.size()-1]-b[0];
            b.erase(b.begin()+b.size()-1);
        }
        else{
            b[b.size()-1]=b[0]-b[b.size()-1];
            b.erase(b.begin());
            }
            o--;
        }
    }
    

    return 0;
}