Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define queint queue<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pq priority_queue<int>                     // 大きい順
#define pqg priority_queue<int, vec, greater<int>> // 小さい順
#define pb push_back
#define vec vector<int>
#define vecvec vector<vector<int>>
#define vecll vector<ll>
#define vecvecll vector<vector<ll>>
#define bs binary_search
#define All(c) (c).begin(), (c).end()
#define mp make_pair
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
int main() {
  int n = in();
  int k = in();
  deque<int> q;
  rep(i, n) { q.push_back(in()); }
  ll ans = 0;
  for (int x = 0; x <= max((ll)k, (ll)q.size()); x++) {
    for (int y = 0; x + y <= max((ll)k, (ll)q.size()); y++) {
      deque<int> p = q;
      vec a(x + y);
      rep(i, x) {
        a[i] = p.front();
        p.pop_front();
      }
      rep(i, y) {
        a[i + x] = p.back();
        p.pop_back();
      }
      sort(All(a));
      ll tmp = 0;
      ll sum = 0;
      rep(i, x + y) sum += a[i];
      tmp = sum;
      for (int i = 0; i < k - x - y & i < a.size(); i++) {
        sum -= a[i];
        tmp = max(tmp, sum);
      }
      ans = max(ans, tmp);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define queint queue<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pq priority_queue<int>                     // 大きい順
#define pqg priority_queue<int, vec, greater<int>> // 小さい順
#define pb push_back
#define vec vector<int>
#define vecvec vector<vector<int>>
#define vecll vector<ll>
#define vecvecll vector<vector<ll>>
#define bs binary_search
#define All(c) (c).begin(), (c).end()
#define mp make_pair
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
int main() {
  int n = in();
  int k = in();
  deque<int> q;
  rep(i, n) { q.push_back(in()); }
  ll ans = 0;
  for (int x = 0; x <= k; x++) {
    for (int y = 0; x + y <= min((ll)k, (ll)q.size()); y++) {
      deque<int> p = q;
      vec a(x + y);
      rep(i, x) {
        a[i] = p.front();
        p.pop_front();
      }
      rep(i, y) {
        a[i + x] = p.back();
        p.pop_back();
      }
      sort(All(a));
      ll tmp = 0;
      ll sum = 0;
      rep(i, x + y) sum += a[i];
      tmp = sum;
      for (int i = 0; i < k - x - y & i < a.size(); i++) {
        sum -= a[i];
        tmp = max(tmp, sum);
      }
      ans = max(ans, tmp);
    }
  }
  cout << ans << endl;
}