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

namespace ProblemD
{
    class Program
    {
        static void Main(string[] args)
        {
            var read = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            var N = read[0];
            var K = read[1];
            var VsList = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList();
            var ret = 0;
            var count = K;
            var jueList = new List<int>();
            var getLeftNum = 0;
            var getRightNum = 0;
            var minNK = Math.Min(N, K);
            while (getLeftNum <= minNK)
            {
                while(getLeftNum + getRightNum <= minNK)
                {
                    jueList = VsList.GetRange(0, getLeftNum).Concat(VsList.GetRange(N - getRightNum, getRightNum)).ToList();
                    jueList.Sort();
                    var backNum = Math.Min(K - getLeftNum - getRightNum, jueList.Count());
                    for (int i = 0; i < backNum && jueList[0] < 0 ; i++)
                    {
                        jueList.RemoveAt(0);
                    }
                    var sum = jueList.Sum();
                    if (ret < sum) ret = sum;
                    getRightNum++;
                }
                getRightNum = 0;
                getLeftNum++;
            }
            //ret = dp(VsList, K, jueList);
            Console.WriteLine(ret);
        }
        public static int dp(IEnumerable<int> Vs, int count, List<int> jList)
        {
            if (count == 0)
                return 0;
            var retA = 0;
            var retB = 0;
            var retC = 0;
            var retD = 0;
            var retNothing = dp(Vs, count - 1, jList);
            if (Vs.Count() == 0)
            {
                retA = int.MinValue;
                retB = int.MinValue;
            }
            else
            {
                var VsForA = new Queue<int>(Vs);
                var temp = VsForA.Dequeue();
                var jListForA = new List<int>(jList);
                jListForA.Add(temp);
                retA = temp + dp(VsForA, count - 1, jListForA);

                var VsForB = new List<int>(Vs);
                temp = VsForB.Last();
                var jListForB = new List<int>(jList);
                jListForB.Add(temp);
                retB = temp;
                //var VsForBQ = new Queue<int>();
                retB += dp(VsForB.GetRange(0, VsForB.Count() - 1), count - 1, jListForB);
            }
            if(jList.Count() == 0)
            {
                retC = int.MinValue;
                retD = int.MinValue;
            }
            else
            {
                var jListForCAndD = new HashSet<int>(jList);
                var temp = jListForCAndD.Min();
                if(temp < 0)
                {
                    jListForCAndD.Remove(temp);
                    var VsForC = new List<int>(Vs);
                    VsForC.Insert(0, temp);
                    //var VsForCQ = new Queue<int>(VsForC);
                    retC = dp(VsForC, count - 1, jListForCAndD.ToList()) - temp;

                    var VsForD = new Queue<int>(Vs);
                    VsForD.Enqueue(temp);
                    retD = dp(VsForD, count - 1, jListForCAndD.ToList()) - temp;
                }
                else
                {
                    retC = int.MinValue;
                    retD = int.MinValue;
                }
            }
            return new List<int> { retA, retB, retC, retD, retNothing }.Max();


        }

    }
}


Measured memory_bytes: 13396