#include <iostream>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    int powers[n];
    int power = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> powers[i];
    }
    for (int i = 0; i < k; i++)
    {
        if (powers[0] > powers[n - i - 1])
        {
            power += powers[0] - powers[n - i - 1];
            for (int j = n - i - 1; j < n; j++)
            {
                powers[j] = powers[j + 1];
            }
        }
        else
        {
            power += powers[n - i - 1] - powers[0];
            for (int j = 0; j < n; j++)
            {
                powers[j] = powers[j + 1];
            }
        }
    }
    cout << power << endl;
    return 0;
}