Problem:
Score : 600 points

Problem Statement

There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball.
The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i.
c_i = W represents the ball is white; c_i = B represents the ball is black.

Takahashi the human wants to achieve the following objective:

For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.

For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.

In order to achieve this, he can perform the following operation:

Swap two adjacent balls.

Find the minimum number of operations required to achieve the objective.

Constraints

1 ≤ N ≤ 2000

1 ≤ a_i ≤ N

c_i = W or c_i = B.

If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).

Input

Input is given from Standard Input in the following format:

N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}

Output

Print the minimum number of operations required to achieve the objective.

Sample Input 1

3
B 1
W 2
B 3
W 1
W 3
B 2

Sample Output 1

4

The objective can be achieved in four operations, for example, as follows:

Swap the black 3 and white 1.

Swap the white 1 and white 2.

Swap the black 3 and white 3.

Swap the black 3 and black 2.

Sample Input 2

4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1

Sample Output 2

18

Sample Input 3

9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7

Sample Output 3

41

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
//#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
//#pragma GCC target("avx2")  //Enable AVX
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

#define all(a) begin(a),end(a)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define sz(x) (int)(x).size()
#define F first
#define S second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
 
const int mod=1e9+7;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
int mul(int a,int b){
  return ((a)*1ll*(b))%mod;
}
 
void add(int &a,int b){
  a+=b;
  if(a>=mod)a-=mod;
}
 
int sub(int a,int b){
  a-=b;
  if(a<0){
    a+=mod;
  }
  return a;
}
 
int powz(int a,int b){
  int res=1;
  while(b){
    if(b&1){
      res=mul(res,a);
    }
    b/=2;
    a=mul(a,a);
  }
  return res;
}
 
template <typename A, typename B>
istream& operator>>(istream& input,pair<A,B>& x) {
  input>>x.F>>x.S;
  return input;
}
 
template <typename A>
istream& operator>>(istream& input,vector<A>& x) {
  for(auto& i:x)
    input>>i;
  return input;
}
 
template<typename A>
ostream& operator<<(ostream& output,vector<A>& x) {
  for(auto& i:x)
    output<<i<<' ';
  return output;
}

void getvec(vector<pair<int,int>>&a){
  for(int i=0;i<a.size();i++){
    cin>>a[i].F;
    a[i].S=i;
  }
}
 
const int N=2005;

vector<int>adj,adj2;
ordered_set st[N];
int dp[2][N];

void solve(){
  int n;
  cin>>n;
  vector<int>a(2*n);
  string s;
  adj.resize(n);
  adj2.resize(n);
  for(int i=0;i<2*n;i++){
    char z;
    cin>>z;
    s+=z;
    cin>>a[i];
    if(z=='B'){
      adj[a[i]]=i;
    }
    else{
      adj2[a[i]]=i;
    }
  }
  for(int j=0;j<=n;j++){
    dp[0][j]=dp[1][j]=1e9;
    for(int i=0;i<2*n;i++){
      st[j].insert(i); 
    }
    for(int i=1;i<=j;i++){
      st[j].erase(adj[i]);
    }
  }
  dp[0][0]=0;
  for(int i=0;i<2*n;i++){
    for(int j=0;j<=min(n,i);j++){
      int gt=st[j].order_of_key(adj[j+1]);
      debug(i,j,gt);
      dp[1][j+1]=min(dp[1][j+1],dp[0][j]+gt);
    }
    for(int j=0;j<=n;j++){
      if(i-j<0){
	continue;
      }
      if(i-j>n){
	continue;
      }
      int gt=st[j].order_of_key(adj2[i-j+1]);
      dp[1][j]=min(dp[1][j],dp[0][j]+gt);
    }
    for(int j=0;j<=n;j++){
      dp[0][j]=dp[1][j];
      dp[1][j]=1e9;
    }
    for(int j=0;j<=n;j++){
      if(i-j+1<=n&&i-j+1>0){
	st[j].erase(adj2[i-j+1]);
      }
    }
  }
  cout<<dp[0][n];
}
 
signed main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc=1;
  //cin>>tc;
  for(int _=0;_<tc;_++){
    // cout<<"Case #"<<_+1<<": ";
    solve();
    if(_!=tc-1){
      cout<<'\n';
    }
  }
}