#include <iostream>
#include <stdio.h>
int main()
{
    using namespace std;
    int K,N,i,a[100],uk=0,min,max,ma=1,mi=1,r=0,p=0;
    cin>>N>>K;
    if (K < 2 || N>200000 || K>N){
        abort();
    }
    for (i=1;i<=N;i++)
    {
        cin>>a[i];
        b[i]= a[i];
        if (abs(a[i])>100000)
        {
            abort();
        }
    }

    do {
    max = a[1];
    min = a[1];
    for (i=1;i<=N;i++) {
        if (a[i]>max){
            max=a[i];
            ma=i;
        }
        if (a[i]<min){
            min=a[i];
            mi=i;
        }
    }
    p = (max-min);
    uk = uk + (max - min);
    a[ma] = p;
    do{
        a[mi] = a[mi+1];
        mi++;
    }while(mi<N-1);
    N--;
    r++;
}while(r<K-1);
cout<<uk;
}