using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Generic;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] x1 = Console.ReadLine().Split();
            int N = int.Parse(x1[0]);
            int K = int.Parse(x1[1]);
            int[] Niz = new int[N];
            int br = 0; int max = 0;
            string[] x2 = Console.ReadLine().Split();
            max = int.Parse(x2[0]); int br2 = 0;
            Niz[0] = int.Parse(x2[0]);
            for (int i = 1; i < N; i++)
            {
                Niz[i] = int.Parse(x2[i]);
                if (Niz[i] < max)
                {
                    max = Niz[i];
                    br = i;
                }
                else if (Niz[i] == max)
                {
                    br2 = i;
                }
            }
            //int[] Niz1 = new int[N];
            // int[] Niz2 = new int[N];
            List<int> Niz1 = new List<int>(N);
            List<int> Niz2 = new List<int>(N);
            List<int> Niz3 = new List<int>(N);
            List<int> Niz4 = new List<int>(N);
            if ( br2==0)
            {
                for (int i = 0; i < br; i++)
                {


                    Niz1.Add(Niz[i]);

                }
                for (int i = br; i < N; i++)
                {

                    if (br + 1 < N)
                        Niz2.Add(Niz[i]);
                }
            }
            else if( br!= br2 && br2!=0)
            {
                for (int i = 0; i < br2; i++)
                {


                    Niz3.Add(Niz[i]);

                }
                for (int i = br2; i < N; i++)
                {

                    if (br2 + 1 < N)
                        Niz4.Add(Niz[i]);
                }
            }

            int kk = K - 1;
            int s1 = 0;
            int s2 = 0;
            for (int i = 0; i < kk; i++)
            {
                if (Niz1.Count != 0)
                {
                    s1 = s1 + Niz1.Max();

                    Niz1.Remove(Niz1.Max());
                }

                if (Niz2.Count != 0)
                {
                    s2 = s2 + Niz2.Max();

                    Niz2.Remove(Niz2.Max());
                }
            }
            int s3 = 0;
            int s4 = 0;

            if (br != br2 && br2!=0)
            {
                for (int i = 0; i < kk; i++)
                {
                    if (Niz3.Count != 0)
                    {
                        s3 = s3 + Niz3.Max();

                        Niz3.Remove(Niz3.Max());
                    }

                    if (Niz4.Count != 0)
                    {
                        s4 = s4 + Niz4.Max();

                        Niz4.Remove(Niz4.Max());
                    }
                }
            }

            Console.WriteLine( Math.Max(Math.Max(s1, s2),Math.Max(s3,s4)) - max);
        }
    }
}