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.Linq;
using static Input;
public class Hello
{
    private const long INF = 1000000007;
    private struct Pair { public long x, y; public Pair(long xx, long yy) { x = xx; y = yy; } }
    public static void Main()
    {
        int N = NextInt();
        int K = NextInt();
        int[] V = LineInt();
        //右からK回、左からK回の組み合わせ
        Dictionary<int, Dictionary<int, int>> d0 = new Dictionary<int, Dictionary<int, int>>();
        Dictionary<int, Dictionary<int, int>> d1 = new Dictionary<int, Dictionary<int, int>>();
        int[] Left = new int[150];
        int[] Right = new int[150];

        for (int i = 0; i < N; i++)
        {
            //とりあえずiまで足してみる
            //許すならマイナスを追い出していく
            PriorityQueue<int, int> Minus = new PriorityQueue<int, int>();
            int sum = 0;
            for (int x = 0; x <= i; x++)
            {
                if (V[x] < 0) { Minus.Add(V[x], V[x]); }
                sum += V[x];
            }
            //iまでなので
            d0[i] = new Dictionary<int, int>();
            d0[i][i + 1] = sum;
            //Left[i] = Math.Max(Left[i], sum);
            //返してみる
            int cost = 0;
            while (Minus.Count() != 0)
            {
                sum -= Minus.Take();
                cost++;
                d0[i][i + cost + 1] = sum;
                //Left[i + cost] = Math.Max(Left[i + cost], sum);
            }
        }
        Array.Reverse(V);

        for (int i = 0; i < N; i++)
        {
            //とりあえずiまで足してみる
            //許すならマイナスを追い出していく
            PriorityQueue<int, int> Minus = new PriorityQueue<int, int>();
            int sum = 0;
            for (int x = 0; x <= i; x++)
            {
                if (V[x] < 0) { Minus.Add(V[x], V[x]); }
                sum += V[x];
            }
            d1[i] = new Dictionary<int, int>();
            d1[i][i + 1] = sum;
            //Right[i] = Math.Max(Right[i], sum);
            //返してみる
            int cost = 0;
            while (Minus.Count() != 0)
            {
                sum -= Minus.Take();
                cost++;
                d1[i][i + cost + 1] = sum;
                //Right[i + cost] = Math.Max(Right[i + cost], sum);
            }
        }
        int ans = 0;
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                for (int k0 = 0; k0 <= K; k0++)
                    for (int k1 = 0; k1 <= K; k1++)
                    {
                        if (k0 + k1 > K || i + j + 1 >= N) break;
                        if (!d0[i].ContainsKey(k0)) d0[i][k0] = 0;
                        if (!d1[j].ContainsKey(k1)) d1[j][k1] = 0;
                        ans = Math.Max(ans, d0[i][k0] + d1[j][k1]);
                    }
        Console.WriteLine(ans);
    }

}

public class PriorityQueue<Tkey, Tvalue> where Tkey : IComparable
{
    private List<Tkey> keys = new List<Tkey>();
    private List<Tvalue> values = new List<Tvalue>();
    private int size = 0;
    private static bool Descending;
    private static int Compare(Tkey a, Tkey b) { return (Descending ? a.CompareTo(b) : b.CompareTo(a)); }

    public PriorityQueue(bool b = true) { Clear(); Descending = b; }
    public int Count() { return size; }
    public void Clear() { keys = new List<Tkey>(); values = new List<Tvalue>(); size = 0; }
    public void Add(Tkey e, Tvalue v)
    {
        keys.Add(e);
        values.Add(v);
        int x = size;
        size += 1;
        while (x != 0)
        {
            int par = (x % 2 == 0 ? x - 2 : x - 1) / 2;
            if (Compare(keys[par], e) <= 0) break;
            Tkey a = keys[x]; keys[x] = keys[par]; keys[par] = a;
            Tvalue b = values[x]; values[x] = values[par]; values[par] = b;
            x = par;
        }
    }
    public void Poll()
    {
        Tkey k = keys[size - 1];
        Tvalue v = values[size - 1];
        int x = 0;
        size--;
        if (size == 0) { Clear(); return; }
        keys.RemoveAt(size);
        values.RemoveAt(size);
        while (true)
        {
            int l = x * 2 + 1;
            int r = x * 2 + 2;
            if (size - 1 < l) break;
            int y = 0;
            if (size - 1 == l) { y = l; }
            else if (Compare(keys[l], keys[r]) == 1) { y = r; }
            else { y = l; }
            if (Compare(keys[y], k) > 0) break;
            keys[x] = keys[y];
            values[x] = values[y];
            x = y;
        }
        keys[x] = k;
        values[x] = v;
    }
    public Tvalue Take() { Tvalue ret = values[0]; Poll(); return ret; }
    public Tvalue Peek() { return values[0]; }
    public void Debug()
    {
        Console.Write("key   : ");
        foreach (var k in keys) Console.Write(k + " ");
        Console.WriteLine();
        Console.Write("value : ");
        foreach (var k in values) Console.Write(k + " ");
        Console.WriteLine();
    }
}

public class Input
{
    private static Queue<string> queue = new Queue<string>();
    private static void Confirm() { if (queue.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(queue.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(queue.Dequeue()); }
    public static string NextString() { Confirm(); return queue.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(queue.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}