Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/
const int MAXN = 2100;

int n;
int a[MAXN];
int c[MAXN];
int dp[MAXN][MAXN];
int dd[MAXN][2][MAXN];
int pos[2][MAXN];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  cin >> n;
  for (int i = 0; i < n * 2; ++i) {
    char ch;
    cin >> ch >> a[i];
    --a[i];
    if (ch == 'W')
      c[i] = 0;
    else
      c[i] = 1;
    pos[c[i]][a[i]] = i;
  }
  for (int i = 0; i < 2 * n; ++i) {
    for (int j = 0; j < i; ++j) {
      ++dd[i][c[j]][a[j] + 1];
    }
    for (int j = 0; j < n; ++j) {
      dd[i][0][j + 1] += dd[i][0][j];
      dd[i][1][j + 1] += dd[i][1][j];
    }
  }
  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= n; ++j)
      dp[i][j] = int(1e9);
  dp[0][0] = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      if (i < n) {
        int x = pos[0][i];
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dd[x][0][n] - dd[x][0][i] +
                                             dd[x][1][n] - dd[x][1][j]);
      }
      if (j < n) {
        int x = pos[1][j];
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + dd[x][0][n] - dd[x][0][i] +
                                             dd[x][1][n] - dd[x][1][j]);
      }
    }
  }
  cout << dp[n][n] << "\n";
  return 0;
}


Fixed submission:
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/
const int MAXN = 4100;

int n;
int a[MAXN];
int c[MAXN];
int dp[MAXN][MAXN];
int dd[MAXN][2][MAXN];
int pos[2][MAXN];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  cin >> n;
  for (int i = 0; i < n * 2; ++i) {
    char ch;
    cin >> ch >> a[i];
    --a[i];
    if (ch == 'W')
      c[i] = 0;
    else
      c[i] = 1;
    pos[c[i]][a[i]] = i;
  }
  for (int i = 0; i < 2 * n; ++i) {
    for (int j = 0; j < i; ++j) {
      ++dd[i][c[j]][a[j] + 1];
    }
    for (int j = 0; j < n; ++j) {
      dd[i][0][j + 1] += dd[i][0][j];
      dd[i][1][j + 1] += dd[i][1][j];
    }
  }
  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= n; ++j)
      dp[i][j] = int(1e9);
  dp[0][0] = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      if (i < n) {
        int x = pos[0][i];
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dd[x][0][n] - dd[x][0][i] +
                                             dd[x][1][n] - dd[x][1][j]);
      }
      if (j < n) {
        int x = pos[1][j];
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + dd[x][0][n] - dd[x][0][i] +
                                             dd[x][1][n] - dd[x][1][j]);
      }
    }
  }
  cout << dp[n][n] << "\n";
  return 0;
}