#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    int n,k;
    cin>>n>>k;
    vector<int> l(n);
    rep(i,n) cin>>l[i];
    sort(all(l));
    int ans=0;
    rep(i,k) ans+=l[n-1-i];
    cout<<ans;
}
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
#include <math.h>
#include <queue>
#include <deque>
#include <stack>
#include <map>
const int MOD = 1e9+7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;


int main(){
	int n, k;
	cin >> n >> k;
	vector<int> v(n);
	for (int i = 0; i < n; i++){
		cin >> v[i];
	}

	ll ans = 0;
	for (int all = 0; all <= min(k, n); all++){
		for (int i = 0; i <= all; i++){
			int l = i, r = all - i;
			int rem = k - all;
			priority_queue<int, vector<int>, greater<int> > que;
			for (int j = 0; j < l; j++){
				que.push(v[j]);
			}
			for (int j = 0; j < r; j++){
				que.push(v[n-j-1]);
			}
			ll tmp = 0;
			while (!que.empty()){
				if (que.top() < 0 && rem > 0){
					rem--;
					que.pop();
				}else{
					tmp += que.top();
					que.pop();
				}
			}
			ans = max(ans, tmp);
		}
	}
	cout << ans << endl;
	return 0;
}