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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		int[][] W = new int[NN][];
		for(int i=0;i<NN;i++){
			W[i] = new int[N];
		}
		int[][] B = new int[NN][];
		for(int i=0;i<NN;i++){
			B[i] = new int[N];
		}
		for(int i=0;i<NN;i++){
			if(C[i] == 'W'){
				W[i][A[i]] = 1;
			} else {
				B[i][A[i]] = 1;
			}
			if(i < NN - 1){
				for(int j=0;j<N;j++){
					W[i+1][j] = W[i][j];
					B[i+1][j] = B[i][j];
				}
			}
		}
		
		int[][] SW = new int[NN][];
		int[][] SB = new int[NN][];
		for(int i=0;i<NN;i++){
			SW[i] = new int[N + 1];
			for(int j=0;j<N;j++){
				SW[i][j+1] = SW[i][j] + W[i][j];
			}
			SB[i] = new int[N + 1];
			for(int j=0;j<N;j++){
				SB[i][j+1] = SB[i][j] + B[i][j];
			}
		}
		
		int[] Binv = new int[N];
		int[] Winv = new int[N];
		for(int i=0;i<NN;i++){
			if(C[i] == 'B'){
				Binv[A[i]] = i;
			} else {
				Winv[A[i]] = i;
			}
		}
		
		
		
		int M = N + 1;
		int MM = M * M;
		long[] min = new long[MM];
		long Inf = (long) 1e18;
		for(int i=0;i<MM;i++) min[i] = Inf;
		bool[] used = new bool[MM];
		
		var SH = new SkewHeap<Pair>();
		min[N * M + N] = 0;
		SH.Push(new Pair(N * M + N, 0));
		
		while(SH.Count > 0){
			var p = SH.Top; SH.Pop();
			if(used[p.Pos]) continue;
			used[p.Pos] = true;
			
			int bb = p.Pos / M;
			int ww = p.Pos % M;
			long cost = p.Cost;
			
			int done = N - bb + N - ww;
			
			// choose next w
			if(ww > 0){
				int ii = Winv[ww - 1];
				int lw = SW[ii][ww - 1];
				int lb = SB[ii][bb];
				int r = NN - done - lw - lb - 1;
				
				int npos = bb * M + (ww - 1);
				if(min[npos] > cost + r){
					min[npos] = cost + r;
					SH.Push(new Pair(npos, cost + r));
				}
			}
			
			// choose next b
			if(bb > 0){
				int ii = Binv[bb - 1];
				int lw = SW[ii][ww];
				int lb = SB[ii][bb - 1];
				int r = NN - done - lw - lb - 1;
				
				int npos = (bb - 1) * M + ww;
				if(min[npos] > cost + r){
					min[npos] = cost + r;
					SH.Push(new Pair(npos, cost + r));
				}
			}
		}
		
		Console.WriteLine(min[0]);
		
		
	}
	int NN;
	
	int N;
	char[] C;
	int[] A;
	public Sol(){
		N = ri();
		NN = N * 2;
		C = new char[NN];
		A = new int[NN];
		for(int i=0;i<NN;i++){
			var ss = rsa();
			C[i] = ss[0][0];
			A[i] = int.Parse(ss[1]) - 1;
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


class Pair : IComparable<Pair> {
	public int Pos;
	public long Cost;
	public Pair(int p, long c) {
		Pos = p; Cost = c;
	}
	public int CompareTo(Pair t) {
		//return this.Cost > t.Cost ? -1 : this.Cost < t.Cost ? 1 : 0;
		return this.Cost > t.Cost ? 1 : this.Cost < t.Cost ? -1 : 0;
	}
}


class SkewHeap<T> where T:IComparable<T>{
	public int Count{
		get{return cnt;}
		private set{cnt=value;}
	}
	
	public SkewHeap(){
		root=null;
		this.Count=0;
	}
	
	public void Push(T v){
		NodeSH<T> p=new NodeSH<T>(v);
		root=NodeSH<T>.Meld(root,p);
		this.Count++;
	}
	
	public void Pop(){
		if(root==null)return;
		root=NodeSH<T>.Meld(root.L,root.R);
		this.Count--;
	}
	
	public T Top{
		get{return root.Val;}
	}
	
	int cnt;
	NodeSH<T> root;
	
	class NodeSH<S> where S : IComparable<S> {
		public NodeSH<S> L, R;
		public S Val;

		public NodeSH(S v){
			Val = v;
			L = null; R = null;
		}
		public static NodeSH<S> Meld(NodeSH<S> a, NodeSH<S> b){
			if(a == null)return b;
			if (b == null) return a;
			if (a.Val.CompareTo(b.Val) > 0) swap(ref a, ref b);
			a.R = Meld(a.R, b);
			swap(ref a.L, ref a.R);
			return a;
		}

		static void swap<U>(ref U x, ref U y) {
			U t = x; x = y; y = t;
		}
	}
}