Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
const int INF = 1001001000;
const int mINF = -1001001000;
const ll LINF = 1010010010010010000;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// グリッド：（典型）dp,dfs,bfs,最短経路,その他
ll n;
char c[4200];
ll a[4200];
ll sumB[2200][2200];
ll sumW[2200][2200];
vl idxB(2200), idxW(2200);
ll dp[2200][2200];
ll f(ll b, ll w) {
  if (b == n && w == n)
    return 0;
  if (0 <= dp[b][w])
    return dp[b][w];
  ll res = INF;
  if (b != n) {
    ll cnt = 0;
    if (0 <= idxB[b + 1] - 1)
      cnt += sumB[b + 1][idxB[b + 1] - 1];
    if (0 <= idxB[b + 1] - 1)
      cnt += sumW[w + 1][idxB[b + 1] - 1];
    chmin(res, f(b + 1, w) + cnt);
  }
  if (w != n) {
    ll cnt = 0;
    if (0 <= idxW[w + 1] - 1)
      cnt += sumB[b + 1][idxW[w + 1] - 1];
    if (0 <= idxW[w + 1] - 1)
      cnt += sumW[w + 1][idxW[w + 1] - 1];
    chmin(res, f(b, w + 1) + cnt);
  }
  return dp[b][w] = res;
}
int main() {
  cin >> n;
  rep(i, 2 * n) cin >> c[i] >> a[i];
  rep(i, 2 * n) {
    if (c[i] == 'B')
      sumB[a[i]][i]++;
    else
      sumW[a[i]][i]++;
  }
  for (int i = 1; i <= 2 * n; i++) {
    for (int j = 1; j < 2 * n; j++)
      sumB[i][j] += sumB[i][j - 1];
    for (int j = 1; j < 2 * n; j++)
      sumW[i][j] += sumW[i][j - 1];
  }
  for (int j = 0; j < 2 * n; j++) {
    for (int i = n; i >= 1; i--)
      sumB[i][j] += sumB[i + 1][j];
    for (int i = n; i >= 1; i--)
      sumW[i][j] += sumW[i + 1][j];
  }
  rep(i, 2 * n) {
    if (c[i] == 'B')
      idxB[a[i]] = i;
    else
      idxW[a[i]] = i;
  }
  rep(i, 2200) {
    rep(j, 2200) { dp[i][j] = -1; }
  }
  cout << f(0, 0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
const int INF = 1001001000;
const int mINF = -1001001000;
const ll LINF = 1010010010010010000;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// グリッド：（典型）dp,dfs,bfs,最短経路,その他
ll n;
char c[4200];
ll a[4200];
ll sumB[4200][4200];
ll sumW[4200][4200];
vl idxB(4200), idxW(4200);
ll dp[2200][2200];
ll f(ll b, ll w) {
  if (b == n && w == n)
    return 0;
  if (0 <= dp[b][w])
    return dp[b][w];
  ll res = INF;
  if (b != n) {
    ll cnt = 0;
    if (0 <= idxB[b + 1] - 1)
      cnt += sumB[b + 1][idxB[b + 1] - 1];
    if (0 <= idxB[b + 1] - 1)
      cnt += sumW[w + 1][idxB[b + 1] - 1];
    chmin(res, f(b + 1, w) + cnt);
  }
  if (w != n) {
    ll cnt = 0;
    if (0 <= idxW[w + 1] - 1)
      cnt += sumB[b + 1][idxW[w + 1] - 1];
    if (0 <= idxW[w + 1] - 1)
      cnt += sumW[w + 1][idxW[w + 1] - 1];
    chmin(res, f(b, w + 1) + cnt);
  }
  return dp[b][w] = res;
}
int main() {
  cin >> n;
  rep(i, 2 * n) cin >> c[i] >> a[i];
  rep(i, 2 * n) {
    if (c[i] == 'B')
      sumB[a[i]][i]++;
    else
      sumW[a[i]][i]++;
  }
  for (int i = 1; i <= 2 * n; i++) {
    for (int j = 1; j < 2 * n; j++)
      sumB[i][j] += sumB[i][j - 1];
    for (int j = 1; j < 2 * n; j++)
      sumW[i][j] += sumW[i][j - 1];
  }
  for (int j = 0; j < 2 * n; j++) {
    for (int i = n; i >= 1; i--)
      sumB[i][j] += sumB[i + 1][j];
    for (int i = n; i >= 1; i--)
      sumW[i][j] += sumW[i + 1][j];
  }
  rep(i, 2 * n) {
    if (c[i] == 'B')
      idxB[a[i]] = i;
    else
      idxW[a[i]] = i;
  }
  rep(i, 2200) {
    rep(j, 2200) { dp[i][j] = -1; }
  }
  cout << f(0, 0) << endl;
  return 0;
}