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 Rust (1.15.1) (judge verdict: Runtime Error)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
 
use std::cmp::Ordering;
use std::cmp;
use std::cmp::min;
use std::collections::BTreeMap;
use std::process;
use std::cmp::Ord;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::collections::BTreeSet;
use std::mem;
use std::collections::BinaryHeap;
/// Equivalent to std::lowerbound and std::upperbound in c++
pub trait BinarySearch<T> {
    fn lower_bound(&self, &T) -> usize;
    fn upper_bound(&self, &T) -> usize;
}
 
impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
 
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
 
    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
 
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}
 
fn matmul(A:&Vec<Vec<i64>>, B:&Vec<Vec<i64>>) -> Vec<Vec<i64>>{
    let mut C = vec![vec![0;B[0].len()];A.len()];
    for i in 0..A.len(){
        for k in 0..B.len(){
            for j in 0..B[0].len(){
                C[i][j] += A[i][k]*B[k][j];
                C[i][j] %= MOD;
            }
        }
    }
    return C;
}
fn matpow(A:&mut Vec<Vec<i64>>, n:usize) -> Vec<Vec<i64>>{
    let mut B = vec![vec![0;A.len()];A.len()];
    for i in 0..A.len(){
        B[i][i] = 1;
    }
    let mut n = n;
    let mut tmp = A.clone();
    while(n>0){
        if n&1 == 1{B = matmul(&B, &tmp);}
        tmp = matmul(&tmp, &tmp);
        n>>=1;
    }
    return B;
}

 
 
 

/*
subset enumeration
let mut set;
let mut subset = set;
        for sm2 in 0..1<<n{
            if subset==0{
                break;
            }

            hogehoge


            subset = (subset-1)&set;
        }
*/
/*
全方位木
    fn dfs(s:usize,bef:usize,m:i64, g:&Vec<Vec<usize>>, dp:&mut Vec<Vec<i64>>)->i64{
        let mut res = 1;
        for i in 0..g[s].len(){
            let e = g[s][i];
            if e != bef{
                dp[s][i] = dfs(e, s, m, g, dp);
                res *= (1 + dp[s][i]);
                res %= m;
            }
        }
        return res;
    }
    let tmp = dfs(0, n, m, &g, &mut dp);
    fn dfs2(s:usize, bef:usize, p:i64, m:i64, g:&Vec<Vec<usize>>, dp:&mut Vec<Vec<i64>>){
        for i in 0..g[s].len(){
            let e = g[s][i];
            if e == bef{dp[s][i] = p;}
        }
        let mut L = vec![0;g[s].len()];
        let mut R = vec![0;g[s].len()];

        for i in 0..g[s].len(){
            L[i] = {
                let mut tmp = 1+dp[s][i];
                tmp %= m;
                if i != 0{
                    tmp *= L[i-1];
                }
                tmp%m
            }
        }

        for i in (0..g[s].len()).rev(){
            if i+1<g[s].len(){
                R[i] = (R[i+1]*((1+dp[s][i])%m))%m;
            }
            else{
                R[i] = (1+dp[s][i])%m;
            }

            if g[s][i] == bef{
                continue;
            }  
            let mut tmp = 1;
            if i != 0{
                tmp *= L[i-1];
                tmp %= m;
            }
            if i+1 != g[s].len(){
                tmp *= R[i+1];
                tmp %= m;
            }
            dfs2(g[s][i], s, tmp, m, g, dp);
        }
    }

    */
fn mpow(x:i64, n:i64) -> i64{
    let mut ans = 1;
    let mut n = n;
    let mut x = x;
    while(n != 0){
        if (n&1 == 1){ans = ans*x%MOD;}
        x = x*x%MOD;
        n = n>>1;
    }
    ans
}
fn comb(a:i64, b:i64, fac:&Vec<i64>, ifac:&Vec<i64>) -> i64{
    if a == 0 && b == 0{return 1;}
    if a<b || a<0 {return 0;}
    let mut tmp = ifac[(a-b) as usize]*ifac[b as usize] %MOD;
    return tmp*fac[a as usize] %MOD;
}
fn divisor(n:usize) -> Vec<usize>{
    let mut res:Vec<usize> = Vec::new(); 
    for i in 1..n+1{
        if i*i>n{break;}
        if n%i == 0{
            res.push(i);
            if i != n/i{
                res.push(n/i);    
            }    
        }    
    }
    res
}
struct UnionFind{
    par:Vec<usize>,
    rank:Vec<usize>,
    size:Vec<usize>,
}
impl UnionFind{
    fn init(n:usize) -> UnionFind{
        let mut par = vec![0;n];
        for i in 0..n{
            par[i] = i;
        }
        UnionFind{
            par:par,
            rank:vec![0;n],
            size:vec![1;n],
        }
    }
    fn find(&mut self, x:usize) ->usize{
        if(self.par[x] == x){
            x
        }
        else{
            let p = self.par[x];
            let res = self.find(p);
            self.par[x] = res;
            res
        }
    }
    fn same(&mut self, a:usize, b:usize)->bool{
        self.find(a) == self.find(b)
    }
    fn unite(&mut self, a:usize, b:usize){
        let x = self.find(a);
        let y = self.find(b);
        if x != y{
            if (self.rank[x] < self.rank[y]){
                self.par[x] = y;
                self.size[y] += self.size[x];
            }
            else{
                self.par[y] = x;
                self.size[x] += self.size[y];
                if(self.rank[x] == self.rank[y]){ self.rank[x]+=1;}
            }
        }
    }
    fn check_size(&mut self, a:usize) -> usize{
        let x = self.find(a);
        let s = self.size[x];
        s
    }
}
pub static MOD:i64 = 1000000007;
const INF: i64 = 1 << 60;
fn main(){
    input!{
        n:usize,
        k:usize,
        v:[i64;n],
    }
    let mut vr = v.clone();
    vr.reverse();
    let mut res = 0;
    for kt in 0..k+1{
        for l in 0..kt+1{
            let mut tmp = Vec::new();
            let mut ret = 0;
            for i in 0..l{
                tmp.push(v[i]);
                ret += v[i];
            }
            for i in cmp::min(n, cmp::max(l, n-cmp::min(n, (kt-l))))..n{
                tmp.push(v[i]);
                ret += v[i];
            }
            tmp.sort();
            for i in 0..cmp::min(tmp.len(), (k-kt)){
                if tmp[i] < 0{
                    ret += tmp[i]*-1;
                }
            }
            res = cmp::max(ret, res);
        }
    }
    println!("{}", res);

}