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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
class ChokudaiSpeedRun002 {
    static int __cnt = 0;
#if LOCAL_ENVIRONMENT
    static string __mark = "@@@";
#endif
    static void Main(string[] args) {
#if LOCAL_ENVIRONMENT
        StreamReader __sr = new StreamReader("stdin.txt");
        string __str = __sr.ReadToEnd();
        __sr.Close();
        int __index = __str.IndexOf(__mark, 0);
        while (__index != -1) {
            __index = __str.IndexOf(__mark, __index + __mark.Length);
            __cnt++;
        }
        if (__cnt > 0) {
            Console.Write("Eenter test data no(1-{0})? ", __cnt);
            Console.Out.Flush();
            if (!int.TryParse(Console.ReadLine(), out __cnt)) __cnt = 1;
            Console.WriteLine("Select/Default test data: {0}", __cnt);
        }
        var stdin = new System.IO.StreamReader("stdin.txt");
        Console.SetIn(stdin);
#endif
        SimpleScanner sc = new SimpleScanner(__cnt);
        int N = sc.NextInt(), K = sc.NextInt();
        int[] V = sc.NextIntArray(N);
        int ans = 0;
        for (var l = 0; l < K; l++) {
            for (var r = 0; r < K - l; r++) {
                if (l + r > N) break;
                var S = new List<int>();
                for (var n = 0; n < l; n++) S.Add(V[n]);
                for (var n = 0; n < r; n++) S.Add(V[N - n - 1]);
                S.Sort();
                int now = 0, d = K - l - r;
                for (var n = 0; n < S.Count; n++) {
                    if (S[n] < 0 && d > 0) d--;
                    else now += S[n];
                }
                ans = Math.Max(ans, now);
            }
        }
        Console.WriteLine(ans);
    } //Main

    public static int LowerBound<T>(T[] array, int start, int end, T value, IComparer<T> comparer) {
        int low = start, high = end, mid;
        while (low < high) {
            mid = ((high - low) >> 1) + low;
            if (comparer.Compare(array[mid], value) < 0)
                low = mid + 1;
            else
                high = mid;
        }
#if LOCAL_ENVIRONMENT
        Console.Error.WriteLine($"LowerBound(array:{array.ToStringA()}");
        Console.Error.WriteLine($"LowerBound(start:{start}, end:{end}, value:{value.ToString()}) return:{low}");
#endif
        return low;
    }
    public static int LowerBound<T>(T[] array, T value) where T : IComparable {
        return LowerBound(array, 0, array.Length, value, Comparer<T>.Default);
    }

    public class BIT {
        int n;
        int[] data;
        public BIT(int size){
            n = size + 1;
            data = new int[n];
        }
        public void add(int i, int x) {
            while (i < n) {
                data[i] += x;
                i += i & -i;
            }
        }
        public int sum(int i) {
            int ret = 0;
            while (i > 0) {
                ret += data[i];
                i -= i & -i;
            }
            return ret;
        }
    }

    public class Item : IEquatable<Item>, IComparable<Item> {
        public long value1 { get; set; }
        public long value2 { get; set; }
        public Item(long value1, long value2) {
            this.value1 = value1;
            this.value2 = value2;
        }
        public override string ToString() {
            return $"[value1:{this.value1}, value2:{this.value2}]";
        }
        public bool Equals(Item other) {
            if (other == null) return false;
            return this.value1 == other.value1 && this.value2 == other.value2;
        }
        public override bool Equals(Object obj) {
            if (obj == null) return false;
            Item item = obj as Item;
            if (item == null)  return false;
            else return Equals(item);
        }
        public override int GetHashCode() {
            long hc = this.value1 * this.value2;
            return hc.GetHashCode();
        }
        public int CompareTo(Item other) {
            if (other == null) return -1;
            if (this.value1 == other.value1)
                return this.value2.CompareTo(other.value2);
            else
                return this.value1.CompareTo(other.value1);
        }
        public int Compare(Item x, Item y) {
            if (x == null) return 1;
            else return x.CompareTo(y);

        }
    }

    public class SimpleScanner {
        private int cnt;
        private string[] items;
        public SimpleScanner(int cnt = 0) {
            this.cnt = 0;
            var sb = new StringBuilder();
            var s = Console.ReadLine();
            while (s != null) {
                sb.Append(s + " ");
                s = Console.ReadLine();
            }
            this.items = sb.ToString().Split(' ');
#if LOCAL_ENVIRONMENT
            if (cnt > 0) {
                int index = Array.IndexOf(this.items, __mark + cnt.ToString());
                this.cnt = index + 1;
            }
            Console.Error.WriteLine("SimpleScanner.cnt: {0}", this.cnt);
            Console.Error.WriteLine("SimpleScanner.items: {0}", this.items.ToStringA());
#endif
        }
        public void SetCnt(int n) { this.cnt = n; }
        public int NextInt() {
            return int.Parse(this.items[this.cnt++]);
        }
        public long NextLong() {
            return long.Parse(this.items[this.cnt++]);
        }
        public string NextString() {
            return this.items[this.cnt++];
        }
        public int[] NextIntArray(int n) {
            if (n < 1) throw new IndexOutOfRangeException("引数は1以上であること！");
            var result = new int[n];
            for (var i = 0; i < n; i++) result[i] = this.NextInt();
#if LOCAL_ENVIRONMENT
            Console.Error.WriteLine("{0}", result.ToStringA());
#endif
            return result;
        }
        public long[] NextLongArray(int n) {
            if (n < 1) throw new IndexOutOfRangeException("引数は1以上であること！");
            var result = new long[n];
            for (var i = 0; i < n; i++) result[i] = this.NextLong();
#if LOCAL_ENVIRONMENT
            Console.Error.WriteLine("{0}", result.ToStringA());
#endif
            return result;
        }
        public string[] NextStringArray(int n) {
            if (n < 1) throw new IndexOutOfRangeException("引数は1以上であること！");
            var result = new string[n];
            for (var i = 0; i < n; i++) result[i] = this.NextString();
#if LOCAL_ENVIRONMENT
            Console.Error.WriteLine("{0}", result.ToStringA());
#endif
            return result;
        }
        public int[][] NextInt2Col(int n) {
            const int M = 2;
            if (n < 1) throw new IndexOutOfRangeException("引数は1以上であること！");
            int[][] result = new int[M][] {
                new int[n],
                new int[n],
            };
            for (var i = 0; i < n; i++) {
                result[0][i] = this.NextInt();
                result[1][i] = this.NextInt();
            }
#if LOCAL_ENVIRONMENT
            Console.Error.WriteLine("NextInt2Col: [");
            for (var i = 0; i < M; i++)
                Console.Error.WriteLine("  {0},", result[i].ToStringA());
            Console.Error.WriteLine("]");
#endif
            return result;
        }
    }

}
static class Extensions {
    public static string ToStringA<T>(this T[] array, string separator = ", ", string braces = "[]") {
        string bL = braces.Length > 0 ? braces[0].ToString() : "";
        string bR = braces.Length > 1 ? braces[1].ToString() : "";
        return bL + String.Join(separator, array) + bR;
    }
}