Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
#define debug cout << "debug_________" << endl
using namespace std;
const int maxn = 2e3 + 50;
int n;
char c[maxn];
int a[maxn];
int b[maxn];
int w[maxn];
int dp[maxn][maxn];
int count_b, count_w;
int costb[maxn][maxn];
int costw[maxn][maxn];
int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < (n << 1); i++) {
      getchar();
      scanf("%c %d", &c[i], &a[i]);
      a[i]--;
      if (c[i] == 'W')
        w[a[i]] = i;
      else
        b[a[i]] = i;
    }
    for (int i = 0; i < n; i++) {
      count_b = count_w = 0;
      for (int j = 0; j < i; j++) {
        if (b[j] > b[i])
          count_b++;
        if (w[j] > w[i])
          count_w++;
      }
      costw[i][0] = count_w; /// i for w ,j for b
      costb[i][0] = count_b;
    }
    //-----------------------
    costw[0][0] = costb[0][0] = 0;
    for (int i = 0; i < n; i++) {
      count_b = costb[i][0];
      count_w = costw[i][0];
      for (int j = 0; j < n; j++) {
        if (w[j] > b[i])
          count_b++;
        if (b[j] > w[i])
          count_w++;
        costb[i][j + 1] = count_b;
        costw[i][j + 1] = count_w;
      }
    }
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      dp[i][0] = dp[i - 1][0] + costw[i - 1][0];
      dp[0][i] = dp[0][i - 1] + costb[i - 1][0];
    }
    //-------------------
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dp[i][j] =
            min(dp[i - 1][j] + costw[i - 1][j], dp[i][j - 1] + costb[j - 1][i]);
    printf("%d\n", dp[n][n]);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
#define debug cout << "debug_________" << endl
using namespace std;
const int maxn = 4e3 + 50;
int n;
char c[maxn];
int a[maxn];
int b[maxn];
int w[maxn];
int dp[maxn][maxn];
int count_b, count_w;
int costb[maxn][maxn];
int costw[maxn][maxn];
int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < (n << 1); i++) {
      getchar();
      scanf("%c %d", &c[i], &a[i]);
      a[i]--;
      if (c[i] == 'W')
        w[a[i]] = i;
      else
        b[a[i]] = i;
    }
    for (int i = 0; i < n; i++) {
      count_b = count_w = 0;
      for (int j = 0; j < i; j++) {
        if (b[j] > b[i])
          count_b++;
        if (w[j] > w[i])
          count_w++;
      }
      costw[i][0] = count_w; /// i for w ,j for b
      costb[i][0] = count_b;
    }
    //-----------------------
    costw[0][0] = costb[0][0] = 0;
    for (int i = 0; i < n; i++) {
      count_b = costb[i][0];
      count_w = costw[i][0];
      for (int j = 0; j < n; j++) {
        if (w[j] > b[i])
          count_b++;
        if (b[j] > w[i])
          count_w++;
        costb[i][j + 1] = count_b;
        costw[i][j + 1] = count_w;
      }
    }
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      dp[i][0] = dp[i - 1][0] + costw[i - 1][0];
      dp[0][i] = dp[0][i - 1] + costb[i - 1][0];
    }
    //-------------------
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dp[i][j] =
            min(dp[i - 1][j] + costw[i - 1][j], dp[i][j - 1] + costb[j - 1][i]);
    printf("%d\n", dp[n][n]);
  }
  return 0;
}