Buggy python submission (verdict: Time Limit Exceeded):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

def cumsum(it):
    """
    累積和
    :param collections.Iterable it:
    """
    cs = 0
    ret = []
    for v in it:
        cs += v
        ret.append(cs)
    return ret


# 解説
N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    C.append(c)
    A.append(int(a))

pos_w = [IINF] * (N + 1)
pos_b = [IINF] * (N + 1)
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# count_b[k][i]: i より左に、k 以下の黒が何個あるか
# count_w[k][i]: i より左に、k 以下の白が何個あるか
bc = [0] * (N * 2 + 1)
wc = [0] * (N * 2 + 1)
count_b = [[0] * (N * 2 + 1)]
count_w = [[0] * (N * 2 + 1)]
for k in range(1, N + 1):
    wc[pos_w[k] + 1] = 1
    bc[pos_b[k] + 1] = 1
    count_b.append(cumsum(bc))
    count_w.append(cumsum(wc))

# dp[b][w]: B を b 回、W を w 回使ったときの転倒数の最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
for i in range(N * 2):
    b = i
    w = 0
    if b > N:
        w = b - N
        b -= w

    while w <= min(i, N):
        if b < N:
            # b + 1 を使う。
            # 位置の写像が与えられたときのスワップの回数だから転倒数
            # もともといた位置より右にあったやつで、既に使ってるのが何個か
            orig_pos = pos_b[b + 1]
            inv = i - count_b[b + 1][orig_pos] - count_w[w][orig_pos]
            dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)
        if w < N:
            orig_pos = pos_w[w + 1]
            inv = i - count_b[b][orig_pos] - count_w[w + 1][orig_pos]
            dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)
        b -= 1
        w += 1
print(dp[N][N])

# import numpy as np
#
# print(np.array(dp))


Fixed submission:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

def cumsum(it):
    """
    累積和
    :param collections.Iterable it:
    """
    cs = 0
    ret = []
    for v in it:
        cs += v
        ret.append(cs)
    return ret


# 解説
N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    C.append(c)
    A.append(int(a))

pos_w = [IINF] * (N + 1)
pos_b = [IINF] * (N + 1)
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# count_b[k][i]: i より左に、k 以下の黒が何個あるか
# count_w[k][i]: i より左に、k 以下の白が何個あるか
bc = [0] * (N * 2 + 1)
wc = [0] * (N * 2 + 1)
count_b = [[0] * (N * 2 + 1)]
count_w = [[0] * (N * 2 + 1)]
for k in range(1, N + 1):
    wc[pos_w[k] + 1] = 1
    bc[pos_b[k] + 1] = 1
    count_b.append(cumsum(bc))
    count_w.append(cumsum(wc))

# dp[b][w]: B を b 回、W を w 回使ったときの転倒数の最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
# for i in range(N * 2):
#     b = i
#     w = 0
#     if b > N:
#         w = b - N
#         b -= w
#
#     while w <= min(i, N):
#         if b < N:
#             # b + 1 を使う。
#             # 位置の写像が与えられたときのスワップの回数だから転倒数
#             # もともといた位置より右にあったやつで、既に使ってるのが何個か
#             orig_pos = pos_b[b + 1]
#             inv = i - count_b[b + 1][orig_pos] - count_w[w][orig_pos]
#             dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)
#         if w < N:
#             orig_pos = pos_w[w + 1]
#             inv = i - count_b[b][orig_pos] - count_w[w + 1][orig_pos]
#             dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)
#         b -= 1
#         w += 1


# もらう
for b, w in itertools.product(range(N + 1), repeat=2):
    if b > 0:
        # 最後に b を使う
        orig_pos = pos_b[b]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b - 1][w] + inv)
    if w > 0:
        # 最後に w を使う
        orig_pos = pos_w[w]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b][w - 1] + inv)
print(dp[N][N])

# import numpy as np
#
# print(np.array(dp))

Buggy python submission (verdict: Time Limit Exceeded):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

