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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

/*#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <array>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
*/
 
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define SORT(c) sort((c).begin(),(c).end())
 
typedef long long ll;
const ll INF = LLONG_MAX / 8 - 100;
const ll mod = 1e9 + 7;
const int MAX_N = 2e5 + 5;
int dx[] = { -1LL,0,1LL,0 }, dy[] = { 0,1LL,0,-1LL };
vector<ll> prime;
 
ll inv[MAX_N], fac[MAX_N];
 
template <class T = ll> T in() { T x; cin >> x; return (x); }
inline ll GCD(ll a, ll b) { ll c; while (b != 0) { c = a % b; a = b; b = c; }return a; }
inline ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
inline ll POW(ll a, ll b) { ll c = 1LL; while (b > 0) { if (b & 1LL) { c = a * c%mod; }a = a * a%mod; b >>= 1LL; }return c; }
inline void _nCr() { fac[0] = 1LL; for (int i = 1LL; i < MAX_N; i++) { fac[i] = fac[i - 1LL] * i%mod; }for (int i = 0; i < MAX_N; i++) { inv[i] = POW(fac[i], mod - 2); } }
inline ll nCr(ll n, ll r) { return (fac[n] * inv[r] % mod)*inv[n - r] % mod; }
inline void PRI(ll n) { bool a[n + 1LL]; for (int i = 0; i < n + 1LL; i++) { a[i] = 1LL; }for (int i = 2; i < n + 1LL; i++) { if (a[i]) { prime.pb(i); ll b = i; while (b <= n) { a[b] = 0; b += i; } } } }
 
typedef pair<int, pair<int, int>> edge;
 
class UnionFind {
public:
	vector<int> par;
	vector<int> color;
	UnionFind(int N) { par = vector<int>(N, -1LL); color = vector<int>(N,-1);}
	int find(int x);
	ll size(int x);
	bool unite(int x, int y);
	bool same(int x, int y);
};
 
 
 
class Kruskal {
private:
	UnionFind *uf;
	vector<edge> e;
public:
	vector<edge> mst;
	Kruskal(int N) { uf = new UnionFind(N); }
	void add(int x, int y, ll z);
	void run();
};
 
 
 
//----UnionFind-------------------------------
int UnionFind::find(int x) {
	if (par[x] < 0) return x;
	else return par[x] = find(par[x]);
}
 
ll UnionFind::size(int x) {
	return -par[find(x)];
}
 
bool UnionFind::unite(int x, int y) {
	x = find(x);
	y = find(y);
 	if (x == y) return true;
	//大きい方に小さい方をくっ付ける
	if (size(x) < size(y)) swap(x, y);
	par[x] += par[y];
	par[y] = x;
	if (color[x] != -1 && color[y] != -1 && color[x] != color[y]) return false;
	else color[x] = max(color[x],color[y]);
	return true;
}
 
bool UnionFind::same(int x, int y) {
	x = find(x);
	y = find(y);
	return x == y;
}
 
 
 
//----Kruskal-------------------------------
void Kruskal::add(int x, int y, ll z) {
	//x < y
	if (x > y) swap(x, y);
	e.push_back({ z,{x,y} });
}
 
void Kruskal::run() {
	sort(e.begin(), e.end());
	e.erase(unique(e.begin(), e.end()), e.end());
	for (auto x : e) {
		if (uf->same(x.second.first, x.second.second)) {
			continue;
		}
		else {
			mst.push_back(x);
			uf->unite(x.second.first, x.second.second);
		}
	}
}

class Warshall_Floyd {
public:
	vector<vector<int>> d;
	int nodecnt = -1;

	Warshall_Floyd(int n) {
		d = vector<vector<int>>(n,vector<int>(n,INF));
		nodecnt = n;
	}

	void add(int s,int e,int cost) {
		d[s][e] = cost;
	}

	bool wf() {
		for (int k = 0;k < nodecnt;k++) {
			for (int i = 0;i < nodecnt;i++) {
				for (int j = 0;j < nodecnt;j++) {
					//k経由でi->j
					d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
				}
			}
		}
	}
};

signed main() {
	int n,k; cin >> n >> k;
	int v[n]; REP (i,n) {cin >> v[i];}
	int val1[105][105], val2[105][105];
	val1[0][0] = 0, val2[0][0] = 0;
	vector<int> A;
	FOR (i,1,min(n+1,k+1)) {
		A.push_back(v[i-1]);
		sort(ALL(A));
		REP (j,i+1) {
			if (i + j > k) break;
			if (j == 0) val1[i][j] = val1[i-1][j] + v[i-1];
			else {
				val1[i][j] = max(val1[i-1][j] + v[i-1],val1[i][j-1] - A[j-1]);
			}
		}
	}
	A.clear();
	FOR (i,1,min(n+1,k+1)) {
		A.push_back(v[n-i]);
		sort(ALL(A));
		REP (j,i+1) {
			if (i + j > k) break;
			if (j == 0) val2[i][j] = val2[i-1][j] + v[n-i];
			else {
				val2[i][j] = max(val2[i-1][j] + v[n-i],val2[i][j-1] - A[j-1]);
			}
		}
	}
	int m1[k+1],m2[k+1];
	REP (i,k+1) m1[i] = m2[i] = 0;
	REP (i,min(n+1,k+1)) {
		REP (j,i+1) {
			if (i+j>k) break;
			m1[i+j] = max(m1[i+j],val1[i][j]);
			m2[i+j] = max(m2[i+j],val2[i][j]);
		}
	}
	int ans = 0;
	REP (i,k+1) {
		REP (j,k+1) {
			if (i+j>k) break;
			ans = max(ans,m1[i]+m2[j]);
		}
	}
	cout<< ans << endl;
}