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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Main{


	static final int MOD = 1000000007;

	static int n;
	static int k;
	static int[] v;


	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);			//文字の入力
		n = sc.nextInt();
		k = sc.nextInt();
		v = new int[n];
		for(int i = 0;i < n;i++){
			v[i] = sc.nextInt();
		}
		int ans = 0;

		for(int l = 0;l < k;l++){
			for(int r = 0;l+r < k;r++){
				ArrayList<Integer> list = sum(l,r);
				int deletenum = k - (r + l);
				ans = Math.max(ans, delete(deletenum,list));
			}
		}
		System.out.print(ans);

	}

	static ArrayList<Integer> sum(int l,int r){
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i = 0;i < l;i++){
			if(i >= n){
				break;
			}
			list.add(v[i]);
		}
		for(int i = 0;i < r;i++){
			if(i >= n){
				break;
			}
			list.add(v[n-1-i]);
		}

		return list;
	}

	static int delete(int num,ArrayList<Integer> list){
		int tmp = 0;
		Collections.sort(list);		
		
		for(int i = 0;i < list.size();i++){
			tmp += list.get(i);
		}
		int count = 0;
		for(int i = 0;i < list.size();i++){
			if(list.get(i) < 0){
				tmp += -list.get(i);
				count++;
				if(count >= num){
					break;
				}
			}else{
				break;
			}
		}

		return tmp;
	}




	//
	//
	//	static long fac[] = new long[MAX];
	//	static long finv[] = new long[MAX];
	//	static long inv[] = new long[MAX];
	//
	//
	//
	//	//テーブルの作成
	//	static void COMinit() {
	//		fac[0] = fac[1] = 1;
	//		finv[0] = finv[1] = 1;
	//		inv[1] = 1;
	//		for (int i = 2; i < MAX; i++){
	//			fac[i] = fac[i - 1] * i % MOD;
	//			inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
	//			finv[i] = finv[i - 1] * inv[i] % MOD;
	//		}
	//	}
	//
	//	// 二項係数計算
	//	static	long COM(int n, int k){
	//		if (n < k) return 0;
	//		if (n < 0 || k < 0) return 0;
	//		return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
	//	}
	//
	//






	static int upperbond(int k,ArrayList<Integer> data){		//k以下で最大の値をdataの中から探し、返す二分探索
		if(data == null){
			return -1;
		}
		int max = data.size();
		int min = -1;
		while(max-min > 1){
			int mid = (max+min)/2;
			if(data.get(mid) <= k){
				min = mid;
			}else{
				max = mid;
			}
		}


		//		if(min != -1){
		//			min = data.get(min);
		//		}

		//		System.out.println(max);
		return min;					//indexを返している
	}

	static int lowerbond(int k,ArrayList<Integer> data){		//k以上で最小の値をdataの中から探し、返す二分探索
		if(data == null){
			return -1;
		}
		//		System.out.println("lo");
		//		System.out.println("lo");
		int max = data.size();
		int min = -1;

		while(max-min > 1){
			int mid = (max+min)/2;
			if(data.get(mid) >= k){
				max = mid;
			}else{
				min = mid;
			}
		}

		return max;			//indexを返している
	}



	static int gcd(int a,int b){				//最大公約数を返す
		if(b == 0){
			return a;
		}else{
			return gcd(b, a%b);
		}
	}
	static long gcd(long a,long b){
		if(b == 0){
			return a;
		}else{
			return gcd(b, a%b);
		}
	}



	static long lcm (long a, long b) {
		long g = gcd(a,b);
		return a/g*b;
	}
}



class TreeNode{
	int value;
	ArrayList<TreeNode> children;
	boolean root;
	boolean fchild;

	public TreeNode(int value,ArrayList<TreeNode> children,boolean root) {
		this.value = value;
		this.children = children;
		this.root = root;
	}


}

class Pair implements Comparable{
	int from;
	int end;
	public Pair(int from,int end) {
		this.from = from;
		this.end = end;
		// TODO 自動生成されたコンストラクター・スタブ
	}

	@Override
	public int compareTo(Object other) {
		Pair otherpair = (Pair)other;

		return  from - otherpair.from;
	}








}


class Data{

	long value;
	int num;
	public Data(long v,int n) {
		value = v;
		num = n;
		// TODO 自動生成されたコンストラクター・スタブ
	}
}