class BinaryIndexedTree:
    # http://hos.ac/slides/20140319_bit.pdf
    def __init__(self, size):
        """
        :param int size:
        """
        self._bit = [0] * size
        self._size = size

    def add(self, i, w):
        """
        i 番目に w を加える
        :param int i:
        :param int w:
        """
        x = i + 1
        while x <= self._size:
            self._bit[x - 1] += w
            x += x & -x

    def sum(self, i):
        """
        [0, i) の合計
        :param int i:
        """
        ret = 0
        while i > 0:
            ret += self._bit[i - 1]
            i -= i & -i
        return ret

    def __len__(self):
        return self._size

    def copy(self):
        ret = BinaryIndexedTree(0)
        ret._bit = self._bit[:]
        ret._size = self._size
        return ret


def cumsum(it):
    """
    累積和
    :param collections.Iterable it:
    """
    cs = 0
    ret = []
    for v in it:
        cs += v
        ret.append(cs)
    return ret


N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    a = int(a) - 1
    C.append(c)
    A.append(a)

pos_w = [-1] * N
pos_b = [-1] * N
ca = []
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# dp[b][w]: 今までに B を b 個、W を w 個使ったときの答えの最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
bits = [[None] * (N + 1) for _ in range(N + 1)]
bits[0][0] = BinaryIndexedTree(size=N * 2)
for i, (c, a) in enumerate(list(zip(C, A))):
    b = max(0, i - N)
    w = min(i, N)
    while w >= max(0, i - N):
        #: :type: BinaryIndexedTree
        bit = bits[b][w]
        if b < N:
            inv = i - bit.sum(pos_b[b])
            dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)
        if w < N:
            inv = i - bit.sum(pos_w[w])
            dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)

        c = 0
        c += b < N and not bits[b + 1][w]
        c += w < N and not bits[b][w + 1]
        if b < N and not bits[b + 1][w]:
            bits[b + 1][w] = bit.copy() if c == 2 else bit
            bits[b + 1][w].add(pos_b[b], 1)
            ref = False
        if w < N and not bits[b][w + 1]:
            bits[b][w + 1] = bit
            bits[b][w + 1].add(pos_w[w], 1)
            ref = False
        b += 1
        w -= 1
    # import numpy as np
    # print(np.array(dp))
    # print()
print(dp[N][N])


Fixed submission:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

def cumsum(it):
    """
    累積和
    :param collections.Iterable it:
    """
    cs = 0
    ret = []
    for v in it:
        cs += v
        ret.append(cs)
    return ret


# 解説
N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    C.append(c)
    A.append(int(a))

pos_w = [IINF] * (N + 1)
pos_b = [IINF] * (N + 1)
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# count_b[k][i]: i より左に、k 以下の黒が何個あるか
# count_w[k][i]: i より左に、k 以下の白が何個あるか
bc = [0] * (N * 2 + 1)
wc = [0] * (N * 2 + 1)
count_b = [[0] * (N * 2 + 1)]
count_w = [[0] * (N * 2 + 1)]
for k in range(1, N + 1):
    wc[pos_w[k] + 1] = 1
    bc[pos_b[k] + 1] = 1
    count_b.append(cumsum(bc))
    count_w.append(cumsum(wc))

# dp[b][w]: B を b 回、W を w 回使ったときの転倒数の最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
# for i in range(N * 2):
#     b = i
#     w = 0
#     if b > N:
#         w = b - N
#         b -= w
#
#     while w <= min(i, N):
#         if b < N:
#             # b + 1 を使う。
#             # 位置の写像が与えられたときのスワップの回数だから転倒数
#             # もともといた位置より右にあったやつで、既に使ってるのが何個か
#             orig_pos = pos_b[b + 1]
#             inv = i - count_b[b + 1][orig_pos] - count_w[w][orig_pos]
#             dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)
#         if w < N:
#             orig_pos = pos_w[w + 1]
#             inv = i - count_b[b][orig_pos] - count_w[w + 1][orig_pos]
#             dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)
#         b -= 1
#         w += 1


# もらう
for b, w in itertools.product(range(N + 1), repeat=2):
    if b > 0:
        # 最後に b を使う
        orig_pos = pos_b[b]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b - 1][w] + inv)
    if w > 0:
        # 最後に w を使う
        orig_pos = pos_w[w]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b][w - 1] + inv)
print(dp[N][N])

# import numpy as np
#
# print(np.array(dp))

Buggy python submission (verdict: Time Limit Exceeded):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

