Problem p03300 (C++)

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

#define MAX 6012

int n;

vector<bool> dp[MAX];

vector<pair<int, int>> pos; // b , a
int mode[MAX];
bool us[MAX];

bool use[MAX];
char buf[MAX];
string s;
vector<int> v1;
vector<int> v2;
bool cmp(vector<bool> a, vector<bool> b) {
  int lef = 0;
  int rig = 0;
  while (1) {
    while (lef < a.size() && a[lef] == false) {
      lef++;
    }
    while (rig < b.size() && b[rig] == false) {
      rig++;
    }
    if (rig == b.size())
      return true;
    if (lef == a.size())
      return false;
    if (s[lef] != s[rig])
      return s[lef] > s[rig];
    lef++;
    rig++;
  }
  return false;
}
vector<bool> dfs(int b) {
  if (b == n)
    return vector<bool>(2 * n, 0);
  if (us[b])
    return dp[b];
  us[b] = true;
  dp[b] = dfs(b + 1); // choose nothing
  if (pos[b].first < pos[b].second) {
    int til;
    vector<bool> flag(2 * n, 0);
    for (int j = b; j < pos.size(); j++) {
      if (pos[j].first > pos[b].second) {
        break;
      }
      til = j;
      flag[pos[j].first] = flag[pos[j].second] = true;
    }
    for (int j = til + 1; j < pos.size(); j++) {
      if (pos[j].first > pos[j - 1].second)
        break;
      flag[pos[j].first] = flag[pos[j].second] = true;
      til = j;
    }
    auto nex = dfs(til + 1);
    for (int i = 0; i < 2 * n; i++) {
      flag[i] = (flag[i] || nex[i]);
    }
    if (cmp(flag, dp[b])) {
      dp[b] = flag;
    }
  } else {
    for (int j = b + 1; j < pos.size(); j++) {
      if (min(pos[j].first, pos[j].second) > pos[b].first) {
        auto nex = dfs(j);
        nex[pos[b].first] = nex[pos[b].second] = true;
        string ss;
        for (int i = 0; i < nex.size(); i++) {
          if (nex[i])
            ss.push_back(s[i]);
        }
        if (cmp(nex, dp[b])) {
          dp[b] = nex;
        }
      }
    }
    vector<bool> fl(2 * n, false);
    fl[pos[b].first] = fl[pos[b].second] = true;
    if (cmp(fl, dp[b])) {
      dp[b] = fl;
    }
  }
  return dp[b];
}

int main() {
  cin >> n;
  scanf("%s", buf);
  s = buf;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'a') {
      v1.push_back(i);
    }
    if (s[i] == 'b') {
      v2.push_back(i);
    }
  }
  for (int i = 0; i < v1.size(); i++) {
    pos.push_back(make_pair(v2[i], v1[i]));
  }
  vector<bool> uss = dfs(0);
  // dfs2(0);
  for (int i = 0; i < s.size(); i++) {
    if (uss[i]) {
      putchar(s[i]);
    }
  }
  puts("");
  return 0;
}


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

#define MAX 6012

int n;

vector<bool> dp[MAX];

vector<pair<int, int>> pos; // b , a
int mode[MAX];
bool us[MAX];

bool use[MAX];
char buf[MAX];
string s;
vector<int> v1;
vector<int> v2;
bool cmp(vector<bool> a, vector<bool> b) {
  int lef = 0;
  int rig = 0;
  while (1) {
    while (lef < a.size() && a[lef] == false) {
      lef++;
    }
    while (rig < b.size() && b[rig] == false) {
      rig++;
    }
    if (rig == b.size())
      return true;
    if (lef == a.size())
      return false;
    if (s[lef] != s[rig])
      return s[lef] > s[rig];
    lef++;
    rig++;
  }
  return false;
}
vector<bool> dfs(int b) {
  if (b == n)
    return vector<bool>(2 * n, 0);
  if (us[b])
    return dp[b];
  us[b] = true;
  dp[b] = dfs(b + 1); // choose nothing
  if (pos[b].first < pos[b].second) {
    int til;
    vector<bool> flag(2 * n, 0);
    for (int j = b; j < pos.size(); j++) {
      if (pos[j].first > pos[b].second) {
        break;
      }
      til = j;
      flag[pos[j].first] = flag[pos[j].second] = true;
    }
    for (int j = til + 1; j < pos.size(); j++) {
      if (pos[j].first > pos[j - 1].second)
        break;
      flag[pos[j].first] = flag[pos[j].second] = true;
      til = j;
    }
    auto nex = dfs(til + 1);
    for (int i = 0; i < 2 * n; i++) {
      flag[i] = (flag[i] || nex[i]);
    }
    if (cmp(flag, dp[b])) {
      dp[b] = flag;
    }
  } else {
    for (int j = b + 1; j < pos.size(); j++) {
      if (min(pos[j].first, pos[j].second) > pos[b].first) {
        auto nex = dfs(j);
        nex[pos[b].first] = nex[pos[b].second] = true;
        string ss;
        for (int i = 0; i < nex.size(); i++) {
          if (nex[i])
            ss.push_back(s[i]);
        }
        if (cmp(nex, dp[b])) {
          dp[b] = nex;
        }
        break;
      }
    }
    vector<bool> fl(2 * n, false);
    fl[pos[b].first] = fl[pos[b].second] = true;
    if (cmp(fl, dp[b])) {
      dp[b] = fl;
    }
  }
  return dp[b];
}

int main() {
  cin >> n;
  scanf("%s", buf);
  s = buf;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'a') {
      v1.push_back(i);
    }
    if (s[i] == 'b') {
      v2.push_back(i);
    }
  }
  for (int i = 0; i < v1.size(); i++) {
    pos.push_back(make_pair(v2[i], v1[i]));
  }
  vector<bool> uss = dfs(0);
  // dfs2(0);
  for (int i = 0; i < s.size(); i++) {
    if (uss[i]) {
      putchar(s[i]);
    }
  }
  puts("");
  return 0;
}