#include<bits/stdc++.h>
using namespace std;

vector<int> graf[100005];
int col[100005];
void dfs(int node){
	for(auto child : graf[node]){
		if (col[child]==-1) {
			col[child]=1-col[node];
			dfs(child);
		} else if (col[node] + col[child] != 1) {
			cout<<-1<<"\n";
			exit(0);
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0);

	memset(col, -1, sizeof col);
	int n, m;
	cin>>n>>m;

	for(int i = 0; i < m; i++){
		int u, v;
		cin>>u>>v;

		graf[u].push_back(v);
		graf[v].push_back(u);
	}
	col[1] = 1;
	dfs(1);

		vector<int> a;
		vector<int> b;

		for(int i = 1; i <= n; i++)
			if(col[i] == 0)
				a.push_back(i);
			else
				b.push_back(i);

		cout<<a.size()<<"\n";
		for(auto i : a)
			cout<<i<<" ";
		cout<<"\n";

		cout<<b.size()<<"\n";
		for(auto i : b)
			cout<<i<<" ";
		cout<<"\n";
	return 0;

}


#include<bits/stdc++.h>
using namespace std;

vector<int> graf[100005];
int col[100005];
int ok = 1;
int dfs(int node){
	for(auto child : graf[node]){
		//cout<<" To "<<child<<" from "<<node<<" "<<col[child]<<" "<<col[node]<<"\n";
		if(col[child] != -1 && col[child] == col[node])
			return 0;
		else if(col[child] == 1 - col[node]) continue;
		col[child] = 1 - col[node];
		ok &= dfs(child);
	}
	return ok;
}

int main(){
	ios_base::sync_with_stdio(0);

	memset(col, -1, sizeof col);
	int n, m;
	cin>>n>>m;

	for(int i = 0; i < m; i++){
		int u, v;
		cin>>u>>v;

		graf[u].push_back(v);
		graf[v].push_back(u);
	}
	col[1] = 1;
	int val = dfs(1);

	if(val == 0)
		cout<<-1<<"\n";
	else{
		vector<int> a;
		vector<int> b;

		for(int i = 1; i <= n; i++)
			if(col[i] == 0)
				a.push_back(i);
			else
				b.push_back(i);

		cout<<a.size()<<"\n";
		for(auto i : a)
			cout<<i<<" ";
		cout<<"\n";

		cout<<b.size()<<"\n";
		for(auto i : b)
			cout<<i<<" ";
		cout<<"\n";
	}
	return 0;

}


#include<bits/stdc++.h>
using namespace std;

vector<int> graf[100005];
int col[100005];

int dfs(int node){
	for(auto child : graf[node]){
		if(col[child] != -1 && col[child] == col[node])
			return 0;
		else if(col[child] == 1 - col[node]) continue;
		col[child] = 1 - col[node];
		dfs(child);
	}
	return 1;
}

int main(){
	ios_base::sync_with_stdio(0);

	memset(col, -1, sizeof col);
	int n, m;
	cin>>n>>m;

	for(int i = 0; i < m; i++){
		int u, v;
		cin>>u>>v;

		graf[u].push_back(v);
		graf[v].push_back(u);
	}
	col[1] = 1;
	int val = dfs(1);

	if(val == 0)
		cout<<-1<<"\n";
	else{
		vector<int> a;
		vector<int> b;

		for(int i = 1; i <= n; i++)
			if(col[i] == 0)
				a.push_back(i);
			else
				b.push_back(i);

		cout<<a.size()<<"\n";
		for(auto i : a)
			cout<<i<<" ";
		cout<<"\n";

		cout<<b.size()<<"\n";
		for(auto i : b)
			cout<<i<<" ";
		cout<<"\n";
	}
	return 0;

}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<string>
#include<bitset>
#include<iomanip>
#include<iostream>
#include<cmath>
using namespace std;
#define rep(i,x,y) for(i=x;i<=y;i++)
#define _rep(i,x,y) for(i=x;i>=y;i--)
#define REP(i,x,y) for(int i=(x);i<=(y);i++)
#define _REP(i,x,y) for(int i=(x);i>=(y);i--)
#define CL(S,x) memset(S,x,sizeof(S))
#define CP(S1,S2) memcpy(S1,S2,sizeof(S2))
#define ALL(x,S) for(x=S.begin();x!=S.end();x++)
#define pb push_back
#define IN insert
#define ER erase
#define BE begin()
#define ED end()
#define LB lower_bound
#define UB upper_bound
#define mp make_pair
#define fi first
#define se second
#define upmin(x,y) x=min(x,y)
#define upmax(x,y) x=max(x,y)
#define COUT(S,x) cout<<fixed<<setprecision(x)<<S<<endl
template<class T> inline void read(T&x){bool fu=0;char c;for(c=getchar();c<=32;c=getchar());if(c=='-')fu=1,c=getchar();for(x=0;c>32;c=getchar())x=x*10+c-'0';if(fu)x=-x;};
template<class T> inline void read(T&x,T&y){read(x);read(y);}
template<class T> inline void read(T&x,T&y,T&z){read(x);read(y);read(z);}
inline char getc(){char c;for(c=getchar();c<=32;c=getchar());return c;}

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

int n,i,j,k,l,p,a[210];
priority_queue<int> Q;
priority_queue<int,vector<int>,greater<int> > P;

int main()
{
    //freopen("1.in","r",stdin);freopen("1.out","w",stdout);
    read(n,k);
    rep(i,1,n)read(a[i]);
    int ans=-1000000;
    REP(st,1,n)
    REP(ed,st,n)
    {
        int res=0;
        Q=priority_queue<int>();
        P=priority_queue<int,vector<int>,greater<int> >();
        rep(i,1,n)if(!(i>=st&&i<=ed))
        if(a[i]>0)Q.push(a[i]);
        rep(i,1,n)if(i>=st&&i<=ed)res+=a[i],P.push(a[i]);
        p=k;
        while(!Q.empty())
        {
            res+=Q.top();Q.pop();
            if(--p==0)break;
            if(!P.empty())
            {
                if(P.top()<0)
                res-=P.top(),P.pop();
            }
        }
        upmax(ans,res);
    }
    cout<<ans<<endl;

    scanf("\n");
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<string>
#include<bitset>
#include<iomanip>
#include<iostream>
#include<cmath>
using namespace std;
#define rep(i,x,y) for(i=x;i<=y;i++)
#define _rep(i,x,y) for(i=x;i>=y;i--)
#define REP(i,x,y) for(int i=(x);i<=(y);i++)
#define _REP(i,x,y) for(int i=(x);i>=(y);i--)
#define CL(S,x) memset(S,x,sizeof(S))
#define CP(S1,S2) memcpy(S1,S2,sizeof(S2))
#define ALL(x,S) for(x=S.begin();x!=S.end();x++)
#define pb push_back
#define IN insert
#define ER erase
#define BE begin()
#define ED end()
#define LB lower_bound
#define UB upper_bound
#define mp make_pair
#define fi first
#define se second
#define upmin(x,y) x=min(x,y)
#define upmax(x,y) x=max(x,y)
#define COUT(S,x) cout<<fixed<<setprecision(x)<<S<<endl
template<class T> inline void read(T&x){bool fu=0;char c;for(c=getchar();c<=32;c=getchar());if(c=='-')fu=1,c=getchar();for(x=0;c>32;c=getchar())x=x*10+c-'0';if(fu)x=-x;};
template<class T> inline void read(T&x,T&y){read(x);read(y);}
template<class T> inline void read(T&x,T&y,T&z){read(x);read(y);read(z);}
inline char getc(){char c;for(c=getchar();c<=32;c=getchar());return c;}

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

int n,i,j,k,l,p,a[210];
priority_queue<int> Q;
priority_queue<int,vector<int>,greater<int> > P;

int main()
{
    //freopen("1.in","r",stdin);freopen("1.out","w",stdout);
    read(n,k);
    rep(i,1,n)read(a[i]);
    int ans=-1000000;
    REP(st,1,n)
    REP(ed,st,n)
    {
        int res=0;
        Q=priority_queue<int>();
        P=priority_queue<int,vector<int>,greater<int> >();
        rep(i,1,n)if(!(i>=st&&i<=ed))Q.push(a[i]);
        rep(i,1,n)if(i>=st&&i<=ed)res+=a[i],P.push(a[i]);
        p=k;
        while(1)
        {
            if(Q.empty()||P.empty())break;
            res+=Q.top();Q.pop();
            res-=P.top();P.pop();
            upmax(ans,res);
            if(--p==0)break;

        }

    }
    cout<<ans<<endl;

    scanf("\n");
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <string>
#include <ctime>
#include <cmath>
#include <set>
#include <map>

typedef long double LD;
typedef long long LL;

using namespace std;

#define sz(A) (A).size()
#define mp make_pair
#define pb push_back

const int N = 205;

int a[N], res = -int(1e9);

int main() {
    int n, K;
    cin >> n >> K;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    res = a[0];
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j <= n; j++) {
            int sum = 0;
            vector<int> other, in;
            for (int k = 0; k < n; k++) {
                if (k >= i && k < j) {
                    sum += a[k];
                    in.pb(a[k]);
                }
                else
                    other.pb(a[k]);
            }
            sort(in.begin(), in.end());
            sort(other.begin(), other.end());
            reverse(other.begin(), other.end());
            for (int k = 0; k < K; k++) {
                if (k == sz(other)) break;
                if (k == sz(in)) break;
                if (other[k] < in[k])
                    break;
                sum += other[k] - in[k];
            }
            res = max(res, sum);
        }

    cout << res << endl;

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <string>
#include <ctime>
#include <cmath>
#include <set>
#include <map>

typedef long double LD;
typedef long long LL;

using namespace std;

#define sz(A) (A).size()
#define mp make_pair
#define pb push_back

const int N = 105;

int a[N][N], n, m, k, b[N][N], res = int(1e9);

void dfs2(int x, int y, int now, int f) {
    if (f <= k - res)
        return;

    if (now == m) {
        res = min(res, k - f);
        if (res == 0) {
            cout << res << endl;
            exit(0);
        }
        return;
    }

    int ff;

    if (rand() & 1) {
        ff = 0;
        for (int i = 0; i < n && ff <= f; i++) {
            if (a[i][now] != b[i][now - 1])
                ff++;
            b[i][now] = b[i][now - 1];
        }

        if (ff <= f) {
            dfs2(x, y, now + 1, f - ff);
        }

        ff = 0;
        for (int i = 0; i < n && ff <= f; i++) {
            if (a[i][now] == b[i][now - 1])
                ff++;
            b[i][now] = 1 - b[i][now - 1];
        }

        if (ff <= f) {
            dfs2(x, y, now + 1, f - ff);
        }
    }
    else {
        ff = 0;
        for (int i = 0; i < n && ff <= f; i++) {
            if (a[i][now] == b[i][now - 1])
                ff++;
            b[i][now] = 1 - b[i][now - 1];
        }

        if (ff <= f) {
            dfs2(x, y, now + 1, f - ff);
        }

        ff = 0;
        for (int i = 0; i < n && ff <= f; i++) {
            if (a[i][now] != b[i][now - 1])
                ff++;
            b[i][now] = b[i][now - 1];
        }

        if (ff <= f) {
            dfs2(x, y, now + 1, f - ff);
        }
    }
}

void dfs(int x, int y, int now, int f) {
    if (f <= k - res)
        return;

    if (now == n) {
        dfs2(x, y, y, f);
        return;
    }

    int c, ff;

    if (rand() & 1) {
        c = b[now - 1][0], ff = 0;
        for (int i = 0; i < y && ff <= f; i++) {
            if (a[now][i] != c)
                ff++;
            b[now][i] = c;
        }
        if (ff <= f) {
            dfs(x, y, now + 1, f - ff);
        }

        c = 1 - c, ff = 0;
        for (int i = 0; i < y && ff <= f; i++) {
            if (a[now][i] != c)
                ff++;
            b[now][i] = c;
        }
        if (ff <= f) {
            dfs(x, y, now + 1, f - ff);
        }
    }
    else {
        c = 1 - c, ff = 0;
        for (int i = 0; i < y && ff <= f; i++) {
            if (a[now][i] != c)
                ff++;
            b[now][i] = c;
        }
        if (ff <= f) {
            dfs(x, y, now + 1, f - ff);
        }

        c = b[now - 1][0], ff = 0;
        for (int i = 0; i < y && ff <= f; i++) {
            if (a[now][i] != c)
                ff++;
            b[now][i] = c;
        }
        if (ff <= f) {
            dfs(x, y, now + 1, f - ff);
        }
    }
}

int main() {
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            for (int c = 0; c < 2; c++) {
                int f = 0;

                for (int q = 0; q < i && f <= k; q++)
                    for (int w = 0; w < j && f <= k; w++) {
                        if (a[q][w] != c) f++;
                        b[q][w] = c;
                    }
                if (f <= k) {
                    dfs(i, j, i, k - f);
                }
            }

    if (res == int(1e9)) {
        cout << -1;
        return 0;
    }
    cout << res;

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <string>
#include <ctime>
#include <cmath>
#include <set>
#include <map>

typedef long double LD;
typedef long long LL;

using namespace std;

#define sz(A) (A).size()
#define mp make_pair
#define pb push_back

const int N = 205;

int a[N], res = -int(1e9);

int main() {
    int n, K;
    cin >> n >> K;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    res = a[0];
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++) {
            int sum = 0;
            vector<int> other;
            for (int k = 0; k < n; k++) {
                if (k >= i && k < j)
                    sum += a[k];
                else
                    other.pb(a[k]);
            }
            sort(other.begin(), other.end());
            reverse(other.begin(), other.end());
            for (int k = 0; k < K; k++) {
                if (k == sz(other)) break;
                if (other[k] < 0)
                    break;
                sum += other[k];
            }
            res = max(res, sum);
        }

    cout << res << endl;

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <string>
#include <ctime>
#include <cmath>
#include <set>
#include <map>

typedef long double LD;
typedef long long LL;

using namespace std;

#define sz(A) (A).size()
#define mp make_pair
#define pb push_back

const int N = 205;

int a[N], res = -int(1e9);

int main() {
    int n, K;
    cin >> n >> K;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    res = a[0];
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++) {
            int sum = 0;
            vector<int> other, in;
            for (int k = 0; k < n; k++) {
                if (k >= i && k < j) {
                    sum += a[k];
                    in.pb(a[k]);
                }
                else
                    other.pb(a[k]);
            }
            sort(in.begin(), in.end());
            sort(other.begin(), other.end());
            reverse(other.begin(), other.end());
            for (int k = 0; k < K; k++) {
                if (k == sz(other)) break;
                if (k == sz(in)) break;
                if (other[k] < in[k])
                    break;
                sum += other[k] - in[k];
            }
            res = max(res, sum);
        }

    cout << res << endl;

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

typedef long long ll;

#define in(c) scanf("%I64d", &c)
#define out(c) printf("%I64d", c)
#define pll pair <ll, ll>
#define mp make_pair
#define pb push_back
#define x first
#define y second

ll n, m;
ll bg[201];
ll d[12][201];
ll g[12][201];
short a[12][201][201];

struct my
{
    int l, r;
    ll sum;
};

my up(int k, int r)
{
        ll rl = 0;
        ll tsum = 0;
        my res;
        res.sum = a[k][r][0];
        res.l = 0;
        res.r = 0;
        for (int i = 0; i < n; i++)
        {
            tsum += a[k][r][i];

            if (tsum > res.sum)
            {
                 res.l = rl;
                 res.r = i;
                 res.sum = tsum;
            }

            if (tsum < 0)
            {
                tsum = 0;
                rl = i + 1;
            }
        }

        return res;
}

void up2(int k, int r)
{
    for (int i = 0; i < n; i++)
    {
        if (g[k][r] < n)
            swap(a[k][r][g[k][r]], a[k][r][i]);

        my temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k + 1][temp.l] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        if (g[k][r] < n)
            swap(a[k][r][g[k][r]], a[k][r][i]);

        if (r > 0)
            swap(a[k][r][r - 1], a[k][r][i]);

        temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k + 1][temp.l] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        if (r > 0)
            swap(a[k][r][r - 1], a[k][r][i]);

        swap(a[k][r][r], a[k][r][i]);

        temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k + 1][temp.l] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        if (r > 0)
            swap(a[k][r][r], a[k][r][i]);

        swap(a[k][r][g[k][r] - 1], a[k][r][i]);

        temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k + 1][temp.l] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        swap(a[k][r][g[k][r] - 1], a[k][r][i]);

    }

}
ll up3(int k, int r)
{
        ll tsum = 0;
        ll res = a[k][r][r];
        g[k][r] = r + 1;
        for (int i = r; i < n; i++)
        {
            tsum += a[k][r][i];

            if (tsum > res)
            {
                g[k][r] = i + 1;
                res = tsum;
            }

            if (tsum < 0)
            {
                break;
            }
        }

        return res;
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> bg[i];
    }

    for (int i = 0; i <= m; i++)
       for (int j = 0; j < n; j++)
           d[i][j] = -100000LL * 100000LL;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            a[0][i][j] = bg[j];
        d[0][i] = up3(0, i);
    }

    for (int i1 = 0; i1 < m; i1++)
    {
        for (int i2 = 0; i2 < n; i2++)
        {
            up2(i1, i2);
        }
    }
    ll sum = - 1000000LL * 1000000LL;
    for (int j = 0; j < m; j++)
    for (int i = 0; i < n; i++)
    {
        if (sum < d[j][i])
        {
            sum = d[j][i];
        }
    }
        bool used = true;
        ll maxel = bg[0];
        for (int i = 0; i < n; i++)
        {
            if (bg[i] >= 0)
                used = false;
            if (bg[i] > maxel)
                maxel = bg[i];
        }

    if (used)
        cout << maxel;
    else
        cout << sum;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

