Problem p03300 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
// #include <cstdlib>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
typedef double db;
const int oo = 2139063143;
const int N = 3010;
const int P = 1000000007;
const db eps = 1e-7;
#define pritnf printf
// char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;
// #define getchar()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T> inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T> inline void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void pr(T x) { print(x), putchar('\n'); }
template <typename T1, typename T2> void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2> void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}

string f[N], s, ans;
int n, a[N], b[N], id[N], p[N];
int find(int x) {
  for (int i = 1; i <= n >> 1; i++)
    if (min(a[i], b[i]) > x)
      return i;
  return n / 2 + 1;
}
string get(int l, int r) {
  string g = "";
  for (int i = l; i <= r; i++)
    if (id[i] >= id[l])
      g += s[i];
  return g;
}
int main() {
  // freopen (".in","r",stdin);
  // freopen (".out","w",stdout);
  sc(n);
  n <<= 1;
  cin >> s;
  for (int i = 0; i < n; i++)
    if (s[i] == 'a')
      a[++a[0]] = i, id[i] = a[0];
    else
      b[++b[0]] = i, id[i] = b[0];
  for (int i = n >> 1; i >= 1; i--) {
    f[i] = f[i + 1];
    if (a[i] < b[i]) {
      chkmax(f[i], "ab" + f[find(b[i])]);
    } else {
      int nx = a[i];
      for (int j = b[i]; j <= nx; j++)
        if (s[j] == 'b')
          chkmax(nx, a[id[j]]);
      chkmax(f[i], get(b[i], nx) + f[find(nx)]);
    }
    chkmax(ans, f[i]);
    // cout<<i<<' '<<f[i]<<endl;
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <algorithm>
// #include <cstdlib>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
typedef double db;
const int oo = 2139063143;
const int N = 6010;
const int P = 1000000007;
const db eps = 1e-7;
#define pritnf printf
// char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;
// #define getchar()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T> inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T> inline void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void pr(T x) { print(x), putchar('\n'); }
template <typename T1, typename T2> void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2> void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}

string f[N], s, ans;
int n, a[N], b[N], id[N], p[N];
int find(int x) {
  for (int i = 1; i <= n >> 1; i++)
    if (min(a[i], b[i]) > x)
      return i;
  return n / 2 + 1;
}
string get(int l, int r) {
  string g = "";
  for (int i = l; i <= r; i++)
    if (id[i] >= id[l])
      g += s[i];
  return g;
}
int main() {
  // freopen (".in","r",stdin);
  // freopen (".out","w",stdout);
  sc(n);
  n <<= 1;
  cin >> s;
  for (int i = 0; i < n; i++)
    if (s[i] == 'a')
      a[++a[0]] = i, id[i] = a[0];
    else
      b[++b[0]] = i, id[i] = b[0];
  for (int i = n >> 1; i >= 1; i--) {
    f[i] = f[i + 1];
    if (a[i] < b[i]) {
      chkmax(f[i], "ab" + f[find(b[i])]);
    } else {
      int nx = a[i];
      for (int j = b[i]; j <= nx; j++)
        if (s[j] == 'b')
          chkmax(nx, a[id[j]]);
      chkmax(f[i], get(b[i], nx) + f[find(nx)]);
    }
    chkmax(ans, f[i]);
    // cout<<i<<' '<<f[i]<<endl;
  }
  cout << ans;

  return 0;
}