macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut $ iter = s . split_whitespace ( ) ; input_inner ! { $ iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , { chars : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
#[inline]
fn rmb(k: usize) -> usize {
    (k as isize & -(k as isize)) as usize
}
pub trait Monoid {
    type T: Clone + PartialEq;
    fn mempty(&self) -> Self::T;
    fn mappend(&self, x: &Self::T, y: &Self::T) -> Self::T;
}
pub trait Group: Monoid {
    fn invert(&self, x: &Self::T) -> Self::T;
}
#[derive(Clone, Debug)]
pub struct BinaryIndexedTree<M: Monoid> {
    bit: Vec<M::T>,
    m: M,
}
impl<M: Monoid> BinaryIndexedTree<M> {
    pub fn new(n: usize, m: M) -> BinaryIndexedTree<M> {
        let bit = vec![m.mempty(); n + 1];
        BinaryIndexedTree { bit: bit, m: m }
    }
    #[doc = " 1-indexed"]
    pub fn query(&self, k: usize) -> M::T {
        let mut res = self.m.mempty();
        let mut k = k;
        while k > 0 {
            res = self.m.mappend(&res, &self.bit[k]);
            k -= rmb(k);
        }
        res
    }
    #[doc = " 1-indexed"]
    pub fn update(&mut self, k: usize, x: M::T) {
        let mut k = k;
        while k < self.bit.len() {
            self.bit[k] = self.m.mappend(&self.bit[k], &x);
            k += rmb(k);
        }
    }
}
impl<G: Group> BinaryIndexedTree<G> {
    #[doc = " 0-indexed [l, r)"]
    pub fn query_section(&self, l: usize, r: usize) -> G::T {
        self.m
            .mappend(&self.m.invert(&self.query(l)), &self.query(r))
    }
    #[doc = " 1-indexed"]
    pub fn get_val(&self, k: usize) -> G::T {
        self.query_section(k - 1, k)
    }
    #[doc = " 1-indexed"]
    pub fn set_val(&mut self, k: usize, x: G::T) {
        let y = self.m.invert(&self.get_val(k));
        let z = self.m.mappend(&y, &x);
        self.update(k, z);
    }
}
impl<M: Monoid> BinaryIndexedTree<M>
where
    M::T: Ord,
{
    #[doc = " 0-indexed"]
    pub fn lower_bound(&self, x: M::T) -> (usize, M::T) {
        let n = self.bit.len() - 1;
        let mut acc = self.m.mempty();
        let mut pos = 0;
        let mut k = 1 << format!("{:b}", n).len();
        while k > 0 {
            if k + pos <= n && self.m.mappend(&acc, &self.bit[k + pos]) < x {
                pos += k;
                acc = self.m.mappend(&acc, &self.bit[pos]);
            }
            k >>= 1;
        }
        (pos, acc)
    }
}
#[derive(Clone, Debug)]
pub struct AddMonoid<T: Copy + std::ops::Add<Output = T>> {
    id: T,
}
impl<T: Copy + std::ops::Add<Output = T>> AddMonoid<T> {
    pub fn new(id: T) -> AddMonoid<T> {
        AddMonoid { id: id }
    }
}
impl<T: Copy + std::ops::Add<Output = T> + PartialEq> Monoid for AddMonoid<T> {
    type T = T;
    fn mempty(&self) -> T {
        self.id.clone()
    }
    fn mappend(&self, x: &T, y: &T) -> T {
        *x + *y
    }
}
#[derive(Clone, Debug)]
pub struct AddGroup<T: Copy + std::ops::Add<Output = T> + std::ops::Neg<Output = T>> {
    id: T,
}
impl<T: Copy + std::ops::Add<Output = T> + std::ops::Neg<Output = T>> AddGroup<T> {
    pub fn new(id: T) -> AddGroup<T> {
        AddGroup { id: id }
    }
}
impl<T: Copy + std::ops::Add<Output = T> + std::ops::Neg<Output = T> + PartialEq> Monoid
    for AddGroup<T>
{
    type T = T;
    fn mempty(&self) -> T {
        self.id.clone()
    }
    fn mappend(&self, x: &T, y: &T) -> T {
        *x + *y
    }
}
impl<T: Copy + std::ops::Add<Output = T> + std::ops::Neg<Output = T> + PartialEq> Group
    for AddGroup<T>
{
    fn invert(&self, x: &T) -> T {
        -*x
    }
}
use std::cmp::min;
fn main() {
    input! { n, ca: [(char, usize1); 2*n] };
    let mut bi = vec![0; n];
    let mut wi = vec![0; n];
    for i in 0..2 * n {
        if ca[i].0 == 'B' {
            bi[ca[i].1] = i + 1;
        } else {
            wi[ca[i].1] = i + 1;
        }
    }
    let mut dp = vec![vec![std::i64::MAX; n + 1]; n + 1];
    dp[0][0] = 0;
    let mut bit = BinaryIndexedTree::new(n * 2, AddGroup::new(0i64));
    for i in 0..n + 1 {
        for j in 0..n + 1 {
            let ni = i + 1;
            let nj = j + 1;
            if nj <= n {
                let k = wi[j];
                let d = i as i64 + j as i64 - bit.query(k);
                dp[i][nj] = min(dp[i][nj], dp[i][j] + d);
            }
            if ni <= n {
                let k = bi[i];
                let d = i as i64 + j as i64 - bit.query(k);
                dp[ni][j] = min(dp[ni][j], dp[i][j] + d);
            }
            if j < n {
                bit.update(wi[j], 1);
            }
        }
        if i < n {
            bit.update(bi[i], 1);
        }
        for j in 0..n {
            bit.set_val(wi[j], 0);
        }
    }
    println!("{}", dp[n][n]);
}


Measured memory_bytes: 45308