typedef long long ll;

#define in(c) scanf("%I64d", &c)
#define out(c) printf("%I64d", c)
#define pll pair <ll, ll>
#define mp make_pair
#define pb push_back
#define x first
#define y second

ll n, m;
ll a[201];

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

    bool used = true;
    ll rl = 0, rr = 0, sum = a[0], maxel = a[0];
    for (int i = 0; i < n; i++)
    {
        if (a[i] >= 0)
            used = false;
        if (a[i] > maxel)
            maxel = a[i];
        for (int j = i; j < n; j++)
        {
            ll tsum = 0;
            for (int k = i; k <= j; k++)
                tsum += a[k];
            if (tsum > sum)
            {
                rl = i;
                rr = j;
                sum = tsum;
            }
        }
    }

    multiset <int> s;
    multiset <int> t;

    for (int i = 0; i < n; i++)
    {
        if (rl > i || rr < i)
            s.insert(a[i]);
        else
            t.insert(a[i]);
        s.insert(0);
    }

    for (int i = 0; i < m; i++)
    {
        multiset <int>::iterator s1 = s.end(); s1--;
        multiset <int>::iterator t1 = t.begin();
        if (*t1 < *s1)
        {
            sum -= *t1;
            sum += *s1;
            t.erase(t1);
            t.insert(*s1);
            s.erase(s1);
            s.insert(*t1);
        }
    }
    if (used)
        cout << maxel;
    else
        cout << sum;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

