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]);
            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;
                }
            }
            //int[] Niz1 = new int[N];
            // int[] Niz2 = new int[N];
            List<int> Niz1 = new List<int>(N);
            List<int> Niz2 = new List<int>(N);
            for (int i = 0; i < br; i++)
            {
                // Niz1[i] = Niz[i];
                // Niz2[i] = Niz[i+br];
                Niz1.Add(Niz[i]);
                Niz2.Add(Niz[i + br]);
            }
           int  kk = K - 1;
            int s1 = 0;
            int s2 = 0;
            for(int i = 0; i < kk; i++)
            {
                s1 =s1+ Niz1.Max();
               
                Niz1.Remove(Niz1.Max());

                s2 =s2+ Niz2.Max();
                Niz2.Remove(Niz2.Max());
            }
            Console.WriteLine(Math.Max(s1, s2) - max);
        }
    }
}