#include <iostream>

using namespace std;

int main()
{
    int n, k, sum = 0;

    cin >> n >> k;

    k = k-1;

    int a[n];

    for(int i = 0; i < n; i++){

        cin >> a[i];
    }

    int l = 0;

    int m = n-1;
    for(int i = 0; i < k; i++){

        if(a[l]<a[m]){

            sum = sum + a[m] - a[l];
            l++;
        }
        else if(a[l]>a[m]){

            sum = sum + (a[l] - a[m]);
            m++;
        }
    }
    cout << sum;

    return 0;
}