typedef long long ll;

#define in(c) scanf("%I64d", &c)
#define out(c) printf("%I64d", c)
#define pll pair <ll, ll>
#define mp make_pair
#define pb push_back
#define x first
#define y second

ll n, m;
ll bg[201];
ll d[12][201];
ll g[12][201];
short a[12][201][201];

struct my
{
    int l, r;
    ll sum;
};

my up(int k, int r)
{
        ll rl = 0;
        ll tsum = 0;
        my res;
        res.sum = a[k][r][0];
        res.l = 0;
        res.r = 0;
        for (int i = 0; i < n; i++)
        {
            tsum += a[k][r][i];

            if (tsum > res.sum)
            {
                 res.l = rl;
                 res.r = i;
                 res.sum = tsum;
            }

            if (tsum < 0)
            {
                tsum = 0;
                rl = i + 1;
            }
        }

        return res;
}

void up2(int k, int r)
{
    for (int i = 0; i < n; i++)
    {
        if (g[k][r] < n)
            swap(a[k][r][g[k][r]], a[k][r][i]);

        my temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k + 1][temp.l] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        if (g[k][r] < n)
            swap(a[k][r][g[k][r]], a[k][r][i]);

        if (r > 0)
            swap(a[k][r][r - 1], a[k][r][i]);

        temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k + 1][temp.l] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        if (r > 0)
            swap(a[k][r][r - 1], a[k][r][i]);

        swap(a[k][r][r], a[k][r][i]);

        temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k + 1][temp.l] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        swap(a[k][r][r], a[k][r][i]);

        swap(a[k][r][g[k][r] - 1], a[k][r][i]);

        temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k + 1][temp.l] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        swap(a[k][r][g[k][r] - 1], a[k][r][i]);

    }

}
ll up3(int k, int r)
{
        ll tsum = 0;
        ll res = a[k][r][r];
        g[k][r] = r + 1;
        for (int i = r; i < n; i++)
        {
            tsum += a[k][r][i];

            if (tsum > res)
            {
                g[k][r] = i + 1;
                res = tsum;
            }
        }

        return res;
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> bg[i];
    }

    for (int i = 0; i <= m; i++)
       for (int j = 0; j < n; j++)
           d[i][j] = -100000LL * 100000LL;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            a[0][i][j] = bg[j];
        d[0][i] = up3(0, i);
    }

    for (int i1 = 0; i1 < m; i1++)
    {
        for (int i2 = 0; i2 < n; i2++)
        {
            up2(i1, i2);
        }
    }
    ll sum = - 1000000LL * 1000000LL;
    for (int j = 0; j <= m; j++)
    for (int i = 0; i < n; i++)
    {
        if (sum < d[j][i])
        {
            sum = d[j][i];
        }
    }
        bool used = true;
        ll maxel = bg[0];
        for (int i = 0; i < n; i++)
        {
            if (bg[i] >= 0)
                used = false;
            if (bg[i] > maxel)
                maxel = bg[i];
        }

    if (used)
        cout << maxel;
    else
        cout << sum;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

typedef long long ll;

#define in(c) scanf("%I64d", &c)
#define out(c) printf("%I64d", c)
#define pll pair <ll, ll>
#define mp make_pair
#define pb push_back
#define x first
#define y second

ll n, k;
ll a[256], b[256], c[256];

int main()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i];

    ll sum = a[0];

    for (ll i = 0; i < n; i++)
        for (ll j = i; j < n; j++)
        {
                       for (int ij = i; ij <= j; ij++) b[ij - i] = a[ij];
            int u = 0; for (int ij = 0; ij < n; ij++) if (ij < i || j < ij) {c[u] = a[ij]; u++;}

            sort(b, b + (j - i + 1));
            sort(c, c + u);

            for (int ij = 0; ij < min(k, (j - i + 1)) && u > 0; ij++)
            {
                if (c[u - 1] > b[ij])
                {
                    b[ij] = c[u - 1];
                    u--;
                }
            }

            ll tsum = 0;
            for (int ij = 0; ij < (j - i + 1); ij++)
                tsum += b[ij];

            if (tsum > sum) sum = tsum;
        }

    cout << sum;

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

typedef long long ll;

#define in(c) scanf("%I64d", &c)
#define out(c) printf("%I64d", c)
#define pll pair <ll, ll>
#define mp make_pair
#define pb push_back
#define x first
#define y second

ll n, m;
ll bg[201];
ll d[12][201];
ll g[12][201];
short a[12][201][201];

struct my
{
    int l, r;
    ll sum;
};

my up(int k, int r)
{
        ll rl = 0;
        ll tsum = 0;
        my res;
        res.sum = a[k][r][0];
        res.l = 0;
        res.r = 0;
        for (int i = 0; i < n; i++)
        {
            tsum += a[k][r][i];

            if (tsum > res.sum)
            {
                 res.l = rl;
                 res.r = i;
                 res.sum = tsum;
            }

            if (tsum < 0)
            {
                tsum = 0;
                rl = i + 1;
            }
        }

        return res;
}

void up2(int k, int r)
{
    for (int i = 0; i < n; i++)
    {
        if (g[k][r] < n)
            swap(a[k][r][g[k][r]], a[k][r][i]);

        my temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k][r] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        if (g[k][r] < n)
            swap(a[k][r][g[k][r]], a[k][r][i]);

        if (r > 0)
            swap(a[k][r][r - 1], a[k][r][i]);

        temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k][r] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        if (r > 0)
            swap(a[k][r][r - 1], a[k][r][i]);

    }
}
ll up3(int k, int r)
{
        ll tsum = 0;
        ll res = a[k][r][r];
        g[k][r] = r + 1;
        for (int i = r; i < n; i++)
        {
            tsum += a[k][r][i];

            if (tsum > res)
            {
                g[k][r] = i + 1;
                res = tsum;
            }

            if (tsum < 0)
            {
                break;
            }
        }

        return res;
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> bg[i];
    }

    for (int i = 0; i <= m; i++)
       for (int j = 0; j < n; j++)
           d[i][j] = -100000LL * 100000LL;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            a[0][i][j] = bg[j];
        d[0][i] = up3(0, i);
    }

    for (int i1 = 0; i1 < m; i1++)
    {
        for (int i2 = 0; i2 < n; i2++)
        {
            up2(i1, i2);
        }
    }
    ll sum = - 1000000LL * 1000000LL;
    for (int i = 0; i < n; i++)
    {
        if (sum < d[m][i])
        {
            sum = d[m][i];
        }
    }
        bool used = true;
        ll maxel = a[0];
        for (int i = 0; i < n; i++)
        {
            if (a[i] >= 0)
                used = false;
            if (a[i] > maxel)
                maxel = a[i];
        }
    if (used)
        cout << maxel;
    else
        cout << sum;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

typedef long long ll;

#define in(c) scanf("%I64d", &c)
#define out(c) printf("%I64d", c)
#define pll pair <ll, ll>
#define mp make_pair
#define pb push_back
#define x first
#define y second

ll n, m;
ll bg[201];
ll d[12][201];
ll g[12][201];
short a[12][201][201];

struct my
{
    int l, r;
    ll sum;
};

my up(int k, int r)
{
        ll rl = 0;
        ll tsum = 0;
        my res;
        res.sum = a[k][r][0];
        res.l = 0;
        res.r = 0;
        for (int i = 0; i < n; i++)
        {
            tsum += a[k][r][i];

            if (tsum > res.sum)
            {
                 res.l = rl;
                 res.r = i;
                 res.sum = tsum;
            }

            if (tsum < 0)
            {
                tsum = 0;
                rl = i + 1;
            }
        }

        return res;
}

