Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define rep(i, a) for (int i = 0; i < a; i++)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int N;
int A[2009], dp[2009][2009];
char C[2009];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  rep(i, N * 2) cin >> C[i] >> A[i], A[i]--;

  VV black(N, V(N + 1, 0));
  VV white(N, V(N + 1, 0));

  V bnum(N, 0), wnum(N, 0);
  rep(k, N * 2) {
    char c = C[k];
    int num = A[k];
    V wsum(N + 1, 0), bsum(N + 1, 0);
    for (int l = N; l > 0; l--) {
      bsum[l - 1] = bsum[l] + bnum[l - 1];
      wsum[l - 1] = wsum[l] + wnum[l - 1];
    }
    for (int l = N + 1; l > 0; l--) {
      if (c == 'B')
        black[num][l - 1] = wsum[l - 1] + bsum[num + 1];
      if (c == 'W')
        white[num][l - 1] = wsum[num + 1] + bsum[l - 1];
    }
    if (c == 'B')
      bnum[num]++;
    else
      wnum[num]++;
  }

  rep(i, 2009) rep(j, 2009) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N + 1) rep(j, N + 1) {
    if (i < N)
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + black[i][j]);
    if (j < N)
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + white[j][i]);
  }
  cout1(dp[N][N]);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define rep(i, a) for (int i = 0; i < a; i++)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int N;
int A[4009], dp[2009][2009];
char C[4009];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  rep(i, N * 2) cin >> C[i] >> A[i], A[i]--;

  VV black(N, V(N + 1, 0));
  VV white(N, V(N + 1, 0));

  V bnum(N, 0), wnum(N, 0);
  rep(k, N * 2) {
    char c = C[k];
    int num = A[k];
    V wsum(N + 1, 0), bsum(N + 1, 0);
    for (int l = N; l > 0; l--) {
      bsum[l - 1] = bsum[l] + bnum[l - 1];
      wsum[l - 1] = wsum[l] + wnum[l - 1];
    }
    for (int l = N + 1; l > 0; l--) {
      if (c == 'B')
        black[num][l - 1] = wsum[l - 1] + bsum[num + 1];
      if (c == 'W')
        white[num][l - 1] = wsum[num + 1] + bsum[l - 1];
    }
    if (c == 'B')
      bnum[num]++;
    else
      wnum[num]++;
  }

  rep(i, 2009) rep(j, 2009) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N + 1) rep(j, N + 1) {
    if (i < N)
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + black[i][j]);
    if (j < N)
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + white[j][i]);
  }
  cout1(dp[N][N]);
}