Problem p03300 (C++)

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

// Shortcut

#define int long long
#define endl '\n'
#define eb emplace_back
#define pb push_back
#define pob pop_back
#define mp make_pair
#define upb upper_bound
#define lwb lower_bound
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define Ford(i, r, l) for (int i = r; i > l; i--)
#define FordE(i, r, l) for (int i = r; i >= l; i--)
#define Fora(i, a) for (auto i : a)

// I/O & Debug

#define PrintV(a)                                                              \
  for (int iiii = 0; iiii < a.size(); iiii++)                                  \
    cout << a[iiii] << ' ';
#define PrintVl(a)                                                             \
  for (int iiii = 0; iiii < a.size(); iiii++)                                  \
    cout << a[iiii] << endl;
#define PrintA(a, n)                                                           \
  for (int iiii = 0; iiii < n; iiii++)                                         \
    cout << a[iiii] << ' ';
#define PrintAl(a, n)                                                          \
  for (int iiii = 0; iiii < n; iiii++)                                         \
    cout << a[iiii] << endl;
#define Ptest(x) return cout << x, 0;
#define gl(s) getline(cin, s);
#define setpre(x) fixed << setprecision(x)
/*
#define debug(args...){ string _sDEB = #args; replace(_sDEB.begin(),
_sDEB.end(), ',', ' '); stringstream _ssDEB(_sDEB); istream_iterator<string>
_itDEB(_ssDEB); DEB(_itDEB, args); } void DEB(istream_iterator<string> it) {}
template<typename T, typename... Args>
void DEB(istream_iterator<string> it, T a, Args... args){
    cout << *it << " = " << a << endl;
    DEB(++it, args...);
}
*/

// Functions

// #define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define bend(a) a.begin(), a.end()
#define rbend(a) a.rbegin(), a.rend()
#define mset(a) memset(a, 0, sizeof(a));
// mt19937 rando(chrono::steady_clock::now().time_since_epoch().count());

// Data Structure

#define pque priority_queue
#define mts multiset
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

const int N = 3e3 + 5, mod = 1e9 + 7, inf = 1e9 + 7;

int n;
int a[N];
string s, ans = "";
vs b, c;

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  int tmp = 0, x = 0, y = 0;
  For(i, 0, s.size()) {
    if (s[i] == 'a') {
      a[i] = ++x;
    } else {
      a[i] = ++y;
    }
    if (x == y) {
      string t = "";
      if (s[tmp] == 'a') {
        int z = 0;
        ForE(j, tmp, i) {
          if (z == a[j]) {
            z = 0;
          }
          if (!z && s[j] == 'a') {
            t += "ab";
            z = a[j];
          }
        }
      } else {
        ForE(j, a[tmp], a[i]) {
          string u = "";
          ForE(k, tmp, i) {
            if (a[k] >= j) {
              u += s[k];
            }
          }
          t = max(t, u);
        }
      }
      b.pb(t);
      tmp = i + 1;
    }
  }
  c.assign(b.size(), "");
  For(i, 0, b.size()) {
    c[i] = b[i];
    For(j, 0, i) { c[i] = max(c[i], c[j] + b[i]); }
    ans = max(ans, c[i]);
  }
  cout << ans;
}

/*
==================================+
INPUT:                            |
------------------------------    |

------------------------------    |
==================================+
OUTPUT:                           |
------------------------------    |

------------------------------    |
==================================+
*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// Shortcut

#define int long long
#define endl '\n'
#define eb emplace_back
#define pb push_back
#define pob pop_back
#define mp make_pair
#define upb upper_bound
#define lwb lower_bound
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define Ford(i, r, l) for (int i = r; i > l; i--)
#define FordE(i, r, l) for (int i = r; i >= l; i--)
#define Fora(i, a) for (auto i : a)

// I/O & Debug

#define PrintV(a)                                                              \
  for (int iiii = 0; iiii < a.size(); iiii++)                                  \
    cout << a[iiii] << ' ';
#define PrintVl(a)                                                             \
  for (int iiii = 0; iiii < a.size(); iiii++)                                  \
    cout << a[iiii] << endl;
#define PrintA(a, n)                                                           \
  for (int iiii = 0; iiii < n; iiii++)                                         \
    cout << a[iiii] << ' ';
#define PrintAl(a, n)                                                          \
  for (int iiii = 0; iiii < n; iiii++)                                         \
    cout << a[iiii] << endl;
#define Ptest(x) return cout << x, 0;
#define gl(s) getline(cin, s);
#define setpre(x) fixed << setprecision(x)
/*
#define debug(args...){ string _sDEB = #args; replace(_sDEB.begin(),
_sDEB.end(), ',', ' '); stringstream _ssDEB(_sDEB); istream_iterator<string>
_itDEB(_ssDEB); DEB(_itDEB, args); } void DEB(istream_iterator<string> it) {}
template<typename T, typename... Args>
void DEB(istream_iterator<string> it, T a, Args... args){
    cout << *it << " = " << a << endl;
    DEB(++it, args...);
}
*/

// Functions

// #define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define bend(a) a.begin(), a.end()
#define rbend(a) a.rbegin(), a.rend()
#define mset(a) memset(a, 0, sizeof(a));
// mt19937 rando(chrono::steady_clock::now().time_since_epoch().count());

// Data Structure

#define pque priority_queue
#define mts multiset
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

const int N = 3e3 + 5, mod = 1e9 + 7, inf = 1e9 + 7;

int n;
int a[2 * N];
string s, ans = "";
vs b, c;

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  int tmp = 0, x = 0, y = 0;
  For(i, 0, s.size()) {
    if (s[i] == 'a') {
      a[i] = ++x;
    } else {
      a[i] = ++y;
    }
    if (x == y) {
      string t = "";
      if (s[tmp] == 'a') {
        int z = 0;
        ForE(j, tmp, i) {
          if (z == a[j]) {
            z = 0;
          }
          if (!z && s[j] == 'a') {
            t += "ab";
            z = a[j];
          }
        }
      } else {
        ForE(j, a[tmp], a[i]) {
          string u = "";
          ForE(k, tmp, i) {
            if (a[k] >= j) {
              u += s[k];
            }
          }
          t = max(t, u);
        }
      }
      b.pb(t);
      tmp = i + 1;
    }
  }
  c.assign(b.size(), "");
  For(i, 0, b.size()) {
    c[i] = b[i];
    For(j, 0, i) { c[i] = max(c[i], c[j] + b[i]); }
    ans = max(ans, c[i]);
  }
  cout << ans;
}

/*
==================================+
INPUT:                            |
------------------------------    |

------------------------------    |
==================================+
OUTPUT:                           |
------------------------------    |

------------------------------    |
==================================+
*/