Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n, k, m = 0;
  cin >> n >> k;
  deque<int> d;
  rep(i, n) {
    int e;
    cin >> e;
    d.push_back(e);
  }
  rep(i, k + 1) {
    rep(j, k - i + 1) {
      int t = 0, a = 0, b = 0;
      deque<int> c = d;
      priority_queue<int, vector<int>, greater<int>> pq;
      while (a < i && !c.empty()) {
        pq.push(c.front());
        c.pop_front();
        ++a;
      }
      while (b < j && !c.empty()) {
        pq.push(c.back());
        c.pop_back();
        ++b;
      }
      rep(l, k - a - b) {
        if (!pq.empty())
          break;
        if (pq.top() < 0)
          pq.pop();
        else
          break;
      }
      while (!pq.empty()) {
        t += pq.top();
        pq.pop();
      }
      m = max(m, t);
    }
  }
  cout << m << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n, k, m = 0;
  cin >> n >> k;
  deque<int> d;
  rep(i, n) {
    int e;
    cin >> e;
    d.push_back(e);
  }
  rep(i, k + 1) {
    rep(j, k - i + 1) {
      int t = 0, a = 0, b = 0;
      deque<int> c = d;
      priority_queue<int, vector<int>, greater<int>> pq;
      while (a < i && !c.empty()) {
        pq.push(c.front());
        c.pop_front();
        ++a;
      }
      while (b < j && !c.empty()) {
        pq.push(c.back());
        c.pop_back();
        ++b;
      }
      rep(l, k - a - b) {
        if (pq.empty())
          break;
        if (pq.top() < 0)
          pq.pop();
        else
          break;
      }
      while (!pq.empty()) {
        t += pq.top();
        pq.pop();
      }
      m = max(m, t);
    }
  }
  cout << m << endl;
  return 0;
}