Problem p03300 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int N = 3005;
int a[N];
int d[N];
bool take[N];

vector<int> solve(vector<int> g) {
  if (g[0] == 0) {
    vector<int> pa, pb;
    for (int i = 0; i < g.size(); i++) {
      if (g[i] == 0)
        pa.push_back(i);
      else
        pb.push_back(i);
    }
    int cur = -1;
    vector<int> sol;
    for (int i = 0; i < pa.size(); i++) {
      if (pa[i] > cur) {
        sol.push_back(0);
        sol.push_back(1);
        cur = pb[i];
      }
    }
    return sol;
  } else {
    vector<int> best;
    vector<int> pa, pb;
    for (int i = 0; i < g.size(); i++) {
      if (g[i] == 0)
        pa.push_back(i);
      else
        pb.push_back(i);
    }
    for (int i = 0; i < pb.size(); i++) {
      for (int j = 0; j < g.size(); j++) {
        take[j] = false;
      }
      for (int j = i; j < pb.size(); j++) {
        take[pb[j]] = true;
        take[pa[j]] = true;
      }
      vector<int> cur;
      for (int j = 0; j < g.size(); j++) {
        if (take[j])
          cur.push_back(g[j]);
      }
      if (cur > best)
        best = cur;
    }
    return best;
  }
}

int main() {
  fastIO;
  int n;
  cin >> n;
  n *= 2;
  char f;
  for (int i = 1; i <= n; i++) {
    cin >> f;
    a[i] = f - 'a';
  }
  for (int i = 1; i <= n; i++) {
    d[i] = d[i - 1] + (a[i] == 0 ? 1 : -1);
  }
  vector<string> parts;
  int b;
  for (int i = 1; i <= n; i++) {
    if (d[i] == 0) {
      b = i;
      while (d[b - 1] != 0)
        b--;
      vector<int> pp;
      for (int j = b; j <= i; j++) {
        pp.push_back(a[j]);
      }
      vector<int> bes = solve(pp);
      string go;
      for (int x : bes) {
        go.push_back((x == 0 ? 'a' : 'b'));
      }
      parts.push_back(go);
    }
  }
  string sol;
  for (int i = (int)parts.size() - 1; i >= 0; i--) {
    sol = max(sol, parts[i] + sol);
  }
  cout << sol << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int N = 6005;
int a[N];
int d[N];
bool take[N];

vector<int> solve(vector<int> g) {
  if (g[0] == 0) {
    vector<int> pa, pb;
    for (int i = 0; i < g.size(); i++) {
      if (g[i] == 0)
        pa.push_back(i);
      else
        pb.push_back(i);
    }
    int cur = -1;
    vector<int> sol;
    for (int i = 0; i < pa.size(); i++) {
      if (pa[i] > cur) {
        sol.push_back(0);
        sol.push_back(1);
        cur = pb[i];
      }
    }
    return sol;
  } else {
    vector<int> best;
    vector<int> pa, pb;
    for (int i = 0; i < g.size(); i++) {
      if (g[i] == 0)
        pa.push_back(i);
      else
        pb.push_back(i);
    }
    for (int i = 0; i < pb.size(); i++) {
      for (int j = 0; j < g.size(); j++) {
        take[j] = false;
      }
      for (int j = i; j < pb.size(); j++) {
        take[pb[j]] = true;
        take[pa[j]] = true;
      }
      vector<int> cur;
      for (int j = 0; j < g.size(); j++) {
        if (take[j])
          cur.push_back(g[j]);
      }
      if (cur > best)
        best = cur;
    }
    return best;
  }
}

int main() {
  fastIO;
  int n;
  cin >> n;
  n *= 2;
  char f;
  for (int i = 1; i <= n; i++) {
    cin >> f;
    a[i] = f - 'a';
  }
  for (int i = 1; i <= n; i++) {
    d[i] = d[i - 1] + (a[i] == 0 ? 1 : -1);
  }
  vector<string> parts;
  int b;
  for (int i = 1; i <= n; i++) {
    if (d[i] == 0) {
      b = i;
      while (d[b - 1] != 0)
        b--;
      vector<int> pp;
      for (int j = b; j <= i; j++) {
        pp.push_back(a[j]);
      }
      vector<int> bes = solve(pp);
      string go;
      for (int x : bes) {
        go.push_back((x == 0 ? 'a' : 'b'));
      }
      parts.push_back(go);
    }
  }
  string sol;
  for (int i = (int)parts.size() - 1; i >= 0; i--) {
    sol = max(sol, parts[i] + sol);
  }
  cout << sol << "\n";
  return 0;
}