using System;
using System.Collections.Generic;

namespace zadaci
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] temp1 = Console.ReadLine().Split(' ');
            int N = Convert.ToInt32(temp1[0]);
            int K = Convert.ToInt32(temp1[1]);

            int[] niz = new int[N]; // napitci
            temp1 = Console.ReadLine().Split(' ');
            for (int i = 0; i < N; i++)
                niz[i] = Convert.ToInt32(temp1[i]);

            int[] napitci = new int[K]; //izabrani

            if (K == 1) //Najveci napitak
            {
                int max = int.MinValue;
                for (int i = 0; i < N; i++)
                    if (niz[i] > max)
                        max = niz[i];

                Console.WriteLine(max);
            }
            else if (K == 2) //Najveci - najmanji
            {
                int max = int.MinValue;
                int min = int.MaxValue;
                for (int i = 0; i < N; i++)
                {
                    if (niz[i] < min)
                        min = niz[i];
                    if (niz[i] > max)
                        max = niz[i];
                }

                Console.WriteLine(max - min);
            }
            else // advanced algoritam
            {
                int min = int.MaxValue;
                int minIndex = int.MaxValue;//index
                List<int> ok = new List<int>();

                bool vera = true;
                while (vera)
                {
                    vera = false;

                    for (int i = 0; i < N; i++)
                    {
                        for (int j = 0; j < ok.Count; j++)
                            if (i == ok[j])
                                i++;

                        if ((N + 1) / 2 - 1 > minIndex || minIndex == int.MaxValue)
                        {
                            if (niz[i] < min)
                            {
                                min = niz[i];
                                minIndex = i;
                            }
                        }
                        else
                        {
                            if (niz[i] <= min)
                            {
                                min = niz[i];
                                minIndex = i;
                            }
                        }
                    }

                    if (K > N - 1 - minIndex && K - 1 > minIndex)
                    {
                        ok.Add(minIndex);
                        vera = true;
                        minIndex = int.MaxValue;
                        min = int.MaxValue;
                    }
                }

                //

                int sLevo = 0; // zbir levo
                int[] indexLevo = new int[K - 1]; //indexi levog
                for (int i = 0; i < K - 1; i++)
                    indexLevo[i] = -1;

                if (minIndex < K - 1)
                    sLevo = int.MinValue;
                else
                {
                    for (int i = 0; i < K - 1; i++) // levo
                    {
                        int indexTemp = -1;
                        int maxTemp = int.MinValue;
                        for (int j = 0; j < minIndex; j++)
                        {
                            bool z = true;
                            for (int g = 0; g < K - 1; g++)
                                if (j == indexLevo[g])
                                    z = false;

                            if (niz[j] > maxTemp && z)
                            {
                                maxTemp = niz[j];
                                indexTemp = j;
                            }
                        }

                        sLevo += maxTemp;
                        indexLevo[i] = indexTemp;
                    }
                }

                //



                int sDesno = 0; // zbir desno
                int[] indexDesno = new int[K - 1]; //indexi desno
                for (int i = 0; i < K - 1; i++)
                    indexDesno[i] = -1;


                if (minIndex > N - 1 - K)
                    sDesno = int.MinValue;
                else
                {
                    for (int i = 0; i < K - 1; i++) // desno
                    {
                        int indexTemp = -1;
                        int maxTemp = int.MinValue;
                        for (int j = minIndex + 1; j < N; j++)
                        {
                            bool z = true;
                            for (int g = 0; g < K - 1; g++)
                                if (j == indexDesno[g])
                                    z = false;

                            if (niz[j] > maxTemp && z)
                            {
                                maxTemp = niz[j];
                                indexTemp = j;
                            }
                        }

                        sDesno += maxTemp;
                        indexDesno[i] = indexTemp;
                    }
                }

                //porednjenje

                bool pro = false;
                if (sDesno < sLevo)
                {
                    Array.Sort(indexLevo);
                    for (int i = 0; i < K - 1; i++)
                        napitci[i] = indexLevo[i];
                    napitci[K - 1] = minIndex;
                    pro = false;
                }
                else
                {
                    Array.Sort(indexDesno);
                    for (int i = 0; i < K - 1; i++)
                        napitci[i + 1] = indexDesno[i];
                    napitci[0] = minIndex;
                    pro = true;
                }

                //Mesanje

                int e = 1;
                while (napitci.Length != 2)
                {
                    int[] novo = new int[K - e];
                    if (!pro) //najmanji je skroz desno
                    {
                        niz[napitci[K - e]] -= niz[napitci[0]];

                        for(int i = 0; i < K - e; i++)
                            novo[i] = napitci[i + 1];
                    }
                    else // najmanji je skroz levo
                    {
                        niz[napitci[0]] -= niz[napitci[K - e]];

                        for (int i = 0; i < K - e; i++)
                            novo[i] = napitci[i];
                    }
                    napitci = novo;
                    e++;
                }

                if (!pro) // mani je desno
                    Console.WriteLine(niz[napitci[0]] - niz[napitci[1]]);
                else // manji je levo
                    Console.WriteLine(niz[napitci[1]] - niz[napitci[0]]);
            }
        }
    }
}