Language: C++

//written by libra9z
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cmath>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);i++)
#define repit(it,a) for (__typeof(a.begin()) it=a.begin();it!=a.end();++it)
#define RET(x) return writefln(x)*0
#define ITR(...) __VA_ARGS__::iterator
#define all(a) (a).begin(),(a).end()
#define reunique(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define sz(a) (int)a.size()
#define MP(...) make_pair(__VA_ARGS__)
#define X first
#define Y second
#define sqr(x) ((x)*(x))
#define fill_inf(a) memset(a,-19999873,sizeof(a))
#define fill_neginf(a) memset(a,-19999872,sizeof(a))
#define fill_0(a) memset(a,0,sizeof(a))
#define fill_neg1(a) memset(a,-1,sizeof(a))
#define pow_of_2(a) (1<<(a))
#define is_pow_of_2(a) (((a-1)&a)==0)
#define printtime cerr<<"\n\nTime elasped : "<<clock()*1.0/1000<<" seconds.\n\n"
using ui=unsigned;using ll=long long;using ull=unsigned long long;using ld=long double;using pii=pair<int,int>;using
pll=pair<ll,ll>;using hash_type=ull;using vi=vector<int>;using vll=vector<ll>;using vpii=vector<pii>;using vpll=
vector<pll>;using si=set<int>;using sstr=set<string>;using sll=set<ll>;using mii=map<int,int>;using msi=map<string,
int>;using mis=map<int,string>;using mll=map<ll,ll>;using usi=unordered_set<int>;using usstr=unordered_set<string>;
using usll=unordered_set<ll>;using umii=unordered_map<int,int>;using umsi=unordered_map<string,int>;using umis=
unordered_map<int,string>;using umll=unordered_map<ll,ll>;template<typename...T>using void_t=void;template<typename
T>using normal_ptr=T*;template<typename T>using min_queue=priority_queue<T,vector<T>,greater<T>>;template<typename
T>using max_queue=priority_queue<T>;
void do_nothing(){}
template<typename T>void readf(T &a){register int b=1,c=getchar();a=0;for(;!isdigit(c);c=getchar())if(c==45)b*=-1;
for(;isdigit(c);c=getchar())a=(a<<3)+(a<<1)+c-48;a*=b;}template<>void readf(double &a){scanf("%lf",&a);}template<>
void readf(float &a){scanf("%f",&a);}template<>void readf(long double &a){scanf("%lf",&a);}template<typename T>void
writef(T x){if(x<0)putchar('-'),x=-x;char s[24];int n=0;while(x||!n)s[n++]='0'+x%10,x/=10;while(n--)putchar(s[n]);}
template<>void writef(double x){printf("%.9lf",x);}template<>void writef(float x){printf("%.9f",x);}template<>void
writef(long double x){printf("%.9lf",x);}template<>void readf(char &a){a=getchar();while(a<=32)a=getchar();}template<
>void readf(string &a){a="";char c=getchar();while(c<=32)c=getchar();while(c>32)a+=c,c=getchar();}template<>void
readf(char *&a){string x;readf(x);a=(char*)x.c_str();}template<typename X,typename ...T>void readf(X &x,T &...a){
readf(x);readf(a...);}template<>void writef(string x){for(register auto&y:x)putchar(y);}template<>void writef(char x)
{putchar(x);}template<>void writef(const char* x){writef(string(x));}template<>void writef(char* x){writef(string(x))
;}template<typename X,typename ...T>void writef(X x,T ...a){writef(x);putchar(' ');writef(a...);}template<typename T>
T gcd(T u,T v){return __gcd(u,v);}template<typename T>T lcm(T u,T v){return u/gcd(u,v)*v;}template<typename T>T
fastpow(T x,T n,__int64 mod){T r=1;for(;n;x=x*x%mod,n/=2)if(n&1)r=r*x%mod;return r;}bool isprime(const long long &x){
if(x<2)return 0;for(long long i=2;i*i<=x;i++)if(x%i==0)return 0;return 1;}template<typename A,typename B>void
translater(const A &a,B &b){stringstream ss;ss<<a;ss>>b;}template<typename T> maxf(const T&a){return a;}template<
typename T,typename ...U>T maxf(const T&a,const U&...b){return max(a,maxf(b...));}template<typename T> minf(const T&a
){return a;}template<typename T,typename ...U>T minf(const T&a,const U&...b){return min(a,minf(b...));}template<class
T>ostream&operator,(ostream&os,const T&object){os<<" "<<object;return os;}template<typename T>void __debug_assistant
(const char*name,T&&t){cerr<<name<<" = "<<t<<endl;}template<typename T1,typename...T>void __debug_assistant(const
char*names,T1&&t1,T&&...t){const char*comma=strchr(names+1,',');cerr.write(names,comma-names)<<" = "<<t1<<", ";
__debug_assistant(comma+1,t...);}
#define writefln(...) writef(__VA_ARGS__),putchar('\n')
#define Debug(...) __debug_assistant(#__VA_ARGS__,__VA_ARGS__)
const int inf=1e9;
const ll lnf=1e18;
const int mod=1e9+7;
const int Maxn=4005;
char c[Maxn][5];
int pos[2][Maxn];
int dp[Maxn][Maxn];
int cost[Maxn][Maxn][2];
int n,a[Maxn],tp[Maxn];
int small[Maxn][Maxn][2];
int biger[Maxn][Maxn][2];
int main()
{
	ios_base::sync_with_stdio(0);cin.tie();cout.tie();
	cout<<setprecision(15);
	readf(n);
	for (int i=1;i<=2*n;i++)
	{
		readf(c[i],a[i]);
		if (c[i][0]=='B') tp[i]=1;
		pos[tp[i]][a[i]]=i;
	}
	for (int i=1;i<=2*n;i++)
	{
		if (i) for (int j=0;j<=n;j++)
		{
			small[i][j][0]=small[i-1][j][0];
			small[i][j][1]=small[i-1][j][1];
			biger[i][j][0]=biger[i-1][j][0];
			biger[i][j][1]=biger[i-1][j][1];
		}
		for (int j=0;j<a[i];j++) small[i][j][tp[i]]++;
		for (int j=a[i];j<=n;j++) biger[i][j][tp[i]]++;
	}
	for (int i=0;i<=n;i++) for (int j=0;j<=n;j++)
	{
		if (i==n) continue;
		int nwpos=-1;
		if (pos[0][i+1]<=i+j) nwpos=i+j+small[pos[0][i+1]][i][0]+small[pos[0][i+1]][j][1];
		else nwpos=pos[0][i+1]+i-biger[pos[0][i+1]][i][0]+j-biger[pos[0][i+1]][j][1];
		cost[i][j][0]=nwpos-i-j-1;
	}
	for (int i=0;i<=n;i++) for (int j=0;j<=n;j++)
	{
		if (j==n) continue;
		int nwpos=-1;
		if (pos[1][j+1]<=i+j) nwpos=i+j+small[pos[1][j+1]][i][0]+small[pos[1][j+1]][j][1];
		else nwpos=pos[1][j+1]+i-biger[pos[1][j+1]][i][0]+j-biger[pos[1][j+1]][j][1];
		cost[i][j][1]=nwpos-i-j-1;
	}
	for (int i=0;i<=n;i++) for (int j=0;j<=n;j++)
	{
		if (!i && !j) continue;
		dp[i][j]=inf;
		if (i) dp[i][j]=min(dp[i][j],dp[i-1][j]+cost[i-1][j][0]);
		if (j) dp[i][j]=min(dp[i][j],dp[i][j-1]+cost[i][j-1][1]);
	}
	writefln(dp[n][n]);
	return 0;
}