void up2(int k, int r)
{
    for (int i = 0; i < n; i++)
    {
        if (g[k][r] < n)
            swap(a[k][r][g[k][r]], a[k][r][i]);

        my temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k][r] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        if (g[k][r] < n)
            swap(a[k][r][g[k][r]], a[k][r][i]);

        if (r > 0)
            swap(a[k][r][r - 1], a[k][r][i]);

        temp = up(k, r);

        if (d[k + 1][temp.l] < temp.sum)
        {
            d[k + 1][temp.l] = temp.sum;
            g[k][r] = temp.r + 1;
            for (int j = 0; j < n; j++)
                a[k + 1][temp.l][j] = a[k][r][j];
        }

        if (r > 0)
            swap(a[k][r][r - 1], a[k][r][i]);

    }
}
ll up3(int k, int r)
{
        ll tsum = 0;
        ll res = a[k][r][r];
        g[k][r] = r + 1;
        for (int i = r; i < n; i++)
        {
            tsum += a[k][r][i];

            if (tsum > res)
            {
                g[k][r] = i + 1;
                res = tsum;
            }

            if (tsum < 0)
            {
                break;
            }
        }

        return res;
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> bg[i];
    }

    for (int i = 0; i <= m; i++)
       for (int j = 0; j < n; j++)
           d[i][j] = -100000LL * 100000LL;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            a[0][i][j] = bg[j];
        d[0][i] = up3(0, i);
    }

    for (int i1 = 0; i1 < m; i1++)
    {
        for (int i2 = 0; i2 < n; i2++)
        {
            up2(i1, i2);
        }
    }
    ll sum = - 1000000LL * 1000000LL;
    for (int i = 0; i < n; i++)
    {
        if (sum < d[m][i])
        {
            sum = d[m][i];
        }
    }
        bool used = true;
        ll maxel = bg[0];
        for (int i = 0; i < n; i++)
        {
            if (bg[i] >= 0)
                used = false;
            if (bg[i] > maxel)
                maxel = bg[i];
        }

    if (used)
        cout << maxel;
    else
        cout << sum;
    return 0;
}


#include<bits/stdc++.h>
#define LLI long long int
using namespace std;
const int MX = 300;
LLI n , k , a[MX] , ans;
vector<LLI> e , v;

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

void solve(){
	for(int i=1;i<=n;++i){
		for(int j=i+1;j<=n;++j){
			LLI sum = 0;
			v.clear() , e.clear();
			for(int s=1;s<=n;++s){
				if(s<i || s>j)
					v.push_back(a[s]);
				else{
					e.push_back(a[s]);
					sum += a[s];
				}
			}
			sort(e.begin() , e.end());
			sort(v.begin() , v.end());
			LLI an = 0;
			for(int s=v.size()-1;s>=max(0*1ll , v.size()-k) ;--s)
				an += max(0*1ll , v[s]);
//			if(i== 3&&j==8)
//				cout <<sum<<" "<<an<<endl;

			sum += an;
			int x= e.size();
			for(int s=0;s<min(x*1ll , k);++s)
				if(e[s] < 0){
					sum += e[s];
				}
			ans = max(ans , sum);
//			if(i== 3&&j==8)
//				cout <<sum<<" "<<an<<endl;

		}
	}
	cout <<ans<<endl;
}

main(){
	input();
	solve();
}


#include<bits/stdc++.h>
#define LLI long long int
using namespace std;
const int MX = 300;
LLI n , k , a[MX] , ans;
vector<LLI> e , v;

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

void solve(){
	for(int i=1;i<=n;++i){
		for(int j=i;j<=n;++j){
			LLI sum = 0;
			v.clear() , e.clear();
			for(int s=1;s<=n;++s){
				if(s<i || s>j)
					v.push_back(a[s]);
				else{
					e.push_back(a[s]);
					sum += a[s];
				}
			}
			sort(e.begin() , e.end());
			sort(v.begin() , v.end());
			LLI an = 0;
			for(int s=v.size()-1;s>=max(0*1ll , v.size()-k) ;--s)
				an += max(0*1ll , v[s]);
			sum += an;
			int x= e.size();
			for(int s=0;s<min(x*1ll , k);++s)
				if(e[s] < 0){
					sum += e[s];
				}
			ans = max(ans , sum);
		}
	}
	cout <<ans<<endl;
}

main(){
	input();
	solve();
}


#include<bits/stdc++.h>
#define LLI long long int
using namespace std;
const int MX = 300;
LLI n , k , a[MX] , ans;
vector<LLI> e , v;

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

void solve(){
	for(int i=1;i<=n;++i){
		for(int j=i;j<=n;++j){
			LLI sum = 0;
			v.clear() , e.clear();
			for(int s=1;s<=n;++s){
				if(s<i || s>j)
					v.push_back(a[s]);
				else{
					e.push_back(a[s]);
					sum += a[s];
				}
			}
			sort(e.begin() , e.end());
			sort(v.begin() , v.end());
			LLI an = 0 , w = 0;
			for(int s=v.size()-1;s>=max(0*1ll , v.size()-k) ;--s){
				if(v[s] < 0){
					w=v.size()-s;
					break;
				}
				else
					an += v[s];
			}
			sum += an;
			int x= e.size();
			for(int s=0;s<min(x*1ll , w);++s)
				if(e[s] < 0){
					sum += e[s];
				}
			ans = max(ans , sum);
		}
	}
	cout <<ans<<endl;
}

main(){
	input();
	solve();
}


#include<bits/stdc++.h>
#define LLI long long int
using namespace std;
const int MX = 300;
LLI n , k , a[MX] , ans;
vector<LLI> e , v;

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

void solve(){
	for(int i=1;i<=n;++i){
		for(int j=i;j<=n;++j){
			LLI sum = 0;
			v.clear() , e.clear();
			for(int s=1;s<=n;++s){
				if(s<i || s>j)
					v.push_back(a[s]);
				else{
					e.push_back(a[s]);
					sum += a[s];
				}
			}
//			if( i==3 && j ==8)
//			cout <<sum<<endl;
			sort(e.begin() , e.end());
			sort(v.begin() , v.end());
			reverse(v.begin() , v.end());
			LLI x = min(v.size() , e.size());
			x = min(k , x);
			LLI an1 = 0 , an2 = 0;
			LLI f = sum;
			for(int s=0;s<x;++s){
//				if(i == 3 && j == 8)
//					cout <<v[s]<<" "<<e[s]<<endl;
				an1 += v[s] , an2 += e[s];
				f = max(f , sum - an2 + an1);
			}
			sum = max(sum , f);
			ans = max(ans , sum);
		}
	}
	cout <<ans<<endl;
}

