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 (GCC 5.4.1) (judge verdict: Accepted)

#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_JEWEL_MAX		100										// 最大宝石数

// 内部変数
static FILE *szpFpI;											// 入力
static int si1Jewel[D_JEWEL_MAX];								// 宝石
static int siJCnt;												// 宝石数
static int siOCnt;												// 操作回数

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
	static int siTNo;
#endif

// 出力
int
fOut(
	char *pcpLine				// <I> １行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// ソート関数 - int降順
int
fSortFnc(
	const void *pzpVal1			// <I> 値１
	, const void *pzpVal2		// <I> 値２
)
{
	int *lipVal1 = (int *)pzpVal1;
	int *lipVal2 = (int *)pzpVal2;

	// int降順
	if (*lipVal1 > *lipVal2) {
		return -1;
	}
	else if (*lipVal1 < *lipVal2) {
		return 1;
	}

	return 0;
}

// 実行メイン
int
fMain(
)
{
	int i, j, k;
	char lc1Buf[1024];

	// 宝石数・操作回数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d%d", &siJCnt, &siOCnt);

	// 宝石 - 取得
	for (i = 0; i < siJCnt; i++) {
		fscanf(szpFpI, "%d", &si1Jewel[i]);
	}
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);

	int liMax = 0;

	// 左側の個数
	int liLMax;
	if (siJCnt < siOCnt) {
		liLMax = siJCnt;
	}
	else {
		liLMax = siOCnt;
	}
	for (i = 0; i <= liLMax; i++) {
		int liJCnt = siJCnt - i;
		int liOCnt = siOCnt - i;

		// 右側の個数
		int liRMax;
		if (liJCnt < liOCnt) {
			liRMax = liJCnt;
		}
		else {
			liRMax = liOCnt;
		}
		for (j = 0; j <= liRMax; j++) {

			// 対象宝石数
			int liTCnt = i + j;
			if (liTCnt < 1) {
				continue;
			}

			// 対象宝石
			int li1Jewel[D_JEWEL_MAX];
			if (i > 0) {
				memcpy(li1Jewel, si1Jewel, sizeof(int) * i);
			}
			if (j > 0) {
				memcpy(&li1Jewel[i], &si1Jewel[siJCnt - j], sizeof(int) * j);
			}
			qsort(li1Jewel, liTCnt, sizeof(int), fSortFnc);

			// 負の宝石削除
			int liDCnt = liOCnt - j;
			for (k = 0; k < liDCnt; k++) {
				if (li1Jewel[liTCnt - 1] < 0) {
					liTCnt--;
				}
				else {
					break;
				}
			}

			// 価値合計 - 取得
			int liSum = 0;
			for (k = 0; k < liTCnt; k++) {
				liSum += li1Jewel[k];
			}

			// 最大値 - 更新
			if (liMax < liSum) {
				liMax = liSum;
			}
		}
	}

	return liMax;
}

// １回実行
int
fOne(
)
{
	int liRet;
	char lc1Buf[1024];

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", siTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", siTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 実行メイン
	liRet = fMain();

	// 出力
	sprintf(lc1Buf, "%d\n", liRet);
	fOut(lc1Buf);

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		siTNo = i;
		fOne();
	}
#else
	fOne();
#endif

	return 0;
}