class BinaryIndexedTree:
    # http://hos.ac/slides/20140319_bit.pdf
    def __init__(self, size):
        """
        :param int size:
        """
        self._bit = [0] * size
        self._size = size

    def add(self, i, w):
        """
        i 番目に w を加える
        :param int i:
        :param int w:
        """
        x = i + 1
        while x <= self._size:
            self._bit[x - 1] += w
            x += x & -x

    def sum(self, i):
        """
        [0, i) の合計
        :param int i:
        """
        ret = 0
        while i > 0:
            ret += self._bit[i - 1]
            i -= i & -i
        return ret

    def __len__(self):
        return self._size

    def copy(self):
        ret = BinaryIndexedTree(0)
        ret._bit = self._bit[:]
        ret._size = self._size
        return ret


def cumsum(it):
    """
    累積和
    :param collections.Iterable it:
    """
    cs = 0
    ret = []
    for v in it:
        cs += v
        ret.append(cs)
    return ret


N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    a = int(a) - 1
    C.append(c)
    A.append(a)

pos_w = [-1] * N
pos_b = [-1] * N
ca = []
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# dp[b][w]: 今までに B を b 個、W を w 個使ったときの答えの最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
bits = [[None] * (N + 1) for _ in range(N + 1)]
bits[0][0] = BinaryIndexedTree(size=N * 2)
for i, (c, a) in enumerate(list(zip(C, A))):
    b = max(0, i - N)
    w = min(i, N)
    while w >= max(0, i - N):
        #: :type: BinaryIndexedTree
        bit = bits[b][w]
        if b < N:
            inv = i - bit.sum(pos_b[b])
            dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)

            if not bits[b + 1][w]:
                bits[b + 1][w] = bit.copy()
                bits[b + 1][w].add(pos_b[b], 1)
        if w < N:
            inv = i - bit.sum(pos_w[w])
            dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)

            if not bits[b][w + 1]:
                bits[b][w + 1] = bit
                bits[b][w + 1].add(pos_w[w], 1)
        b += 1
        w -= 1
    # import numpy as np
    # print(np.array(dp))
    # print()
print(dp[N][N])


Fixed submission:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

def cumsum(it):
    """
    累積和
    :param collections.Iterable it:
    """
    cs = 0
    ret = []
    for v in it:
        cs += v
        ret.append(cs)
    return ret


# 解説
N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    C.append(c)
    A.append(int(a))

pos_w = [IINF] * (N + 1)
pos_b = [IINF] * (N + 1)
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# count_b[k][i]: i より左に、k 以下の黒が何個あるか
# count_w[k][i]: i より左に、k 以下の白が何個あるか
bc = [0] * (N * 2 + 1)
wc = [0] * (N * 2 + 1)
count_b = [[0] * (N * 2 + 1)]
count_w = [[0] * (N * 2 + 1)]
for k in range(1, N + 1):
    wc[pos_w[k] + 1] = 1
    bc[pos_b[k] + 1] = 1
    count_b.append(cumsum(bc))
    count_w.append(cumsum(wc))

# dp[b][w]: B を b 回、W を w 回使ったときの転倒数の最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
# for i in range(N * 2):
#     b = i
#     w = 0
#     if b > N:
#         w = b - N
#         b -= w
#
#     while w <= min(i, N):
#         if b < N:
#             # b + 1 を使う。
#             # 位置の写像が与えられたときのスワップの回数だから転倒数
#             # もともといた位置より右にあったやつで、既に使ってるのが何個か
#             orig_pos = pos_b[b + 1]
#             inv = i - count_b[b + 1][orig_pos] - count_w[w][orig_pos]
#             dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)
#         if w < N:
#             orig_pos = pos_w[w + 1]
#             inv = i - count_b[b][orig_pos] - count_w[w + 1][orig_pos]
#             dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)
#         b -= 1
#         w += 1


# もらう
for b, w in itertools.product(range(N + 1), repeat=2):
    if b > 0:
        # 最後に b を使う
        orig_pos = pos_b[b]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b - 1][w] + inv)
    if w > 0:
        # 最後に w を使う
        orig_pos = pos_w[w]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b][w - 1] + inv)
print(dp[N][N])

# import numpy as np
#
# print(np.array(dp))

Buggy python submission (verdict: Time Limit Exceeded):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

