using System;

namespace takmicenje_drugi_krug
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int k = int.Parse(s[1]);
            int[] a = new int[n];
            int moc = 0;
            s = Console.ReadLine().Split();
            for (int i = 0; i < n; i++)
            {
                a[i] = int.Parse(s[i]);
            }
            int j = 0;
            while (j < n - 1)
            {
                if (k >= 2)
                {
                    if (a[j] > a[n - 1])
                    {
                        moc += a[j] - a[n - 1];
                        j++;
                        n--;
                        k -= 2;
                    }
                    else
                    {
                        moc += a[n - 1] - a[j];
                        a[j] = a[j + 1];
                        n--;
                        k -= 2;
                        j++;
                    }
                }
            }
            if (k == 0)
                Console.WriteLine(moc);
        }
    }
}