Problem p03300 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
// #pragma GCC target("avx,tune=native")
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 1000000007;
typedef long long ll;
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
inline string getstr(string &s, int l, int r) {
  string ret = "";
  for (int i = l; i <= r; i++)
    ret.push_back(s[i]);
  return ret;
}
int modpow(int x, int y, int md = mod) {
  if (y == 0)
    return 1;
  int ret = modpow(x, y >> 1, md);
  ret = (ll)ret * ret % md;
  if (y & 1)
    ret = (ll)ret * x % md;
  return ret;
}
int n;
string s;
string dp[3003];
string suf[3003];
int pos[6003];
vector<int> a, b;
int main() {
  cin >> n >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'a')
      pos[i] = a.size(), a.push_back(i);
    else
      pos[i] = b.size(), b.push_back(i);
  }
  dp[n] = "";
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] < b[i]) {
      // ab
      int mx = -inf;
      for (int j = a[i]; j <= b[i]; j++) {
        mx = max(mx, pos[j]);
      }
      dp[i] = max(dp[i], "ab" + dp[mx + 1]);
    } else {
      // ba
      int tmp = i;
      while (b[tmp + 1] < a[tmp])
        tmp++;
      for (int j = b[i]; j < s.size(); j++)
        if (i <= pos[j] && pos[j] <= tmp)
          dp[i].push_back(s[j]);
      dp[i] += dp[tmp + 1];
    }
    dp[i] = max(dp[i], dp[i + 1]);
  }
  cout << *max_element(dp, dp + n) << endl;
  return 0;
}

Fixed submission:
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
// #pragma GCC target("avx,tune=native")
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 1000000007;
typedef long long ll;
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
inline string getstr(string &s, int l, int r) {
  string ret = "";
  for (int i = l; i <= r; i++)
    ret.push_back(s[i]);
  return ret;
}
int modpow(int x, int y, int md = mod) {
  if (y == 0)
    return 1;
  int ret = modpow(x, y >> 1, md);
  ret = (ll)ret * ret % md;
  if (y & 1)
    ret = (ll)ret * x % md;
  return ret;
}
int n;
string s;
string dp[3003];
string suf[3003];
int pos[6003];
vector<int> a, b;
int main() {
  cin >> n >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'a')
      pos[i] = a.size(), a.push_back(i);
    else
      pos[i] = b.size(), b.push_back(i);
  }
  dp[n] = "";
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] < b[i]) {
      // ab
      int mx = -inf;
      for (int j = a[i]; j <= b[i]; j++) {
        mx = max(mx, pos[j]);
      }
      dp[i] = max(dp[i], "ab" + dp[mx + 1]);
    } else {
      // ba
      int tmp = i;
      while (tmp < n - 1 && b[tmp + 1] < a[tmp])
        tmp++;
      for (int j = b[i]; j < s.size(); j++)
        if (i <= pos[j] && pos[j] <= tmp)
          dp[i].push_back(s[j]);
      dp[i] += dp[tmp + 1];
    }
    dp[i] = max(dp[i], dp[i + 1]);
  }
  cout << *max_element(dp, dp + n) << endl;
  return 0;
}