Language: C++

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
const double PI=3.14159265358979323846;
const in MOD = 1000000007;
const in INF=1e18+7;
const int inf=1e9+7;
using P=pair<int,int>;
vector<int> dx={0,1,-1,0};
vector<int> dy={1,0,0,-1};



int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout << fixed << setprecision(10);
    int n,k;
    cin>>n>>k;
    vector<int> v(n);
        rep(i,n) cin>>v.at(i);
    int ans=0;

    vector<int> xx(n);
    xx[0]=v[0];
    rep(i,n-1) xx[i+1]=xx[i]+v[i+1];
    vector<int> yy(n);
    yy[0]=v[n-1];
    rep(i,n-1) yy[i+1]=yy[i]+v[n-i-2];

    vector<vector<int>> minx(n,vector<int>(0));
    if(v[0]<0) minx[0].emplace_back(v[0]);
    rep(i,n-1){
        minx[i+1]=minx[i];
        if(v[i+1]<0)minx[i+1].emplace_back(v[i+1]);
    }
    rep(i,n) sort(all(minx[i]));
    vector<vector<int>> miny(n,vector<int>(0));
    if(v[n-1]<0) miny[0].emplace_back(v[0]);
    rep(i,n-1){
        miny[i+1]=miny[i];
        if(v[n-i-2]<0) miny[i+1].emplace_back(v[n-i-2]);
    }
    rep(i,n) sort(all(miny[i]));

    for(int x=0;x<=n;x++){
        for(int y=0;y<=n-x;y++){
            if(x+y>k) break;
            int sum=0;
                if(x>0) sum=xx[x-1];
                if(y>0) sum +=yy[y-1];
            int z=k-x-y;
            vector<int> sx(0),sy(0);
            // int i=0,j=0;
            //     if(x==0)i=-1;
            //     else sx=minx[x-1];
            //     if(y==0)j=-1;
            //     else sy=miny[y-1];
            //     if(sx.empty()) i=-1;
            //     if(sy.empty()) j=-1;
            rep(mkoij,z){
                if(i<0 && j<0) break;
                if(i<0){
                    sum -=sy[j];
                    j ++;
                    if(j>=sy.size()) y=-1;
                }
                else if(j<0){
                    sum -=sx[i];
                    i ++;
                    if(i>=sx.size()) i=-1;
                }
                else if(sx[i]<sy[j]){
                    sum -=sx[i];
                    i ++;
                    if(i>=sx.size()) i =-1;
                }
                else{
                    sum -=sy[j];
                    j ++;
                    if(j>=sy.size()) j=-1;
                }
            }
            ans=max(ans,sum);
        }
    }
    cout<<ans<<endl;
}
//./problem.exe

Compiler output:
a.cc: In function 'int main()':
a.cc:69:20: error: 'i' was not declared in this scope
   69 |                 if(i<0 && j<0) break;
      |                    ^
a.cc:69:27: error: 'j' was not declared in this scope
   69 |                 if(i<0 && j<0) break;
      |                           ^
a.cc:70:20: error: 'i' was not declared in this scope
   70 |                 if(i<0){
      |                    ^
a.cc:71:30: error: 'j' was not declared in this scope
   71 |                     sum -=sy[j];
      |                              ^
a.cc:75:25: error: 'j' was not declared in this scope
   75 |                 else if(j<0){
      |                         ^