Problem p03357 (C++)

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

using namespace std;

namespace {

#define VEVE(i, a, b) for (ll i = a, __##i = b; i < __##i; ++i)
#define DYDY(i, a, b) for (ll i = a, __##i = b; i > __##i; --i)
#define RARA(x, seq) for (auto &x : seq)
#define SIZE(x) ((ll)(x.size()))
#define ALL(x) x.begin(), x.end()

typedef int ll;
typedef double dd;
const ll MAX = 1024;
// cost[c][i][j] = # of balls of opposite color of c that have
// num <= j and appear after where ball (i,c) is originally positioned
ll pos[2][MAX], cost[2][MAX][MAX];
ll dp[MAX][MAX];

void Solve(ll) {
  ll n;
  if (not(cin >> n))
    return;
  vector<ll> seq[2];
  VEVE(i, 0, 2 * n) {
    char col;
    ll num;
    cin >> skipws >> col >> num;
    seq[col == 'B'].push_back(num);
    pos[col == 'B'][num] = i;
  }
  memset(cost, 0, sizeof(cost));
  VEVE(c, 0, 2) {
    VEVE(i, 1, n + 1) {
      VEVE(j, 1, n + 1) {
        cost[c][i][j] = cost[c][i][j - 1] + (pos[c ^ 1][j] > pos[c][i]);
      }
    }
  }
  memset(dp, 0x3F, sizeof(dp));
  dp[0][0] = 0;
  VEVE(i, 0, n + 1) {
    VEVE(j, 0, n + 1) {
      if (i > 0) {
        // white ball is last
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + cost[0][i][j]);
      }
      if (j > 0) {
        // black ball is last
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + cost[1][j][i]);
      }
    }
  }
  ll res = dp[n][n];
  //  DEBUG(res);
  VEVE(c, 0, 2) VEVE(i, 0, n) VEVE(j, i + 1, n) res += seq[c][i] > seq[c][j];
  cout << res << endl;
}

void Init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}
} // namespace

int32_t main() {
#ifdef AZN
  freopen("input.txt", "r", stdin);
#endif
  Init();
  ll tests = 11;
  VEVE(test, 1, tests + 1) Solve(test);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

namespace {

#define VEVE(i, a, b) for (ll i = a, __##i = b; i < __##i; ++i)
#define DYDY(i, a, b) for (ll i = a, __##i = b; i > __##i; --i)
#define RARA(x, seq) for (auto &x : seq)
#define SIZE(x) ((ll)(x.size()))
#define ALL(x) x.begin(), x.end()

typedef int ll;
typedef double dd;
const ll MAX = 2048;
// cost[c][i][j] = # of balls of opposite color of c that have
// num <= j and appear after where ball (i,c) is originally positioned
ll pos[2][MAX], cost[2][MAX][MAX];
ll dp[MAX][MAX];

void Solve(ll) {
  ll n;
  if (not(cin >> n))
    return;
  vector<ll> seq[2];
  VEVE(i, 0, 2 * n) {
    char col;
    ll num;
    cin >> skipws >> col >> num;
    seq[col == 'B'].push_back(num);
    pos[col == 'B'][num] = i;
  }
  memset(cost, 0, sizeof(cost));
  VEVE(c, 0, 2) {
    VEVE(i, 1, n + 1) {
      VEVE(j, 1, n + 1) {
        cost[c][i][j] = cost[c][i][j - 1] + (pos[c ^ 1][j] > pos[c][i]);
      }
    }
  }
  memset(dp, 0x3F, sizeof(dp));
  dp[0][0] = 0;
  VEVE(i, 0, n + 1) {
    VEVE(j, 0, n + 1) {
      if (i > 0) {
        // white ball is last
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + cost[0][i][j]);
      }
      if (j > 0) {
        // black ball is last
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + cost[1][j][i]);
      }
    }
  }
  ll res = dp[n][n];
  //  DEBUG(res);
  VEVE(c, 0, 2) VEVE(i, 0, n) VEVE(j, i + 1, n) res += seq[c][i] > seq[c][j];
  cout << res << endl;
}

void Init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}
} // namespace

int32_t main() {
#ifdef AZN
  freopen("input.txt", "r", stdin);
#endif
  Init();
  ll tests = 11;
  VEVE(test, 1, tests + 1) Solve(test);
  return 0;
}