Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
template <typename T> class BIT {
  int sz;

public:
  std::vector<T> node;
  BIT(int sz) : sz(sz + 1), node(std::vector<T>(sz + 1, 0)) {}
  void add(int idx, T val) {
    ++idx;
    assert(0 < idx && idx < sz);
    while (idx < sz) {
      node[idx] += val;
      idx += idx & -idx;
    }
  }
  T sum(int idx) {
    ++idx;
    assert(0 < idx && idx < sz);
    T res = 0;
    while (idx > 0) {
      res += node[idx];
      idx -= idx & -idx;
    }
    return res;
  }
};
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < ((int)(v.size())); ++i) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<pair<char, int>> v;
  for (int i = 0; i < (2 * n); ++i) {
    char c;
    int a;
    cin >> c >> a;
    a--;
    v.push_back({c, a});
  }
  vvi black(n, vi(n + 1, 0)), white(n, vi(n + 1, 0));
  vi bnum(n, 0), wnum(n, 0);
  for (int k = 0; k < (2 * n); ++k) {
    char c = v[k].first;
    int num = v[k].second;
    vi wsum(n + 1, 0), bsum(n + 1, 0);
    for (int l = (int)(n); l >= (int)(1); --l) {
      bsum[l - 1] = bsum[l] + bnum[l - 1];
      wsum[l - 1] = wsum[l] + wnum[l - 1];
    }
    for (int l = (int)(n + 1); l >= (int)(1); --l) {
      if (c == 'B')
        black[num][l - 1] = wsum[l - 1] + bsum[num + 1];
      else
        white[num][l - 1] = wsum[num + 1] + bsum[l - 1];
    }
    if (c == 'B')
      bnum[num]++;
    else
      wnum[num]++;
  }
  vvi dp(2 * n + 1, vi(n + 1, INF));
  dp[0][0] = 0;
  for (int i = 0; i < (2 * n); ++i) {
    for (int j = 0; j < (i + 1); ++j) {
      if (dp[i][j] == INF)
        continue;
      int nxtw = j;
      int nxtb = i - j;
      if (nxtw < n)
        chmin(dp[i + 1][j + 1], dp[i][j] + black[nxtw][nxtb]);
      if (nxtb < n)
        chmin(dp[i + 1][j], dp[i][j] + white[nxtb][nxtw]);
    }
  }
  cout << dp[2 * n][n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
template <typename T> class BIT {
  int sz;

public:
  std::vector<T> node;
  BIT(int sz) : sz(sz + 1), node(std::vector<T>(sz + 1, 0)) {}
  void add(int idx, T val) {
    ++idx;
    assert(0 < idx && idx < sz);
    while (idx < sz) {
      node[idx] += val;
      idx += idx & -idx;
    }
  }
  T sum(int idx) {
    ++idx;
    assert(0 < idx && idx < sz);
    T res = 0;
    while (idx > 0) {
      res += node[idx];
      idx -= idx & -idx;
    }
    return res;
  }
};
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < ((int)(v.size())); ++i) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<pair<char, int>> v;
  for (int i = 0; i < (2 * n); ++i) {
    char c;
    int a;
    cin >> c >> a;
    a--;
    v.push_back({c, a});
  }
  vvi black(n, vi(n + 1, 0)), white(n, vi(n + 1, 0));
  vi bnum(n, 0), wnum(n, 0);
  for (int k = 0; k < (2 * n); ++k) {
    char c = v[k].first;
    int num = v[k].second;
    vi wsum(n + 1, 0), bsum(n + 1, 0);
    for (int l = (int)(n); l >= (int)(1); --l) {
      bsum[l - 1] = bsum[l] + bnum[l - 1];
      wsum[l - 1] = wsum[l] + wnum[l - 1];
    }
    for (int l = (int)(n + 1); l >= (int)(1); --l) {
      if (c == 'B')
        black[num][l - 1] = wsum[l - 1] + bsum[num + 1];
      else
        white[num][l - 1] = wsum[num + 1] + bsum[l - 1];
    }
    if (c == 'B')
      bnum[num]++;
    else
      wnum[num]++;
  }
  vvi dp(2 * n + 1, vi(n + 1, INF));
  dp[0][0] = 0;
  for (int i = 0; i < (2 * n); ++i) {
    for (int j = 0; j < (min(n + 1, i + 1)); ++j) {
      if (dp[i][j] == INF)
        continue;
      int nxtw = j;
      int nxtb = i - j;
      if (nxtw < n)
        chmin(dp[i + 1][j + 1], dp[i][j] + black[nxtw][nxtb]);
      if (nxtb < n)
        chmin(dp[i + 1][j], dp[i][j] + white[nxtb][nxtw]);
    }
  }
  cout << dp[2 * n][n] << endl;
}