/// kAAzuki Hoshino
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
#define inf 1000000000
#define mod 1000000007
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define rep1(i, a, b) for(int i =a; i >=b; i--)
#define dmp(x) cerr<<"line "<<__LINE__<<" "<<#x<<":"<<x<<endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define mt make_tuple
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}

int n,k,a[1010],ans;

signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);

        cin >> n >> k;

        for(int i=1;i<=n;i++){
                cin >> a[i];
        }

        for(int i=1;i<=min(k,n);i++){
                        vi neg,pos;
                        int res = 0;
                        int cur = k-i;
                        for(int l=1;l<=i;l++){
                                res+=a[l];
                                if( a[l]>0 )pos.pb(a[l]);
                                else neg.pb(a[l]);
                        }
                        sort(all(pos));
                        sort(all(neg));
                        //cout << neg << endl;
                        reverse(all(neg));
                        int m = neg.size();
                       // cout << res << endl;
                        for( int l=m-1;l>=0;l-- ){
                                if(cur){res -= neg[l];cur--;}
                        }
                        //cout << res << endl;
                        ans = max(ans, res);
        }
        for(int i=n;i>=max(1LL*0,n-k+1);i--){
                        vi neg,pos;
                        int res = 0;
                        int cur = ( k - ( n-i+1 ) );
                        for(int l=n;l>=i;l--){
                                res+=a[l];
                                if( a[l]>0 )pos.pb(a[l]);
                                else neg.pb(a[l]);
                        }
                        sort(all(pos));
                        sort(all(neg));
                        //cout << neg << endl;
                        reverse(all(neg));
                        int m = neg.size();
                       // cout << res << endl;
                        for( int l=m-1;l>=0;l-- ){
                                if(cur){res -= neg[l];cur--;}
                        }
                        //cout << res << endl;
                        ans = max(ans, res);
        }
        //cout << ans << endl;
        for(int i=1;i<=n;i++){
                for(int j=i+1;j<=n;j++){

                        if( i + (n-j+1) > k  )continue;
                        int cur = k- ( i + n-j+1 );
                        vi neg,pos;
                        int res = 0;
                        for(int l=1;l<=i;l++){
                                res+=a[l];
                                if( a[l]>0 )pos.pb(a[l]);
                                else neg.pb(a[l]);
                        }
                        for(int l=n;l>=j;l--){
                                res+=a[l];
                                if( a[l]>0 )pos.pb(a[l]);
                                else neg.pb(a[l]);
                        }
                        sort(all(pos));
                        sort(all(neg));
                        reverse(all(neg));
                        int m = neg.size();

                        for( int l=m-1;l>=0;l-- ){
                                if(cur){res -= neg[l];cur--;}
                        }
                        ans = max(ans, res);
                }
        }

        cout << ans << endl;



return 0;
}
///....


Measured memory_bytes: 256