Problem:
Score : 400 points

Problem Statement

Your friend gave you a dequeue D as a birthday present.

D is a horizontal cylinder that contains a row of N jewels.

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 100

-10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

Do operation A. You take out the jewel of value -10 from the left end of D.

Do operation B. You take out the jewel of value 6 from the right end of D.

Do operation A. You take out the jewel of value 8 from the left end of D.

Do operation D. You insert the jewel of value -10 to the right end of D.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC128.D();
        }



        class ABC128
        {
            public static void A()
            {
                var A = Read.Int();
                var P = Read.Int();
                Console.WriteLine((A * 3 + P) / 2);
            }
            public static void B()
            {
                var N = Read.Int();
                var dict = new Dictionary<string, List<Tuple<int, int>>>();
                for (int n = 1; n <= N; ++n)
                {
                    var s = Read.Str();
                    var p = Read.Int();
                    if (!dict.ContainsKey(s)) { dict.Add(s, new List<Tuple<int, int>>()); }
                    dict[s].Add(new Tuple<int, int>(p, n));
                }
                foreach (var kvp in dict.OrderBy(x => x.Key))
                {
                    var ps = kvp.Value.OrderBy(x => -x.Item1).ToArray();
                    foreach (var p in ps)
                    {
                        Console.WriteLine(p.Item2);
                    }
                }
            }
            public static void C()
            {
                var N = Read.Int();
                var M = Read.Int();
                var S = new HashSet<int>[M];
                for (int i = 0; i < M; ++i)
                {
                    S[i] = new HashSet<int>();
                    var s = Read.IntN();
                    foreach (var ss in s.Skip(1))
                    {
                        S[i].Add(ss - 1);
                    }
                }
                var P = Read.IntN();
                int count = 0;
                for (int i = 0; i < (1 << N); ++i)
                {
                    bool ok = true;
                    for (int m = 0; m < M; ++m)
                    {
                        var on = 0;
                        foreach (var n in S[m])
                        {
                            if (((1 << n) & i) > 0) { on++; }
                        }
                        if (on % 2 != P[m]) { ok = false; break; }
                    }
                    if (ok) { count++; }
                }
                Console.WriteLine(count);
            }
            public static void D()
            {
                var N = Read.Int();
                var K = Read.Int();
                var V = Read.LongN();
                long max = 0;
                for (int left = 0; left <= N; ++left)
                {
                    for (int right = 0; right <= N - left; ++right)
                    {
                        if (left + right > K) { break; }
                        List<long> list = new List<long>();
                        for (int i = 0; i < left; ++i) { list.Add(V[i]); }
                        for (int i = 0; i < right; ++i) { list.Add(V[N - 1 - i]); }
                        list = list.OrderBy(x => x).ToList();
                        long sum = 0;
                        int drop = K - left - right;
                        for (int i = 0; i < list.Count; ++i)
                        {
                            sum += i < drop ? Math.Max(0, list[i]) : list[i];
                        }
                        max = Math.Max(max, sum);
                    }
                }
                Console.WriteLine(max);
            }
            public static void E()
            {
                var N = Read.Int();
                var Q = Read.Int();
                var STX = new Tuple<long, long, long>[N];
                for (int i = 0; i < N; ++i)
                {
                    var v = Read.LongN();
                    STX[i] = new Tuple<long, long, long>(v[0], v[1], v[2]);
                }
                var D = Enumerable.Range(0, Q).Select(i => Read.Long()).ToArray();

            }
            public static void F()
            {

            }
        }




        class Util
        {
            public static long[] ASum(long[] array)
            {
                var res = new long[array.Length + 1];
                for (int i = 0; i < array.Length; ++i)
                {
                    res[i + 1] = res[i] + array[i];
                }
                return res;
            }
        }

        static class Read
        {
            static Queue<string> queue = new Queue<string>();
            static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
            public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
            public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
            public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
            public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
            public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
            public static string[] StrN() { return Console.ReadLine().Split(' '); }
            public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
            public static long[,] LongMatrix(int r, int c)
            {
                var mat = new long[r, c];
                for (int i = 0; i < r; ++i)
                {
                    var x = Read.LongN();
                    for (int j = 0; j < c; ++j)
                    {
                        mat[i, j] = x[j];
                    }
                }
                return mat;
            }
            public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M)
            {
                Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new HashSet<int>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1]);
                    dict[ab[1]].Add(ab[0]);
                }
                return dict;
            }
            public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
            {
                Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new Dictionary<int, long>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1], ab[2]);
                    dict[ab[1]].Add(ab[0], ab[2]);
                }
                return dict;
            }
        }
    }
}