main(){
	input();
	solve();
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = -INF, cur;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) cin >> tab[i];
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                //cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                while(x < k && it1 != in.end() && it2 != out.rend())
                {
                    if(*it1 < *it2)
                        tmp += *it2 - *it1;
                    //tmp -= *it1;
                    //tmp += *it2;
                    x++; it1++; it2++;
                }
                res = max(res, tmp+cur);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, cur, res = 0;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) {cin >> tab[i]; res+=tab[i];}
    REP(i, 0, n)
    {
        REP(j, i, n)
        {
            v_int in, out;
            int cur = 0, tmp = 0;
            FOR(x, n)
            {
                if(x <= j && x > i) {in.PB(tab[x]); cur += tab[x];}
                else out.PB(tab[x]);
            }
            sort(in.begin(), in.end());
            sort(out.rbegin(), out.rend());
            int l = min((int)in.size(),min((int)out.size(), k));
            FOR(x, l) if(in[x] < out[x]) tmp += out[x]-in[x];
            res = max(res, cur+tmp);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, cur, res = -INF;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) {cin >> tab[i]; res+=tab[i];}
    REP(i, 0, n)
    {
        REP(j, i+1, n)
        {
            v_int in, out;
            int cur = 0, tmp = 0;
            FOR(x, n)
            {
                if(x <= j &&  i > x) {in.PB(tab[x]); cur += tab[x];}
                else out.PB(tab[x]);
            }
            sort(in.begin(), in.end());
            sort(out.rbegin(), out.rend());
            int l = min((int)in.size(),min((int)out.size(), k));
            FOR(x, l) if(in[x] < out[x]) tmp += out[x]-in[x];
            res = max(res, cur+tmp);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = INF, cur;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) cin >> tab[i];
    FOR(i, n) res = min(res, tab[i]);
    REP(i, 0, n-1)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        REP(j, i, n-1)
        {
                cur += tab[j];
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                while(x < k && x < j-i && it1 != in.end() && it2 != out.rend())
                {
                    tmp -= *it1;
                    tmp += *it2;
                    it1++;
                    it2++;
                    x++;
                }
                res = max(res, tmp+cur);
        }
    }

    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = INF, cur;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) cin >> tab[i];
    FOR(i, n) res = min(res, tab[i]);
    REP(i, 0, n-1)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        REP(j, i, n-1)
        {
                cur += tab[j];
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                while(x < k && x <= j-i && it1 != in.end() && it2 != out.rend())
                {
                    tmp -= *it1;
                    tmp += *it2;
                    it1++;
                    it2++;
                    x++;
                }
                res = max(res, tmp+cur);
        }
    }

    cout << res;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, cur, res = 0;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in;
    multiset<int, greater<int> > out;
    FOR(i, n) {cin >> tab[i]; res+=tab[i];}
    for(int i = 0; i < n; i++)
    {
        for(int j = i; j < n; j++)
        {
            cur = 0;
            in.clear();
            out.clear();
            for(int x = 0; x < n; x++) if(x >= i && x <= j) in.insert(tab[x]); else out.insert(tab[x]);
            int x = 0;
            while(!in.empty() && !out.empty() && x < k)
            {
                int a = *in.begin();
                int b = *out.begin();
                if(a < b)
                {
                    in.erase(in.begin());
                    out.erase(out.begin());
                    in.insert(b);
                    out.insert(a);
                }
                else break;
                x++;
            }
            int cnt = 0;
            FOREACH(it, in) cnt += *it;
            res = max(res, cnt);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = -INF;
    cin >> n >> k;
    v_int tab(n);
    FOR(i, n) {cin >> tab[i]; res+=tab[i];}
    for(int i = 0; i < n; i++)
    {
        for(int j = i; j < n; j++)
        {
            v_int in, out;
            int cur = 0, tmp = 0;
            FOR(x, n)
            {
                if(j <= x &&  x >= i) {in.PB(tab[x]); cur += tab[x];}
                else out.PB(tab[x]);
            }
            if(in.empty()) continue;
            sort(in.begin(), in.end());
            sort(out.rbegin(), out.rend());
            int l = min((int)in.size(),min((int)out.size(), k));
            FOR(x, l) if(in[x] < out[x]) tmp += out[x]-in[x];
            //cout << cur << " " << tmp << " " << i << " " << j << endl;
            res = max(res, cur+tmp);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = -INF;
    cin >> n >> k;
    v_int tab(n);
    FOR(i, n) {cin >> tab[i]; res+=tab[i];}
    REP(i, 0, n)
    {
        REP(j, i, n)
        {
            v_int in, out;
            int cur = 0, tmp = 0;
            FOR(x, n)
            {
                if(x <= j &&  i >= x) {in.PB(tab[x]); cur += tab[x];}
                else out.PB(tab[x]);
            }
            sort(in.begin(), in.end());
            sort(out.rbegin(), out.rend());
            int l = min((int)in.size(),min((int)out.size(), k));
            FOR(x, l) if(in[x] < out[x]) tmp += out[x]-in[x];
            //cout << cur+tmp << endl;
            res = max(res, cur+tmp);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, cur, res = 0;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) {cin >> tab[i]; res+=tab[i];}
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                //cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                int l = min((int)in.size(), min((int)out.size(), k));
                for(int x = 0;x < l && *it1 < *it2; x++, it1++, it2++)
                    tmp += *it2 - *it1;
                res = max(res, tmp+cur);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = -INF, cur;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) cin >> tab[i];
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                while(x < k && it1 != in.end() && it2 != out.rend())
                {
                    if(*it1 < *it2)
                        tmp += *it2 - *it1;
                    //tmp -= *it1;
                    //tmp += *it2;
                    x++; it1++; it2++;
                }
                res = max(res, tmp+cur);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, cur, res = 0;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in;
    multiset<int, greater<int> > out;
    FOR(i, n) {cin >> tab[i]; res+=tab[i];}
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                //cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = cur;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::iterator it2 = out.begin();
                int l = min((int)in.size(), min((int)out.size(), k));
                for(int x = 0;x < l && *it1 < *it2; x++, it1++, it2++)
                    tmp += *it2 - *it1;
                res = max(res, tmp);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, cur, res = -INF;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) {cin >> tab[i]; res+=tab[i];}
    REP(i, 0, n)
    {
        REP(j, i, n)
        {
            v_int in, out;
            int cur = 0, tmp = 0;
            FOR(x, n)
            {
                if(x <= j && x > i) {in.PB(tab[x]); cur += tab[x];}
                else out.PB(tab[x]);
            }
            sort(in.begin(), in.end());
            sort(out.rbegin(), out.rend());
            int l = min((int)in.size(),min((int)out.size(), k));
            FOR(x, l) if(in[x] < out[x]) tmp += out[x]-in[x];
            res = max(res, cur+tmp);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = -INF, cur;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) cin >> tab[i];
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                //cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                while(x < k && it1 != in.end() && it2 != out.rend())
                {
                    if(*it1 < *it2)
                    {
                        tmp += *it2 - *it1;
                        it1++; it2++;
                    }
                    else
                    {
                        it2++;
                    }

                    //tmp -= *it1;
                    //tmp += *it2;
                    x++;
                }
                res = max(res, tmp+cur);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, cur, res = 0;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) {cin >> tab[i]; res+=tab[i];}
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                //cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                int l = min((int)in.size(), min((int)out.size(), k));
                while(x < l)
                {
                    if(*it1 < *it2)
                    {
                        tmp += *it2 - *it1;
                    }
                    it1++; it2++;
                    x++;
                }
                res = max(res, tmp+cur);
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = -INF, cur;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) cin >> tab[i];
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                //cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                while(x < k && it1 != in.end() && it2 != out.rend())
                {
                    res = max(res, tmp+cur);
                    tmp -= *it1;
                    tmp += *it2;
                    x++; it1++; it2++;
                }
                //cout << tmp << " " << cur << endl;
                res = max(res, tmp+cur);
        }
    }

    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = -INF, cur;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) cin >> tab[i];
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                //cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                while(x < k && it1 != in.end() && it2 != out.rend() && *it1 <= *it2)
                {
                    tmp -= *it1;
                    tmp += *it2;
                    x++; it1++; it2++;
                }
                //cout << tmp << " " << cur << endl;
                res = max(res, tmp+cur);
        }
    }

    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = -INF, cur;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) cin >> tab[i];
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                //cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                while(x < k && it1 != in.end() && it2 != out.rend() && *it1 < *it2)
                {
                    tmp -= *it1;
                    tmp += *it2;
                    x++; it1++; it2++;
                }
                //cout << tmp << " " << cur << endl;
                res = max(res, tmp+cur);
        }
    }

    cout << res << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
using namespace std;

#define FIRST(a) (a).begin()
#define REMOVE_FIRST(a) (a).erase((a).begin())
#define REMOVE_LAST(a) (a).erase(--(a).end())
#define LAST(a) (--(a).end())
#define PRINT_ALL(a) for(typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ ) printf("%d ", *it);
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define p_int pair<int, int>
#define v_int vector<int>
#define PB push_back
#define LL long long int
#define MP make_pair
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(0)
inline void print(int n){cout<<n<<endl;}
#define mod 1000000009
#define INF 1000000000

int main()
{
    IOS;
    int n, k, res = -INF, cur;
    cin >> n >> k;
    v_int tab(n);
    multiset<int> in, out;
    FOR(i, n) cin >> tab[i];
    for(int i = 0; i < n; i++)
    {
        cur = 0;
        in.clear();
        out.clear();
        FOR(x, n) out.insert(tab[x]);
        for(int j = i; j < n; j++)
        {
                cur += tab[j];
                //cout << i << " " << j << endl;
                in.insert(tab[j]);
                out.erase(tab[j]);
                int x = 0, tmp = 0;
                multiset<int>::iterator it1 = in.begin();
                multiset<int>::reverse_iterator it2 = out.rbegin();
                while(x < k && it1 != in.end() && it2 != out.rend())
                {
                    if(*it1 < *it2)
                    {
                        tmp += *it2 - *it1;
                        it1++; it2++;
                        x++;
                    }
                    else
                    {
                        it2++;
                    }
                }
                res = max(res, tmp+cur);
        }
    }
    cout << res << endl;
}


//Caution to the wind,complete freedom
#include<bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define sd(n) scanf("%d",&n);
#define pd(n) printf("%d\n",n);
#define sl(n) scanf("%lld",&n);
#define pl(n) printf("%lld\n",n);
ll mpow(ll a, ll n,ll mod)
{ll ret=1;ll b=a;while(n) {if(n&1)
    ret=(ret*b)%mod;b=(b*b)%mod;n>>=1;}
return (ll)ret;
}
using namespace std;
#define pi acos(-1.0)
#define MAX 1000001
#define  pii pair<int,int>
#define vi vector<int>
int a[301];
          vector<int> v,v1;
int main()
{
    int n,m,i,j,k;
    cin>>n>>m;
    for(j=0;j<n;j++)
        cin>>a[j];
        int sum1=0,max1=-20000;
    for(i=0;i<n;i++)
        for(j=i;j<n;j++)
        {
            sum1=0;
   v.clear();
   v1.clear();
      int p;
      for(p=0;p<n;p++)
       {
         if(p>=i&&p<=j)
        {v.push_back(a[p]);sum1+=a[p];}
        else
            v1.push_back(a[p]);
       }
        sort(v.begin(),v.end());
        sort(v1.begin(),v1.end());
        int l=0;
        if(v1.empty())
        {
            max1=max(max1,sum1);
            continue;
        }
        p=v1.size()-1;
        for(int k=0;k<v.size();k++)
        {
            if(v[k]<v1[p])
            {
                sum1-=v[k];
                sum1+=v1[p];
                l++;
                p--;
            }
        if(p==-1||l==m)
            break;
        }

        if(sum1>max1)
        max1=sum1;
        }
    cout<<max1;

    return 0;
}


//Caution to the wind,complete freedom
#include<bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define sd(n) scanf("%d",&n);
#define pd(n) printf("%d\n",n);
#define sl(n) scanf("%lld",&n);
#define pl(n) printf("%lld\n",n);
ll mpow(ll a, ll n,ll mod)
{ll ret=1;ll b=a;while(n) {if(n&1)
    ret=(ret*b)%mod;b=(b*b)%mod;n>>=1;}
return (ll)ret;
}
using namespace std;
#define pi acos(-1.0)
#define MAX 1000001
#define  pii pair<int,int>
#define vi vector<int>
int a[301];
          vi v,v1;
