Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define mp make_pair
#define fi first
#define se second
#define pb push_back

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

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

  int n, a[2010];
  char c[2010];

  cin >> n;

  REP(i, n * 2)
  cin >> c[i] >> a[i];

  int dp[2010][2010];

  REP(i, n * 2 + 1)
  REP(j, n * 2 + 1)
  dp[i][j] = INT_MAX;

  dp[0][0] = 0;

  int w[2010][2010];
  int b[2010][2010];

  REP(i, n * 2) {
    int cnt = 0;
    int cnt_ = 0;
    bool e[2010] = {false};
    REP(j, i) {
      cnt += (c[j] == c[i] && a[i] < a[j]);
      if (c[j] != c[i])
        e[a[j]] = true, cnt_++;
    }

    int t = cnt + cnt_;
    for (int j = 0; j <= n; j++) {
      if (e[j])
        t--;

      if (c[i] == 'W')
        w[a[i]][j] = t;
      else
        b[a[i]][j] = t;
    }
  }

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + w[i + 1][j]);
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + b[j + 1][i]);
    }
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define mp make_pair
#define fi first
#define se second
#define pb push_back

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

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

  int n, a[4010];
  char c[4010];

  cin >> n;

  REP(i, n * 2)
  cin >> c[i] >> a[i];

  int dp[2010][2010];

  REP(i, n * 2 + 1)
  REP(j, n * 2 + 1)
  dp[i][j] = INT_MAX;

  dp[0][0] = 0;

  int w[2010][2010];
  int b[2010][2010];

  REP(i, n * 2) {
    int cnt = 0;
    int cnt_ = 0;
    bool e[2010] = {false};
    REP(j, i) {
      cnt += (c[j] == c[i] && a[i] < a[j]);
      if (c[j] != c[i])
        e[a[j]] = true, cnt_++;
    }

    int t = cnt + cnt_;
    for (int j = 0; j <= n; j++) {
      if (e[j])
        t--;

      if (c[i] == 'W')
        w[a[i]][j] = t;
      else
        b[a[i]][j] = t;
    }
  }

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + w[i + 1][j]);
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + b[j + 1][i]);
    }
  }

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

  return 0;
}