Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
const int M = 2002;
int a[M], pos[2][M], dp[M][M];
char c[M];
std::vector<int> vec[2][M];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= 2 * n; i++) {
    cin >> c[i] >> a[i];
    vec[0][i] = vec[0][i - 1];
    vec[1][i] = vec[1][i - 1];
    if (c[i] == 'B')
      vec[0][i].pb(a[i]);
    else
      vec[1][i].pb(a[i]);
    sort(all(vec[0][i]));
    sort(all(vec[1][i]));
    if (c[i] == 'B')
      pos[0][a[i]] = i;
    else
      pos[1][a[i]] = i;
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i + j)
        dp[i][j] = 1e9;
      // (i-1,j)
      if (i) {
        int z = pos[0][i];
        int x = (int)(upper_bound(all(vec[0][z]), i - 1) - vec[0][z].begin());
        int y = (int)(upper_bound(all(vec[1][z]), j) - vec[1][z].begin());
        int res = dp[i - 1][j] + i - 1 - x + sz(vec[0][z]) - x + j - y +
                  sz(vec[1][z]) - y;
        dp[i][j] = min(dp[i][j], res - 1);
      }
      if (j) {
        int z = pos[1][j];
        int x = (int)(upper_bound(all(vec[0][z]), i) - vec[0][z].begin());
        int y = (int)(upper_bound(all(vec[1][z]), j - 1) - vec[1][z].begin());
        int res = dp[i][j - 1] + i - x + sz(vec[0][z]) - x + j - 1 - y +
                  sz(vec[1][z]) - y;
        dp[i][j] = min(dp[i][j], res - 1);
      }
    }
  }
  cout << dp[n][n] / 2 << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
const int M = 4002;
int a[M], pos[2][M], dp[M][M];
char c[M];
std::vector<int> vec[2][M];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= 2 * n; i++) {
    cin >> c[i] >> a[i];
    vec[0][i] = vec[0][i - 1];
    vec[1][i] = vec[1][i - 1];
    if (c[i] == 'B')
      vec[0][i].pb(a[i]);
    else
      vec[1][i].pb(a[i]);
    sort(all(vec[0][i]));
    sort(all(vec[1][i]));
    if (c[i] == 'B')
      pos[0][a[i]] = i;
    else
      pos[1][a[i]] = i;
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i + j)
        dp[i][j] = 1e9;
      // (i-1,j)
      if (i) {
        int z = pos[0][i];
        int x = (int)(upper_bound(all(vec[0][z]), i - 1) - vec[0][z].begin());
        int y = (int)(upper_bound(all(vec[1][z]), j) - vec[1][z].begin());
        int res = dp[i - 1][j] + i - 1 - x + sz(vec[0][z]) - x + j - y +
                  sz(vec[1][z]) - y;
        dp[i][j] = min(dp[i][j], res - 1);
      }
      if (j) {
        int z = pos[1][j];
        int x = (int)(upper_bound(all(vec[0][z]), i) - vec[0][z].begin());
        int y = (int)(upper_bound(all(vec[1][z]), j - 1) - vec[1][z].begin());
        int res = dp[i][j - 1] + i - x + sz(vec[0][z]) - x + j - 1 - y +
                  sz(vec[1][z]) - y;
        dp[i][j] = min(dp[i][j], res - 1);
      }
    }
  }
  cout << dp[n][n] / 2 << "\n";
  return 0;
}