Problem p03357 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

const int N = (int)3e5;

ll mpow(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll memo = mpow(a, b / 2);
    return memo * memo % MOD;
  } else
    return mpow(a, b - 1) * a % MOD;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
  if (kaijo_memo.size() > n)
    return kaijo_memo[n];
  if (kaijo_memo.size() == 0)
    kaijo_memo.push_back(1);
  while (kaijo_memo.size() <= n)
    kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
                         MOD);
  return kaijo_memo[n];
}
ll nCr(ll n, ll r) {
  if (n < r || r < 0)
    return 0;
  ll ret = 1;
  ret *= kaijo(n);
  ret %= MOD;
  ret *= mpow(kaijo(r), MOD - 2);
  ret %= MOD;
  ret *= mpow(kaijo(n - r), MOD - 2);
  ret %= MOD;
  return ret;
}

// cの数字iよりも右にある, 色jのうちk以下の数字の個数(c==jならばk<i)
ll memo[2][2001][2][2001];

int main(void) {
  int n;
  cin >> n;
  vector<int> W[2];
  rep(i, n + 1) {
    rep(j, 2) { W[j].push_back(0); }
  }
  reps(i, 2 * n) {
    int a;
    char c;
    cin >> c >> a;
    W[(c == 'B' ? 1 : 0)][a] = i;
  }

  rep(c, 2) {
    reps(i, n) {
      rep(j, 2) {
        memo[c][i][j][0] = 0;
        reps(k, n) {
          if (c == j && k >= i) {
            memo[c][i][j][k] = 0;
            break;
          }
          if (W[j][k] > W[c][i]) {
            memo[c][i][j][k] = 1;
          } else {
            memo[c][i][j][k] = 0;
          }
        }
        rep(k, n) {
          memo[c][i][j][k + 1] += memo[c][i][j][k];
          // cerr<<memo[c][i][j][k]<<" ";
        }
        // cerr<<endl;
      }
    }
  }
  cerr << "hoge" << endl;
  // i番目までを決めて, Wの個数がjであるものの最短手数
  vector<vector<ll>> dp;
  dp.push_back(vector<ll>());
  rep(j, n + 1) { dp[0].push_back(LINF / 10); }
  dp[0][0] = 0;

  reps(i, 2 * n) {
    dp.push_back(vector<ll>());
    rep(j, n + 1) {
      ll addW = LINF / 10;
      if (j - 1 >= 0 && i - j >= 0 && i - j <= n) {
        addW = max((ll)0,
                   (W[0][j] - i + memo[0][j][0][j - 1] + memo[0][j][1][i - j]));
      }
      // cerr<<"huga"<<endl;
      ll addB = LINF / 10;
      if (i - j - 1 >= 0 && i - j <= n) {
        addB = max((ll)0, (W[1][i - j] - i + memo[1][i - j][0][j] +
                           memo[1][i - j][1][i - j - 1]));
      }
      // cerr<<"nya"<<endl;
      dp[i].push_back(dp[i - 1][j] + addB);
      if (j - 1 >= 0) {
        chmin(dp[i][j], dp[i - 1][j - 1] + addW);
      }
      chmin(dp[i][j], LINF / 10);
      // cerr<<j<<endl;
    }
  }
  rep(i, 2 * n + 1) {
    rep(j, n + 1) { cerr << dp[i][j] << " "; }
    cerr << endl;
  }
  cout << dp[2 * n][n] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

const int N = (int)3e5;

ll mpow(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll memo = mpow(a, b / 2);
    return memo * memo % MOD;
  } else
    return mpow(a, b - 1) * a % MOD;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
  if (kaijo_memo.size() > n)
    return kaijo_memo[n];
  if (kaijo_memo.size() == 0)
    kaijo_memo.push_back(1);
  while (kaijo_memo.size() <= n)
    kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
                         MOD);
  return kaijo_memo[n];
}
ll nCr(ll n, ll r) {
  if (n < r || r < 0)
    return 0;
  ll ret = 1;
  ret *= kaijo(n);
  ret %= MOD;
  ret *= mpow(kaijo(r), MOD - 2);
  ret %= MOD;
  ret *= mpow(kaijo(n - r), MOD - 2);
  ret %= MOD;
  return ret;
}

// cの数字iよりも右にある, 色jのうちk以下の数字の個数(c==jならばk<i)
ll memo[2][2001][2][2001];

int main(void) {
  int n;
  cin >> n;
  vector<int> W[2];
  rep(i, n + 1) {
    rep(j, 2) { W[j].push_back(0); }
  }
  reps(i, 2 * n) {
    int a;
    char c;
    cin >> c >> a;
    W[(c == 'B' ? 1 : 0)][a] = i;
  }

  rep(c, 2) {
    reps(i, n) {
      rep(j, 2) {
        memo[c][i][j][0] = 0;
        reps(k, n) {
          if (c == j && k >= i) {
            memo[c][i][j][k] = 0;
            break;
          }
          if (W[j][k] > W[c][i]) {
            memo[c][i][j][k] = 1;
          } else {
            memo[c][i][j][k] = 0;
          }
        }
        rep(k, n) {
          memo[c][i][j][k + 1] += memo[c][i][j][k];
          // cerr<<memo[c][i][j][k]<<" ";
        }
        // cerr<<endl;
      }
    }
  }
  cerr << "hoge" << endl;
  // i番目までを決めて, Wの個数がjであるものの最短手数
  vector<vector<ll>> dp;
  dp.push_back(vector<ll>());
  rep(j, n + 1) { dp[0].push_back(LINF / 10); }
  dp[0][0] = 0;

  reps(i, 2 * n) {
    dp.push_back(vector<ll>());
    rep(j, n + 1) {
      ll addW = LINF / 10;
      if (j - 1 >= 0 && i - j >= 0 && i - j <= n) {
        addW = max((ll)0,
                   (W[0][j] - i + memo[0][j][0][j - 1] + memo[0][j][1][i - j]));
      }
      // cerr<<"huga"<<endl;
      ll addB = LINF / 10;
      if (i - j - 1 >= 0 && i - j <= n) {
        addB = max((ll)0, (W[1][i - j] - i + memo[1][i - j][0][j] +
                           memo[1][i - j][1][i - j - 1]));
      }
      // cerr<<"nya"<<endl;
      dp[i].push_back(dp[i - 1][j] + addB);
      if (j - 1 >= 0) {
        chmin(dp[i][j], dp[i - 1][j - 1] + addW);
      }
      chmin(dp[i][j], LINF / 10);
      // cerr<<j<<endl;
    }
  }
  /*rep(i,2*n+1){
    rep(j,n+1){
      cerr<<dp[i][j]<<" ";
    }
    cerr<<endl;
  }*/
  cout << dp[2 * n][n] << endl;
  return 0;
}