class BinaryIndexedTree:
    # http://hos.ac/slides/20140319_bit.pdf
    def __init__(self, size):
        """
        :param int size:
        """
        self._bit = [0] * size
        self._size = size

    def add(self, i, w):
        """
        i 番目に w を加える
        :param int i:
        :param int w:
        """
        x = i + 1
        while x <= self._size:
            self._bit[x - 1] += w
            x += x & -x

    def sum(self, i):
        """
        [0, i) の合計
        :param int i:
        """
        ret = 0
        while i > 0:
            ret += self._bit[i - 1]
            i -= i & -i
        return ret

    def __len__(self):
        return self._size

    def copy(self):
        ret = BinaryIndexedTree(0)
        ret._bit = self._bit[:]
        ret._size = self._size
        return ret


N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    a = int(a) - 1
    C.append(c)
    A.append(a)

pos_w = [-1] * N
pos_b = [-1] * N
ca = []
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# dp[b][w]: 今までに B を b 個、W を w 個使ったときの答えの最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
bits = [[None] * (N + 1) for _ in range(N + 1)]
bits[0][0] = BinaryIndexedTree(size=N * 2)
for i, (c, a) in enumerate(list(zip(C, A))):
    b = max(0, i - N)
    w = min(i, N)
    while w >= max(0, i - N):
        #: :type: BinaryIndexedTree
        bit = bits[b][w]
        if b < N:
            inv = i - bit.sum(pos_b[b])
            dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)

            if not bits[b + 1][w]:
                bits[b + 1][w] = bit.copy()
                bits[b + 1][w].add(pos_b[b], 1)
        if w < N:
            inv = i - bit.sum(pos_w[w])
            dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)

            if not bits[b][w + 1]:
                bits[b][w + 1] = bit.copy()
                bits[b][w + 1].add(pos_w[w], 1)
        b += 1
        w -= 1
    # import numpy as np
    # print(np.array(dp))
    # print()
print(dp[N][N])


Fixed submission:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

def cumsum(it):
    """
    累積和
    :param collections.Iterable it:
    """
    cs = 0
    ret = []
    for v in it:
        cs += v
        ret.append(cs)
    return ret


# 解説
N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    C.append(c)
    A.append(int(a))

pos_w = [IINF] * (N + 1)
pos_b = [IINF] * (N + 1)
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# count_b[k][i]: i より左に、k 以下の黒が何個あるか
# count_w[k][i]: i より左に、k 以下の白が何個あるか
bc = [0] * (N * 2 + 1)
wc = [0] * (N * 2 + 1)
count_b = [[0] * (N * 2 + 1)]
count_w = [[0] * (N * 2 + 1)]
for k in range(1, N + 1):
    wc[pos_w[k] + 1] = 1
    bc[pos_b[k] + 1] = 1
    count_b.append(cumsum(bc))
    count_w.append(cumsum(wc))

# dp[b][w]: B を b 回、W を w 回使ったときの転倒数の最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
# for i in range(N * 2):
#     b = i
#     w = 0
#     if b > N:
#         w = b - N
#         b -= w
#
#     while w <= min(i, N):
#         if b < N:
#             # b + 1 を使う。
#             # 位置の写像が与えられたときのスワップの回数だから転倒数
#             # もともといた位置より右にあったやつで、既に使ってるのが何個か
#             orig_pos = pos_b[b + 1]
#             inv = i - count_b[b + 1][orig_pos] - count_w[w][orig_pos]
#             dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)
#         if w < N:
#             orig_pos = pos_w[w + 1]
#             inv = i - count_b[b][orig_pos] - count_w[w + 1][orig_pos]
#             dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)
#         b -= 1
#         w += 1


# もらう
for b, w in itertools.product(range(N + 1), repeat=2):
    if b > 0:
        # 最後に b を使う
        orig_pos = pos_b[b]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b - 1][w] + inv)
    if w > 0:
        # 最後に w を使う
        orig_pos = pos_w[w]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b][w - 1] + inv)
print(dp[N][N])

# import numpy as np
#
# print(np.array(dp))

Buggy python submission (verdict: Time Limit Exceeded):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

