Problem p03357 (C++)

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

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ends << H;
  debug_out(T...);
}
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

////////////////////////////////////////////////////////////////////

const int N = 2e3 + 5;

int A[N], DP[N][N], B[N], W[N], BW[N][N], WB[N][N], WW[N], BB[N];
char C[N];

int main() {

  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  mt19937 Rnd(time(0));

  int n;
  cin >> n;
  for (int i = 0; i < n + n; i++)
    cin >> C[i] >> A[i], A[i]--, (C[i] == 'B' ? B : W)[A[i]] = i;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < i; j++)
      WW[i + 1] += (W[j] > W[i]), BB[i + 1] += (B[j] > B[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      BW[i + 1][j + 1] = BW[i + 1][j] + (W[j] > B[i]),
                    WB[i + 1][j + 1] = WB[i + 1][j] + (B[j] > W[i]);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      if (i + j > 0) {
        DP[i][j] = INF;
        if (i)
          DP[i][j] = min(DP[i][j], DP[i - 1][j] + BB[i] + BW[i][j]);
        if (j)
          DP[i][j] = min(DP[i][j], DP[i][j - 1] + WW[j] + WB[j][i]);
      }
  cout << DP[n][n] << endl;

  return 0;
}


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

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ends << H;
  debug_out(T...);
}
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

////////////////////////////////////////////////////////////////////

const int N = 2e3 + 5;

int A[N << 1], DP[N][N], B[N], W[N], BW[N][N], WB[N][N], WW[N], BB[N];
char C[N << 1];

int main() {

  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  mt19937 Rnd(time(0));

  int n;
  cin >> n;
  for (int i = 0; i < n + n; i++)
    cin >> C[i] >> A[i], A[i]--, (C[i] == 'B' ? B : W)[A[i]] = i;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < i; j++)
      WW[i + 1] += (W[j] > W[i]), BB[i + 1] += (B[j] > B[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      BW[i + 1][j + 1] = BW[i + 1][j] + (W[j] > B[i]),
                    WB[i + 1][j + 1] = WB[i + 1][j] + (B[j] > W[i]);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      if (i + j > 0) {
        DP[i][j] = INF;
        if (i)
          DP[i][j] = min(DP[i][j], DP[i - 1][j] + BB[i] + BW[i][j]);
        if (j)
          DP[i][j] = min(DP[i][j], DP[i][j - 1] + WW[j] + WB[j][i]);
      }
  cout << DP[n][n] << endl;

  return 0;
}