Compiler output:
a.cc: In function 'void readf(T&)':
a.cc:47:51: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 | template<typename T>void readf(T &a){register int b=1,c=getchar();a=0;for(;!isdigit(c);c=getchar())if(c==45)b*=-1;
      |                                                   ^
a.cc:47:55: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 | template<typename T>void readf(T &a){register int b=1,c=getchar();a=0;for(;!isdigit(c);c=getchar())if(c==45)b*=-1;
      |                                                       ^
a.cc: In function 'void writef(T) [with T = std::__cxx11::basic_string<char>]':
a.cc:55:73: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   55 | readf(x);readf(a...);}template<>void writef(string x){for(register auto&y:x)putchar(y);}template<>void writef(char x)
      |                                                                         ^
a.cc: At global scope:
a.cc:59:17: error: '__int64' has not been declared
   59 | fastpow(T x,T n,__int64 mod){T r=1;for(;n;x=x*x%mod,n/=2)if(n&1)r=r*x%mod;return r;}bool isprime(const long long &x){
      |                 ^~~~~~~
a.cc:61:79: error: ISO C++ forbids declaration of 'maxf' with no type [-fpermissive]
   61 | translater(const A &a,B &b){stringstream ss;ss<<a;ss>>b;}template<typename T> maxf(const T&a){return a;}template<
      |                                                                               ^~~~
a.cc:62:104: error: ISO C++ forbids declaration of 'minf' with no type [-fpermissive]
   62 | typename T,typename ...U>T maxf(const T&a,const U&...b){return max(a,maxf(b...));}template<typename T> minf(const T&a
      |                                                                                                        ^~~~
a.cc: In instantiation of 'void readf(T&) [with T = int]':
a.cc:85:7:   required from here
   85 |         readf(n);
      |         ~~~~~^~~
a.cc:47:51: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 | template<typename T>void readf(T &a){register int b=1,c=getchar();a=0;for(;!isdigit(c);c=getchar())if(c==45)b*=-1;
      |                                                   ^
a.cc:47:55: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 | template<typename T>void readf(T &a){register int b=1,c=getchar();a=0;for(;!isdigit(c);c=getchar())if(c==45)b*=-1;
      |                                                       ^
a.cc: In instantiation of 'void readf(T&) [with T = char [5]]':
a.cc:55:6:   required from 'void readf(first&, T& ...) [with first = char [5]; T = {int}]'
   55 | readf(x);readf(a...);}template<>void writef(string x){for(register auto&y:x)putchar(y);}template<>void writef(char x)
      | ~~~~~^~~
a.cc:88:8:   required from here
   88 |                 readf(c[i],a[i]);
      |                 ~~~~~^~~~~~~~~~~
a.cc:47:51: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 | template<typename T>void readf(T &a){register int b=1,c=getchar();a=0;for(;!isdigit(c);c=getchar())if(c==45)b*=-1;
      |                                                   ^
a.cc:47:55: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 | template<typename T>void readf(T &a){register int b=1,c=getchar();a=0;for(;!isdigit(c);c=getchar())if(c==45)b*=-1;
      |                                                       ^
a.cc:47:68: error: incompatible types in assignment of 'int' to 'char [5]'
   47 | template<typename T>void readf(T &a){register int b=1,c=getchar();a=0;for(;!isdigit(c);c=getchar())if(c==45)b*=-1;
      |                                                                   ~^~
a.cc:48:33: error: invalid operands of types 'char [5]' and 'int' to binary 'operator<<'
   48 | for(;isdigit(c);c=getchar())a=(a<<3)+(a<<1)+c-48;a*=b;}template<>void readf(double &a){scanf("%lf",&a);}template<>
      |                               ~~^~~~
a.cc:48:40: error: invalid operands of types 'char [5]' and 'int' to binary 'operator<<'
   48 | for(;isdigit(c);c=getchar())a=(a<<3)+(a<<1)+c-48;a*=b;}template<>void readf(double &a){scanf("%lf",&a);}template<>
      |                                      ~~^~~~
a.cc:48:51: error: invalid operands of types 'char [5]' and 'int' to binary 'operator*'
   48 | for(;isdigit(c);c=getchar())a=(a<<3)+(a<<1)+c-48;a*=b;}template<>void readf(double &a){scanf("%lf",&a);}template<>
      |                                                  ~^~~
a.cc:48:51: note:   in evaluation of 'operator*=(char [5], int)'