int main()
{
    int n,m,i,j,k;
    cin>>n>>m;
    for(j=0;j<n;j++)
        cin>>a[j];
        int sum1=0,max1=-2000000;
    for(i=0;i<n;i++)
        for(j=i;j<n;j++)
        {
            sum1=0;
   v.clear();
   v1.clear();
      int p;
      for(p=0;p<n;p++)
       {
         if(p>=i&&p<=j)
        {v.push_back(a[p]);sum1+=a[p];}
        else
            v1.push_back(a[p]);
       }
        sort(v.begin(),v.end());
        sort(v1.begin(),v1.end());
        reverse(v1.begin(),v1.end());
        int l=0;
        if(v1.empty())
        {
            max1=max(max1,sum1);
            continue;
        }
        p=v1.size()-1;
        for(int k=0;k<n;k++)
        {
            if(v[k]<v1[p])
            {
                sum1-=v[k];
                sum1+=v1[p];
                l++;
                p--;
            }
        if(p==-1||l==m)
            break;
        }
        if(sum1>max1)
        max1=sum1;
        }
    cout<<max1;

    return 0;
}


//Caution to the wind,complete freedom
#include<bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define sd(n) scanf("%d",&n);
#define pd(n) printf("%d\n",n);
#define sl(n) scanf("%lld",&n);
#define pl(n) printf("%lld\n",n);
ll mpow(ll a, ll n,ll mod)
{ll ret=1;ll b=a;while(n) {if(n&1)
    ret=(ret*b)%mod;b=(b*b)%mod;n>>=1;}
return (ll)ret;
}
using namespace std;
#define pi acos(-1.0)
#define MAX 1000001
#define  pii pair<int,int>
#define vi vector<int>
int a[301];
          vi v,v1;
int main()
{
    int n,m,i,j,k;
    cin>>n>>m;
    for(j=0;j<n;j++)
        cin>>a[j];
        int sum1=0,max1=INT_MIN;
    for(i=0;i<n;i++)
        for(j=i;j<n;j++)
        {
            sum1=0;
   v.clear();
   v1.clear();
      int p;
      for(p=0;p<n;p++)
       {
         if(p>=i&&p<=j)
        {v.push_back(a[p]);sum1+=a[p];}
        else
            v1.push_back(a[p]);
       }
        sort(v.begin(),v.end());
        sort(v1.begin(),v1.end());
        reverse(v1.begin(),v1.end());
        int l=0;
        if(v1.empty())
        {
            max1=max(max1,sum1);
            continue;
        }
        p=v1.size()-1;
        for(int k=0;k<n;k++)
        {
            if(v[k]<v1[p])
            {
                sum1-=v[k];
                sum1+=v1[p];
                l++;
                p--;
            }
        if(p==-1||l==m)
            break;
        }
        if(sum1>max1)
        max1=sum1;
        }
    cout<<max1;

    return 0;
}


//Caution to the wind,complete freedom
#include<bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define sd(n) scanf("%d",&n);
#define pd(n) printf("%d\n",n);
#define sl(n) scanf("%lld",&n);
#define pl(n) printf("%lld\n",n);
ll mpow(ll a, ll n,ll mod)
{ll ret=1;ll b=a;while(n) {if(n&1)
    ret=(ret*b)%mod;b=(b*b)%mod;n>>=1;}
return (ll)ret;
}
using namespace std;
#define pi acos(-1.0)
#define MAX 1000001
#define  pii pair<int,int>
#define vi vector<int>
int a[301];
          vi v,v1;
int main()
{
    int n,m,i,j,k;
    cin>>n>>m;
    for(j=0;j<n;j++)
        cin>>a[j];
        int sum1=0,max1=-200000;
    for(i=0;i<n;i++)
        for(j=i;j<n;j++)
        {
            sum1=0;
   v.clear();
   v1.clear();
      int p;
      for(p=0;p<n;p++)
       {
         if(p>=i&&p<=j)
        {v.push_back(a[p]);sum1+=a[p];}
        else
            v1.push_back(a[p]);
       }
        sort(v.begin(),v.end());
        sort(v1.begin(),v1.end());
        reverse(v1.begin(),v1.end());
        int l=0;
        if(v1.empty())
        {
            max1=max(max1,sum1);
            continue;
        }
        p=v1.size()-1;
        for(int k=0;k<n;k++)
        {
            if(v[k]<v1[p])
            {
                sum1-=v[k];
                sum1+=v1[p];
                l++;
                p--;
            }
        if(p==-1||l==m)
            break;
        }
        if(sum1>max1)
        max1=sum1;
        }
    cout<<max1;

    return 0;
}


#include <stdio.h>
#include <algorithm>
#define MAX 205

using namespace std;

int inp[MAX],ind[MAX],ns[MAX];

bool cmp(int a,int b)
{
    return inp[a]<inp[b];
}

