Problem p03357 (C++)

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

typedef long long ll;
typedef pair<ll, ll> P;

#define fr first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)

const int INF = 1000000000;

int main() {
  static int n;
  static char c[2002];
  static int a[2002];
  scanf("%d", &n);
  for (int i = 1; i <= 2 * n; i++) {
    scanf("\n%c %d", &c[i], &a[i]);
  }

  static int dp[2002][2002];
  static int d[2][2002][2002];
  for (int i = 0; i < 2002; i++) {
    for (int j = 0; j < 2002; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    set<int> s;
    for (int k = 1; k <= 2 * n; k++) {
      if (c[k] == 'W' && a[k] == i)
        break;
      if (c[k] == 'W') {
        if (a[k] > i)
          dp[0][0]++;
      } else {
        s.insert(a[k]);
      }
    }
    for (int j = 0; j <= n; j++) {
      s.erase(j);
      d[0][i][j] = s.size();
    }
  }
  for (int i = 1; i <= n; i++) {
    set<int> s;
    for (int k = 1; k <= 2 * n; k++) {
      if (c[k] == 'B' && a[k] == i)
        break;
      if (c[k] == 'B') {
        if (a[k] > i)
          dp[0][0]++;
      } else {
        s.insert(a[k]);
      }
    }
    for (int j = 0; j <= n; j++) {
      s.erase(j);
      d[1][j][i] = s.size();
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i < n)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + d[0][i + 1][j]);
      if (j < n)
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + d[1][i][j + 1]);
    }
  }

  /*rep(i,n+1){
          rep(j,n+1){
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }*/

  cout << dp[n][n] << endl;
}


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

typedef long long ll;
typedef pair<ll, ll> P;

#define fr first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)

const int INF = 1000000000;

int main() {
  static int n;
  static char c[4002];
  static int a[4002];
  scanf("%d", &n);
  for (int i = 1; i <= 2 * n; i++) {
    scanf("\n%c %d", &c[i], &a[i]);
  }

  static int dp[2002][2002];
  static int d[2][2002][2002];
  for (int i = 0; i < 2002; i++) {
    for (int j = 0; j < 2002; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    set<int> s;
    for (int k = 1; k <= 2 * n; k++) {
      if (c[k] == 'W' && a[k] == i)
        break;
      if (c[k] == 'W') {
        if (a[k] > i)
          dp[0][0]++;
      } else {
        s.insert(a[k]);
      }
    }
    for (int j = 0; j <= n; j++) {
      s.erase(j);
      d[0][i][j] = s.size();
    }
  }
  for (int i = 1; i <= n; i++) {
    set<int> s;
    for (int k = 1; k <= 2 * n; k++) {
      if (c[k] == 'B' && a[k] == i)
        break;
      if (c[k] == 'B') {
        if (a[k] > i)
          dp[0][0]++;
      } else {
        s.insert(a[k]);
      }
    }
    for (int j = 0; j <= n; j++) {
      s.erase(j);
      d[1][j][i] = s.size();
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i < n)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + d[0][i + 1][j]);
      if (j < n)
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + d[1][i][j + 1]);
    }
  }

  /*rep(i,n+1){
          rep(j,n+1){
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }*/

  cout << dp[n][n] << endl;
}