Problem p03357 (C++)

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

typedef pair<int, int> pii;
typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> bool chmax(T &t, const U &u) {
  if (t < u) {
    t = u;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmin(T &t, const U &u) {
  if (t > u) {
    t = u;
    return 1;
  }
  return 0;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int INF = TEN(9);

int dp[2010][2010];
int pos[2][2010];
int num[2][2010][2010];

int main() {
  int N;
  cin >> N;
  V<pii> vec;
  rep(i, N * 2) {
    char c;
    int a;
    cin >> c >> a;
    --a;
    bool f = (c == 'B');
    vec.eb(f, a);
    pos[f][a] = i;
  }

  rep(c, 2) {
    rep(i, N) {
      int p = pos[c][i];
      for (int j = 0; j <= i; ++j) {
        ++num[c][p][j];
      }
    }

    rep(i, N * 2) {
      rep(j, N) { num[c][i + 1][j] += num[c][i][j]; }
    }
  }

  cerr << vec << endl;

  rep(i, 2010) {
    rep(j, 2010) { dp[i][j] = INF; }
  }
  dp[0][0] = 0;

  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= N; ++j) {
      if (i < N) { // put W
        int p = pos[0][i];
        int val = num[0][p][i + 1] + num[1][p][j];

        chmin(dp[i + 1][j], dp[i][j] + val);
      }
      if (j < N) { // put W
        int p = pos[1][j];
        int val = num[0][p][i] + num[1][p][j + 1];

        chmin(dp[i][j + 1], dp[i][j] + val);
      }
    }
  }
  cout << dp[N][N] << endl;

  return 0;
}

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

typedef pair<int, int> pii;
typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> bool chmax(T &t, const U &u) {
  if (t < u) {
    t = u;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmin(T &t, const U &u) {
  if (t > u) {
    t = u;
    return 1;
  }
  return 0;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int INF = TEN(9);

int dp[2010][2010];
int pos[2][2010];
int num[2][4010][2010];

int main() {
  int N;
  cin >> N;
  V<pii> vec;
  rep(i, N * 2) {
    char c;
    int a;
    cin >> c >> a;
    --a;
    bool f = (c == 'B');
    vec.eb(f, a);
    pos[f][a] = i;
  }

  rep(c, 2) {
    rep(i, N) {
      int p = pos[c][i];
      for (int j = 0; j <= i; ++j) {
        ++num[c][p][j];
      }
    }

    rep(i, N * 2) {
      rep(j, N) { num[c][i + 1][j] += num[c][i][j]; }
    }
  }

  cerr << vec << endl;

  rep(i, 2010) {
    rep(j, 2010) { dp[i][j] = INF; }
  }
  dp[0][0] = 0;

  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= N; ++j) {
      if (i < N) { // put W
        int p = pos[0][i];
        int val = num[0][p][i + 1] + num[1][p][j];

        chmin(dp[i + 1][j], dp[i][j] + val);
      }
      if (j < N) { // put W
        int p = pos[1][j];
        int val = num[0][p][i] + num[1][p][j + 1];

        chmin(dp[i][j + 1], dp[i][j] + val);
      }
    }
  }
  cout << dp[N][N] << endl;

  return 0;
}