int main()
{
    int n,K,i,ans,cnt,sum,j,k,l,swp,tmp;
    scanf("%d%d",&n,&K);
    for (i=1;i<=n;i++) scanf("%d",&inp[i]);
    for (i=1;i<=n;i++) ind[i]=i;
    sort(ind+1,ind+n+1,cmp);
    ans=-1000;
    for (i=1;i<=n;i++)
    {
        cnt=0;
        sum=0;
        for (j=i;j<=n;j++)
        {
            sum+=inp[j];
            for (k=0;k<cnt;k++)
            {
                if (inp[j]<ns[k])
                {
                    for (l=cnt-2;l>=k;l--) ns[l+1]=ns[l];
                    ns[k]=inp[j];
                    break;
                }
            }
            if (k==cnt&&cnt<K) ns[cnt++]=inp[j];
            swp=0;
            ans=max(ans,sum);
            tmp=sum;
            for (k=n;k>=1&&swp<K&&swp<cnt;k--)
            {
                if (ind[k]>=i&&ind[k]<=j) continue;
                swp++;
                tmp-=ns[swp-1];
                tmp+=inp[ind[k]];
                ans=max(ans,tmp);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <stdio.h>
#include <algorithm>
#define MAX 205

using namespace std;

int inp[MAX],ind[MAX],ns[MAX];

bool cmp(int a,int b)
{
    return inp[a]<inp[b];
}

int main()
{
    int n,K,i,ans,cnt,sum,j,k,l,swp,tmp;
    scanf("%d%d",&n,&K);
    for (i=1;i<=n;i++) scanf("%d",&inp[i]);
    for (i=1;i<=n;i++) ind[i]=i;
    sort(ind+1,ind+n+1,cmp);
    ans=-1000;
    for (i=1;i<=n;i++)
    {
        cnt=0;
        sum=0;
        for (j=i;j<=n;j++)
        {
            sum+=inp[j];
            for (k=0;k<cnt;k++)
            {
                if (inp[j]<ns[k])
                {
                    for (l=cnt-2;l>=k;l--) ns[l+1]=ns[l];
                    ns[k]=inp[j];
                    break;
                }
            }
            if (k==cnt&&cnt<K) ns[cnt++]=inp[j];
            swp=0;
            ans=max(ans,sum);
            tmp=sum;
            for (k=n;k>=1&&swp<K;k--)
            {
                if (ind[k]>=i&&ind[k]<=j) continue;
                swp++;
                tmp-=ns[swp-1];
                tmp+=inp[ind[k]];
                ans=max(ans,tmp);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <stdio.h>
#include <algorithm>
#define MAX 205
#define INF 1000000000

using namespace std;

int inp[MAX],ind[MAX],ns[MAX];

bool cmp(int a,int b)
{
    return inp[a]<inp[b];
}

int main()
{
    int n,K,i,ans,cnt,sum,j,k,l,swp,tmp;
    scanf("%d%d",&n,&K);
    for (i=1;i<=n;i++) scanf("%d",&inp[i]);
    for (i=1;i<=n;i++) ind[i]=i;
    sort(ind+1,ind+n+1,cmp);
    ans=-INF;
    for (i=1;i<=n;i++)
    {
        cnt=0;
        sum=0;
        for (j=i;j<=n;j++)
        {
            sum+=inp[j];
            for (k=0;k<cnt;k++)
            {
                if (inp[j]<ns[k])
                {
                    for (l=cnt-2;l>=k;l--) ns[l+1]=ns[l];
                    ns[k]=inp[j];
                    break;
                }
            }
            if (k==cnt&&cnt<K) ns[cnt++]=inp[j];
            swp=0;
            ans=max(ans,sum);
            tmp=sum;
            for (k=n;k>=1&&swp<K&&swp<cnt;k--)
            {
                if (ind[k]>=i&&ind[k]<=j) continue;
                swp++;
                tmp-=ns[swp-1];
                tmp+=inp[ind[k]];
                ans=max(ans,tmp);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <stdio.h>
#include <algorithm>
#define MAX 205
#define INF 1000000000

using namespace std;

int inp[MAX],ind[MAX],ns[MAX];

bool cmp(int a,int b)
{
    return inp[a]<inp[b];
}

int main()
{
    int n,K,i,ans,cnt,sum,j,k,l,swp,tmp;
    scanf("%d%d",&n,&K);
    for (i=1;i<=n;i++) scanf("%d",&inp[i]);
    for (i=1;i<=n;i++) ind[i]=i;
    sort(ind+1,ind+n+1,cmp);
    ans=-INF;
    for (i=1;i<=n;i++)
    {
        cnt=0;
        sum=0;
        for (j=i;j<=n;j++)
        {
            sum+=inp[j];
            for (k=0;k<cnt;k++)
            {
                if (inp[j]<ns[k])
                {
                    for (l=K-2;l>=k;l--) ns[l+1]=ns[l];
                    ns[k]=inp[j];
                    cnt=min(cnt+1,K);
                    break;
                }
            }
            if (k==cnt&&cnt<K) ns[cnt++]=inp[j];
            swp=0;
            ans=max(ans,sum);
            tmp=sum;
            for (k=n;k>=1&&swp<K&&swp<cnt;k--)
            {
                if (ind[k]>=i&&ind[k]<=j) continue;
                swp++;
                tmp-=ns[swp-1];
                tmp+=inp[ind[k]];
                ans=max(ans,tmp);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <stdio.h>
#include <algorithm>
#define MAX 205
#define INF 1000000000

using namespace std;

int inp[MAX],ind[MAX],ns[MAX];

bool cmp(int a,int b)
{
    return inp[a]<inp[b];
}

int main()
{
    int n,K,i,ans,cnt,sum,j,k,l,swp,tmp;
    scanf("%d%d",&n,&K);
    for (i=1;i<=n;i++) scanf("%d",&inp[i]);
    for (i=1;i<=n;i++) ind[i]=i;
    sort(ind+1,ind+n+1,cmp);
    ans=-INF;
    for (i=1;i<=n;i++)
    {
        cnt=0;
        sum=0;
        for (j=i;j<=n;j++)
        {
            sum+=inp[j];
            for (k=0;k<cnt;k++)
            {
                if (inp[j]<ns[k])
                {
                    for (l=cnt-2;l>=k;l--) ns[l+1]=ns[l];
                    ns[k]=inp[j];
                    cnt=min(cnt+1,K);
                    break;
                }
            }
            if (k==cnt&&cnt<K) ns[cnt++]=inp[j];
            swp=0;
            ans=max(ans,sum);
            tmp=sum;
            for (k=n;k>=1&&swp<K&&swp<cnt;k--)
            {
                if (ind[k]>=i&&ind[k]<=j) continue;
                swp++;
                tmp-=ns[swp-1];
                tmp+=inp[ind[k]];
                ans=max(ans,tmp);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char const *argv[])
{
    //while(1)
    {
        int n, k, maxGroupVal = -1, index, sum = 0, beginIndex = 0;
        cin >> n >> k;
        vector<int> a;

        for (int i = 0; i < n; ++i){
            int tmp;
            cin >> tmp;
            a.push_back(tmp);

            if(a[i] > 0) sum += a[i];
            else {
                if(sum > maxGroupVal){
                    maxGroupVal = sum;
                    index = beginIndex;
                }
                beginIndex = i + 1;
                sum = 0;
            }
        }
        if(sum > 0){
            a[index] = maxGroupVal;
            while(a[index + 1] > 0 && (index != n-1)){
                a.erase(a.begin() + index + 1);
            }
            /*for(int i=0;i<a.size();i++)cout << a[i] << " ";
            cout << endl;*/

            int res = maxGroupVal;
            a.erase(a.begin() + index);
            while(k--){
                int maxIndex = 0;
                for(int i=1;i<a.size();i++){
                    if(a[i] > a[maxIndex])maxIndex = i;
                }
                res += a[maxIndex];
                a.erase(a.begin() + maxIndex);
            }
            cout << res << endl;
        }
        else {
            int mymax = a[0];
            for(int i = 1; i < a.size(); i++){
                mymax = max(mymax, a[i]);
            }
            cout << mymax << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

int getMaxSquence(vector<int> &v){
    int sum = 0, maxGroupVal = -1, beginIndex = 0, index;
    for (int i = 0; i < v.size(); ++i){
        if(v[i] > 0) sum += v[i];
        else {
            if(sum > maxGroupVal){
                maxGroupVal = sum;
                index = beginIndex;
            }
            beginIndex = i + 1;
            sum = 0;
        }
    }
    v[index] = maxGroupVal;
    while(index + 1 < v.size() && v[index + 1] > 0)
        v.erase(v.begin() + index + 1);
    return index;
}
void PrintVector(std::vector<int> v){
    for (int i = 0; i < v.size(); ++i){
        cout << v[i] << " ";
    }
    cout << endl;
}
int getOtherMax(vector<int> v, int index){
    int myMaxIndex = 0;
    for (int i = 1; i < v.size(); ++i){
        if(i != index && v[i] > v[myMaxIndex]){
            myMaxIndex = i;
        }
    }
    return myMaxIndex;
}
bool onePos(std::vector<int> v){
    int count = 0;
    for (int i = 0; i < v.size(); ++i){
        count = (v[i] > 0 ? count + 1 : count);
        if(count > 1)return false;
    }
    return true;
}
int main(int argc, char const *argv[])
{
    //while(1)
    {
        int n, k, t;
        cin >> n >> k;
        k++;
        vector<int> v;
        bool allNeg = true;
        for (int i = 0; i < n; ++i){
            cin >> t;
            allNeg = (t > 0 ? false : true);
            v.push_back(t);
        }
        if(!allNeg){
            while(k--){
                int maxGroupIndex = getMaxSquence(v);
                int myMaxIndex = getOtherMax(v, maxGroupIndex);
                //PrintVector(v);
                if(maxGroupIndex + 2 < v.size() && maxGroupIndex - 2 > -1){
                    if(v[maxGroupIndex + 2] > v[maxGroupIndex - 2])
                        swap(v[myMaxIndex], v[maxGroupIndex + 1]);
                    else swap(v[myMaxIndex], v[maxGroupIndex - 1]);
                }
                else{
                    if(maxGroupIndex + 2 >= v.size() && maxGroupIndex - 2 > -1)
                        swap(v[myMaxIndex], v[maxGroupIndex - 1]);
                    else if(maxGroupIndex + 2 < v.size())
                        swap(v[myMaxIndex], v[maxGroupIndex + 1]);
                }
            }
            cout << (*max_element(v.begin(), v.end())) << endl;
        }
        else cout << (*max_element(v.begin(), v.end())) << endl;
    }
    return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

int getMaxSquence(vector<int> &v){
	int sum = 0, maxGroupVal = -1, beginIndex = 0, index;
	for (int i = 0; i < v.size(); ++i){
		if(v[i] > 0) sum += v[i];
		else {
			if(sum > maxGroupVal){
				maxGroupVal = sum;
				index = beginIndex;
			}
			beginIndex = i + 1;
			sum = 0;
		}
	}
	v[index] = maxGroupVal;
	while(index + 1 < v.size() && v[index + 1] > 0)
		v.erase(v.begin() + index + 1);
	return index;
}
void PrintVector(std::vector<int> v){
	for (int i = 0; i < v.size(); ++i){
		cout << v[i] << " ";
	}
	cout << endl;
}
int getOtherMax(vector<int> v, int index){
	int myMaxIndex = 0;
	for (int i = 1; i < v.size(); ++i){
		if(i != index && v[i] > v[myMaxIndex]){
			myMaxIndex = i;
		}
	}
	return myMaxIndex;
}
int main(int argc, char const *argv[])
{
	//while(1)
{
		int n, k, t;
		cin >> n >> k;
		vector<int> v;
		bool allNeg = true;
		for (int i = 0; i < n; ++i){
			cin >> t;
			allNeg = (t > 0 ? false : true);
			v.push_back(t);
		}
		if(!allNeg)
			while(1){
				int maxGroupIndex = getMaxSquence(v);
				int myMaxIndex = getOtherMax(v, maxGroupIndex);
				if(maxGroupIndex + 2 < v.size() && maxGroupIndex - 2 > -1){
					if(v[maxGroupIndex + 2] > v[maxGroupIndex - 2])
						swap(v[myMaxIndex], v[maxGroupIndex + 1]);
					else swap(v[myMaxIndex], v[maxGroupIndex - 1]);
				}
				else{
					if(maxGroupIndex + 2 >= v.size() && maxGroupIndex - 2 > -1)
						swap(v[myMaxIndex], v[maxGroupIndex - 1]);
					else if(maxGroupIndex + 2 < v.size())
						swap(v[myMaxIndex], v[maxGroupIndex + 1]);
					else{
						cout << (*max_element(v.begin(), v.end())) << endl;
						break;
					}
				}
			}
		else cout << (*max_element(v.begin(), v.end())) << endl;
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

int getMaxSquence(vector<int> &v){
    int sum = 0, maxGroupVal = -1, beginIndex = 0, index;
    for (int i = 0; i < v.size(); ++i){
        if(v[i] > 0) sum += v[i];
        else {
            if(sum > maxGroupVal){
                maxGroupVal = sum;
                index = beginIndex;
            }
            beginIndex = i + 1;
            sum = 0;
        }
    }
    v[index] = maxGroupVal;
    while(index + 1 < v.size() && v[index + 1] > 0)
        v.erase(v.begin() + index + 1);
    return index;
}
void PrintVector(std::vector<int> v){
    for (int i = 0; i < v.size(); ++i){
        cout << v[i] << " ";
    }
    cout << endl;
}
int getOtherMax(vector<int> v, int index){
    int myMaxIndex = 0;
    for (int i = 1; i < v.size(); ++i){
        if(i != index && v[i] > v[myMaxIndex]){
            myMaxIndex = i;
        }
    }
    return myMaxIndex;
}
bool onePos(std::vector<int> v){
    int count = 0;
    for (int i = 0; i < v.size(); ++i){
        count = (v[i] > 0 ? count + 1 : count);
        if(count > 1)return false;
    }
    return true;
}
int main(int argc, char const *argv[])
{
    //while(1)
    {
        int n, k, t;
        cin >> n >> k;
        vector<int> v;
        bool allNeg = true;
        for (int i = 0; i < n; ++i){
            cin >> t;
            allNeg = (t > 0 ? false : true);
            v.push_back(t);
        }
        if(!allNeg)
            while(1){
                if(onePos(v)){
                    cout << (*max_element(v.begin(), v.end())) << endl;
                    break;
                }
                int maxGroupIndex = getMaxSquence(v);
                int myMaxIndex = getOtherMax(v, maxGroupIndex);
                //PrintVector(v);
                if(maxGroupIndex + 2 < v.size() && maxGroupIndex - 2 > -1){
                    if(v[maxGroupIndex + 2] > v[maxGroupIndex - 2])
                        swap(v[myMaxIndex], v[maxGroupIndex + 1]);
                    else swap(v[myMaxIndex], v[maxGroupIndex - 1]);
                }
                else{
                    if(maxGroupIndex + 2 >= v.size() && maxGroupIndex - 2 > -1)
                        swap(v[myMaxIndex], v[maxGroupIndex - 1]);
                    else if(maxGroupIndex + 2 < v.size())
                        swap(v[myMaxIndex], v[maxGroupIndex + 1]);
                    else{
                        cout << (*max_element(v.begin(), v.end())) << endl;
                        break;
                    }
                }
            }
        else cout << (*max_element(v.begin(), v.end())) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
typedef std::vector<int> vi;

bool comp(int a, int b){return a > b;}
void run(){
    int n, k;
    vi m;
    cin >> n >> k;
    while(n--){
        int t; cin >> t;
        m.push_back(t);
    }
    int maxSum = -1010;
    rep(i, 0, m.size()){
        rep(j, i, m.size()){
            vi u, v;
            rep(t, 0, m.size()){
                if(t >= i && t <= j)u.push_back(m[t]);
                else v.push_back(m[t]);
            }
            sort(u.begin(), u.end());
            if(v.size() > 0){
                sort(v.begin(), v.end(), comp);
                int k2 = k;
                rep(t, 0, v.size()){
                    if(v[t] > u[t]){
                        swap(v[t], u[t]);
                        k2--;
                        if(!k2)break;
                    }
                }
            }
            int sum = 0;
            rep(t, 0, u.size())sum+=u[t];
            maxSum = max(maxSum, sum);
            u.clear();v.clear();
        }
    }
    cout << maxSum << endl;
}
int main(int argc, char const *argv[])
{
    //while(1)
        run();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
typedef std::vector<int> vi;

bool comp(int a, int b){return a > b;}
void run(){
    int n, k;
    vi m;
    cin >> n >> k;
    while(n--){
        int t; cin >> t;
        m.push_back(t);
    }
    int maxSum = -1010;
    rep(i, 0, m.size()){
        rep(j, i, m.size()){
            vi u, v;
            rep(t, 0, m.size()){
                if(t >= i && t <= j)u.push_back(m[t]);
                else v.push_back(m[t]);
            }
            sort(u.begin(), u.end());
            if(v.size() > 0){
                sort(v.begin(), v.end(), comp);
                int k2 = k;
                rep(t, 0, v.size()){
                    if(v[t] > u[t]){
                        swap(v[t], u[t]);
                        k2--;
                        if(!k2)break;
                    }
                }
            }
            int sum = 0;
            rep(t, 0, u.size())sum+=u[t];
            maxSum = max(maxSum, sum);
        }
    }
    cout << maxSum << endl;
}
int main(int argc, char const *argv[])
{
    //while(1)
        run();
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdio>
#include <sstream>
using namespace std;

string VectoString(std::vector<int> v){
    string s = "";
    for (int i = 0; i < v.size(); ++i){
        stringstream a;
        a << v[i] << " ";
        s.append(a.str());
        a.str("");
        a.clear();
    }
    return s;
}
int getMaxGroupVal(vector<int> v){
    int maxSum = -1000;
    for(int i=0;i<v.size();i++){
        for(int j=i;j<v.size();j++){
            int sum = 0;
            for(int u=i;u<=j;u++)sum += v[u];
            maxSum = max(maxSum, sum);
        }
    }
    return maxSum;
}

int m_max(vector<int> v, int k){
    //cout << VectoString(v) << endl;
    if(!k)return getMaxGroupVal(v);
    int myMax = -1001;
    for(int i=0;i<v.size();i++)
        for(int j=i+1;j<v.size();j++){
            if(v[i] != v[j]){
                swap(v[i], v[j]);
                myMax = max(myMax, m_max(v, k - 1));
                swap(v[i], v[j]);
            }
        }
    return (myMax != -1001 ? myMax : *max_element(v.begin(), v.end()));
}
int main(int argc, char const *argv[])
{
    //while(1)
    {
        int n, k;
        std::vector<int> v;
        cin >> n >> k;
        for (int i = 0; i < n; ++i){
            int tmp;
            cin >> tmp;
            v.push_back(tmp);
        }
        cout << m_max(v, k) << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char const *argv[])
{
    //while(1)
    {
        int n, k, maxGroupVal = -1, index, sum = 0, beginIndex = 0;
        cin >> n >> k;
        vector<int> a;

        for (int i = 0; i < n; ++i){
            int tmp;
            cin >> tmp;
            a.push_back(tmp);

 A           if(a[i] > 0) sum += a[i];
            else {
                if(sum > maxGroupVal){
                    maxGroupVal = sum;
                    index = beginIndex;
                }
                beginIndex = i + 1;
                sum = 0;
            }
        }
        if(sum > 0){
            a[index] = maxGroupVal;
            while(a[index + 1] > 0 && (index != n-1)){
                a.erase(a.begin() + index + 1);
            }
            /*for(int i=0;i<a.size();i++)cout << a[i] << " ";
            cout << endl;*/

            int res = maxGroupVal;
            a.erase(a.begin() + index);
            while(k--){
                int maxIndex = 0;
                for(int i=1;i<a.size();i++){
                    if(a[i] > a[maxIndex])maxIndex = i;
                }
                res += a[maxIndex];
                a.erase(a.begin() + maxIndex);
            }
            cout << res << endl;
        }
        else {
            int mymax = a[0];
            for(int i = 1; i < a.size(); i++){
                mymax = max(mymax, a[i]);
            }
            cout << mymax << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
typedef std::vector<int> vi;

bool comp(int a, int b){return a > b;}
void run(){
    int n, k;
    vi m;
    cin >> n >> k;
    while(n--){
        int t; cin >> t;
        m.push_back(t);
    }
    int maxSum = -1010;
    rep(i, 0, m.size()){
        rep(j, i, m.size()){
            vi u, v;
            rep(t, 0, m.size()){
                if(t >= i && t <= j)u.push_back(m[t]);
                else v.push_back(m[t]);
            }
            sort(u.begin(), u.end());
            if(v.size() > 0){
                sort(v.begin(), v.end(), comp);
                int k2 = k;
                rep(t, 0, min(v.size(), u.size())){
                    if(v[t] > u[t]){
                        swap(v[t], u[t]);
                        k2--;
                        if(!k2)break;
                    }
                }
            }
            int sum = 0;
            rep(t, 0, u.size())sum+=u[t];
            maxSum = max(maxSum, sum);
            u.clear();v.clear();
        }
    }
    cout << maxSum << endl;
}
int main(int argc, char const *argv[])
{
    //while(1)
        run();
    return 0;
}


#include <bits/stdc++.h>

#define fi(a,b,c) for(int a=b; a<=c; a++)
#define fd(a,b,c) for(int a=b; a>=c; a--)
#define fii(a,b,c) for(int a=b; a<c; a++)
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define reset(a,b) memset(a,b, sizeof a)

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef unsigned int ui;

const int N = 1009;
const int oo = (int)(1e9);

multiset <int> setin, setout;

int n, k , a[N];

int main() {
    scanf("%d %d", &n, &k);
    fi(i, 1, n ) scanf("%d", &a[i]), setout.insert(a[i]);
    int ans = -oo;
    fi(i, 1, n) fi(j, i, n) {
        int sum = 0;
        fi(u, i , j) {
            sum+= a[u];
            multiset <int> :: iterator it;
            it = setout.lower_bound(a[u]);
            setout.erase(it);
            setin.insert(a[u]);
        }
        multiset <int> ::iterator it1;
        multiset <int> ::iterator it2;
        int cnt;
        for (it1 = setout.end() , it2 = setin.begin(), cnt = 1; cnt <= k && it1 != setout.begin() && it2 != setin.end(); cnt ++ , it2++) {
            it1 --;
            sum -= *(it2);
            sum += *(it1);
        }
        for (; cnt<= k && it2 != setin.end(); cnt ++, it2 ++) {
            if (*(it2) < 0)
                sum -= *(it2);
        }
        ans = max(ans, sum);
        fi(u, i, j) {
            multiset <int> ::iterator it;
            it = setin.lower_bound(a[u]);
            setin.erase(it);
            setout.insert(a[u]);
        }
    }

    cout << ans;
}