class BinaryIndexedTree:
    # http://hos.ac/slides/20140319_bit.pdf
    def __init__(self, size):
        """
        :param int size:
        """
        self._bit = [0] * size
        self._size = size

    def add(self, i, w):
        """
        i 番目に w を加える
        :param int i:
        :param int w:
        """
        x = i + 1
        while x <= self._size:
            self._bit[x - 1] += w
            x += x & -x

    def sum(self, i):
        """
        [0, i) の合計
        :param int i:
        """
        ret = 0
        while i > 0:
            ret += self._bit[i - 1]
            i -= i & -i
        return ret

    def __len__(self):
        return self._size

    def copy(self):
        ret = BinaryIndexedTree(0)
        ret._bit = self._bit[:]
        ret._size = self._size
        return ret


N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    a = int(a) - 1
    C.append(c)
    A.append(a)

pos_w = [-1] * N
pos_b = [-1] * N
ca = []
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# dp[b][w]: 今までに B を b 個、W を w 個使ったときの答えの最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
bits = [[None] * (N + 1) for _ in range(N + 1)]
bits[0][0] = BinaryIndexedTree(size=N * 2)
for i, (c, a) in enumerate(list(zip(C, A))):
    b = max(0, i - N)
    w = min(i, N)
    while w >= max(0, i - N):
        #: :type: BinaryIndexedTree
        bit = bits[b][w]
        if b < N:
            inv = i - bit.sum(pos_b[b])
            dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)

            if not bits[b + 1][w]:
                bits[b + 1][w] = bit.copy()
                bits[b + 1][w].add(pos_b[b], 1)
        if w < N:
            inv = i - bit.sum(pos_w[w])
            dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)

            if not bits[b][w + 1]:
                bits[b][w + 1] = bit.copy()
                bits[b][w + 1].add(pos_w[w], 1)
        b += 1
        w -= 1
    # import numpy as np
    # print(np.array(dp))
    # print()
print(dp[N][N])


Fixed submission:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353

def cumsum(it):
    """
    累積和
    :param collections.Iterable it:
    """
    cs = 0
    ret = []
    for v in it:
        cs += v
        ret.append(cs)
    return ret


# 解説
N = int(sys.stdin.buffer.readline())
CA = [sys.stdin.buffer.readline().decode().rstrip().split() for _ in range(N * 2)]
C = []
A = []
for c, a in CA:
    C.append(c)
    A.append(int(a))

pos_w = [IINF] * (N + 1)
pos_b = [IINF] * (N + 1)
for i, (c, a) in enumerate(list(zip(C, A))):
    if c == 'W':
        pos_w[a] = i
    else:
        pos_b[a] = i

# count_b[k][i]: i より左に、k 以下の黒が何個あるか
# count_w[k][i]: i より左に、k 以下の白が何個あるか
bc = [0] * (N * 2 + 1)
wc = [0] * (N * 2 + 1)
count_b = [[0] * (N * 2 + 1)]
count_w = [[0] * (N * 2 + 1)]
for k in range(1, N + 1):
    wc[pos_w[k] + 1] = 1
    bc[pos_b[k] + 1] = 1
    count_b.append(cumsum(bc))
    count_w.append(cumsum(wc))

# dp[b][w]: B を b 回、W を w 回使ったときの転倒数の最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
# for i in range(N * 2):
#     b = i
#     w = 0
#     if b > N:
#         w = b - N
#         b -= w
#
#     while w <= min(i, N):
#         if b < N:
#             # b + 1 を使う。
#             # 位置の写像が与えられたときのスワップの回数だから転倒数
#             # もともといた位置より右にあったやつで、既に使ってるのが何個か
#             orig_pos = pos_b[b + 1]
#             inv = i - count_b[b + 1][orig_pos] - count_w[w][orig_pos]
#             dp[b + 1][w] = min(dp[b + 1][w], dp[b][w] + inv)
#         if w < N:
#             orig_pos = pos_w[w + 1]
#             inv = i - count_b[b][orig_pos] - count_w[w + 1][orig_pos]
#             dp[b][w + 1] = min(dp[b][w + 1], dp[b][w] + inv)
#         b -= 1
#         w += 1


# もらう
for b, w in itertools.product(range(N + 1), repeat=2):
    if b > 0:
        # 最後に b を使う
        orig_pos = pos_b[b]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b - 1][w] + inv)
    if w > 0:
        # 最後に w を使う
        orig_pos = pos_w[w]
        inv = b - count_b[b][orig_pos] + w - count_w[w][orig_pos] - 1
        dp[b][w] = min(dp[b][w], dp[b][w - 1] + inv)
print(dp[N][N])

# import numpy as np
#
# print(np.array(dp))