Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    c = "BW".index(c); a = int(a)
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

dp = [[10**9]*(N+1) for i in range(N+1)]
dp[0][0] = 0
for i in range(N):
    dp[i+1][0] = dp[i][0] + P[i][0]
for i in range(N):
    dp[0][i+1] = dp[0][i] + Q[i][0]
for i in range(N):
    for j in range(N):
        dp[i+1][j+1] = min(dp[i][j+1] + P[i][j+1], dp[i+1][j] + Q[j][i+1])
print(dp[N][N])


Fixed submission:
from bisect import bisect
N = int(input())
B = []
BW = []; BB = []
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    c = "BW".index(c)
    B.append((c, a))
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

dist = [[10**9]*(N+1) for i in range(N+1)]
dist[0][0] = 0
que = [(0, 0, 0)]
for i in range(N):
    dist[i+1][0] = dist[i][0] + P[i][0]
for i in range(N):
    dist[0][i+1] = dist[0][i] + Q[i][0]
for i in range(N):
    for j in range(N):
        dist[i+1][j+1] = min(dist[i][j+1] + P[i][j+1], dist[i+1][j] + Q[j][i+1])
print(dist[N][N])

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
N = int(input())
B = []
BW = []; BB = []
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    c = "BW".index(c)
    B.append((c, a))
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

from heapq import heappush, heappop
dist = [[10**9]*(N+1) for i in range(N+1)]
dist[0][0] = 0
que = [(0, 0, 0)]
while que:
    cost, i, j = heappop(que)
    if dist[i][j] < cost:
        continue
    if i == j == N:
        print(cost)
        break
    if i < N and cost + P[i][j] < dist[i+1][j]:
        dist[i+1][j] = d = cost + P[i][j]
        heappush(que, (d, i+1, j))
    if j < N and cost + Q[j][i] < dist[i][j+1]:
        dist[i][j+1] = d = cost + Q[j][i]
        heappush(que, (d, i, j+1))


Fixed submission:
from bisect import bisect
N = int(input())
B = []
BW = []; BB = []
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    c = "BW".index(c)
    B.append((c, a))
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

dist = [[10**9]*(N+1) for i in range(N+1)]
dist[0][0] = 0
que = [(0, 0, 0)]
for i in range(N):
    dist[i+1][0] = dist[i][0] + P[i][0]
for i in range(N):
    dist[0][i+1] = dist[0][i] + Q[i][0]
for i in range(N):
    for j in range(N):
        dist[i+1][j+1] = min(dist[i][j+1] + P[i][j+1], dist[i+1][j] + Q[j][i+1])
print(dist[N][N])

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
N = int(input())
B = []
BW = []; BB = []
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    c = "BW".index(c)
    B.append((c, a))
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

from heapq import heappush, heappop
dist = [[10**9]*(N+1) for i in range(N+1)]
dist[0][0] = 0
que = [(0, 0, 0)]
while que:
    cost, i, j = heappop(que)
    if dist[i][j] < cost:
        continue
    if i == j == N:
        print(cost)
        break
    if i < N and cost + P[i][j] < dist[i+1][j]:
        dist[i+1][j] = d = cost + P[i][j]
        heappush(que, (d, i+1, j))
    if j < N and cost + Q[j][i] < dist[i][j+1]:
        dist[i][j+1] = d = cost + Q[j][i]
        heappush(que, (d, i, j+1))

Fixed submission:
from bisect import bisect
N = int(input())
B = []
BW = []; BB = []
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    c = "BW".index(c)
    B.append((c, a))
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

dist = [[10**9]*(N+1) for i in range(N+1)]
dist[0][0] = 0
que = [(0, 0, 0)]
for i in range(N):
    dist[i+1][0] = dist[i][0] + P[i][0]
for i in range(N):
    dist[0][i+1] = dist[0][i] + Q[i][0]
for i in range(N):
    for j in range(N):
        dist[i+1][j+1] = min(dist[i][j+1] + P[i][j+1], dist[i+1][j] + Q[j][i+1])
print(dist[N][N])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    if c == 'W':
        MW[a] = i
    else:
        MB[a] = i
data = [0]*(2*N+1)
def add(k, x):
    while k <= 2*N:
        data[k] += x
        k += k & -k
def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
AW = sorted(MW)
AB = sorted(MB)
cB = [[0]*(N+1) for i in range(N+1)]
cW = [[0]*(N+1) for i in range(N+1)]

IW = [MW[a]+1 for a in AW]
IB = [MB[a]+1 for a in AB]

for i in range(1, 2*N+1):
    data[i] = i & -i

for i, p in enumerate(IW):
    cWi = cW[i]; cBi = cB[i]
    for j, q in enumerate(IB):
        cWi[j] = get(p)-1
        cBi[j] = get(q)-1
        add(q, -1)
    cWi[N] = get(p)-1

    for q in IB:
        add(q, 1)
    add(p, -1)
cBi = cB[N]
for j, q in enumerate(IB):
    cBi[j] = get(q)-1
    add(q, -1)

rN1 = range(1, N+1)
dp = [[10**9]*(N+1) for i in range(N+1)]
dp[0][0] = tmp = 0
T = dp[0]; cBi = cB[0]
for j in rN1:
    tmp = T[j] = tmp + cBi[j-1]

for i in rN1:
    T = dp[i]
    S = dp[i-1]
    cWi = cW[i-1]; cBi = cB[i]
    tmp = T[0] = S[0] + cWi[0]
    for j in rN1:
        tmp = T[j] = min(S[j] + cWi[j], tmp + cBi[j-1])
print(dp[N][N])


Fixed submission:
from bisect import bisect
N = int(input())
B = []
BW = []; BB = []
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    c = "BW".index(c)
    B.append((c, a))
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

dist = [[10**9]*(N+1) for i in range(N+1)]
dist[0][0] = 0
que = [(0, 0, 0)]
for i in range(N):
    dist[i+1][0] = dist[i][0] + P[i][0]
for i in range(N):
    dist[0][i+1] = dist[0][i] + Q[i][0]
for i in range(N):
    for j in range(N):
        dist[i+1][j+1] = min(dist[i][j+1] + P[i][j+1], dist[i+1][j] + Q[j][i+1])
print(dist[N][N])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    if c == 'W':
        MW[a] = i
    else:
        MB[a] = i
data = [0]*(2*N+1)
def add(k, x):
    while k <= 2*N:
        data[k] += x
        k += k & -k
def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
AW = sorted(MW)
AB = sorted(MB)
cB = [[0]*(N+1) for i in range(N+1)]
cW = [[0]*(N+1) for i in range(N+1)]

IW = [MW[a]+1 for a in AW]
IB = [MB[a]+1 for a in AB]

for i in range(2*N):
    add(i+1, 1)
for i in range(N):
    cWi = cW[i]; cBi = cB[i]
    for j in range(N):
        cWi[j] = get(IW[i])-1
        cBi[j] = get(IB[j])-1
        add(IB[j], -1)
    cWi[N] = get(IW[i])-1

    for j in range(N):
        add(IB[j], 1)
    if i < N:
        add(IW[i], -1)
cBi = cB[N]
for j in range(N):
    cBi[j] = get(IB[j])-1
    add(IB[j], -1)

dp = [[10**9]*(N+1) for i in range(N+1)]
dp[0][0] = 0
for i in range(N+1):
    T = dp[i]
    S = dp[i-1]
    cWi = cW[i-1]; cBi = cB[i]
    for j in range(N+1):
        v = T[j]
        if i:
            v = min(v, S[j] + cWi[j])
        if j:
            v = min(v, T[j-1] + cBi[j-1])
        T[j] = v
print(dp[N][N])


Fixed submission:
from bisect import bisect
N = int(input())
B = []
BW = []; BB = []
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    c = "BW".index(c)
    B.append((c, a))
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

dist = [[10**9]*(N+1) for i in range(N+1)]
dist[0][0] = 0
que = [(0, 0, 0)]
for i in range(N):
    dist[i+1][0] = dist[i][0] + P[i][0]
for i in range(N):
    dist[0][i+1] = dist[0][i] + Q[i][0]
for i in range(N):
    for j in range(N):
        dist[i+1][j+1] = min(dist[i][j+1] + P[i][j+1], dist[i+1][j] + Q[j][i+1])
print(dist[N][N])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    if c == 'W':
        MW[a] = i
    else:
        MB[a] = i
data = [0]*(2*N+1)
def add(k, x):
    while k <= 2*N:
        data[k] += x
        k += k & -k
def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
AW = sorted(MW)
AB = sorted(MB)
cB = [[0]*(N+1) for i in range(N+1)]
cW = [[0]*(N+1) for i in range(N+1)]

IW = [MW[a]+1 for a in AW]
IB = [MB[a]+1 for a in AB]

for i in range(2*N):
    add(i+1, 1)
for i in range(N):
    cWi = cW[i]; cBi = cB[i]
    for j in range(N):
        cWi[j] = get(IW[i])-1
        cBi[j] = get(IB[j])-1
        add(IB[j], -1)
    cWi[N] = get(IW[i])-1

    for j in range(N):
        add(IB[j], 1)
    if i < N:
        add(IW[i], -1)
cBi = cB[N]
for j in range(N):
    cBi[j] = get(IB[j])-1
    add(IB[j], -1)

dp = [[10**9]*(N+1) for i in range(N+1)]
dp[0][0] = 0
for i in range(N+1):
    T = dp[i]
    S = dp[i-1]
    cWi = cW[i-1]; cBi = cB[i]
    for j in range(N+1):
        v = T[j]
        if i:
            v = min(v, S[j] + cWi[j])
        if j:
            v = min(v, T[j-1] + cBi[j-1])
        T[j] = v
print(dp[N][N])

Fixed submission:
from bisect import bisect
N = int(input())
B = []
BW = []; BB = []
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    c = "BW".index(c)
    B.append((c, a))
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

dist = [[10**9]*(N+1) for i in range(N+1)]
dist[0][0] = 0
que = [(0, 0, 0)]
for i in range(N):
    dist[i+1][0] = dist[i][0] + P[i][0]
for i in range(N):
    dist[0][i+1] = dist[0][i] + Q[i][0]
for i in range(N):
    for j in range(N):
        dist[i+1][j+1] = min(dist[i][j+1] + P[i][j+1], dist[i+1][j] + Q[j][i+1])
print(dist[N][N])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    #B.append(("BW".index(c), a))
    if c == 'W':
        MW[a] = i
    else:
        MB[a] = i
data = [0]*(2*N+1)
def add(k, x):
    while k <= 2*N:
        data[k] += x
        k += k & -k
def get(k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
AW = sorted(MW)
AB = sorted(MB)
cB = [[0]*(N+1) for i in range(N+1)]
cW = [[0]*(N+1) for i in range(N+1)]

for i in range(2*N):
    add(i+1, 1)
for i in range(N+1):
    for j in range(N+1):
        if i < N:
            cW[i][j] = get(MW[AW[i]]+1)-1
        if j < N:
            cB[i][j] = get(MB[AB[j]]+1)-1
        #print(i, j, cW[i][j], cB[i][j])
        if j < N:
            add(MB[AB[j]]+1, -1)
    for j in range(N):
        add(MB[AB[j]]+1, 1)
    if i < N:
        add(MW[AW[i]]+1, -1)

dp = [[10**9]*(N+1) for i in range(N+1)]
dp[0][0] = 0
for i in range(N+1):
    for j in range(N+1):
        v = dp[i][j]
        if i > 0:
            v = min(v, dp[i-1][j] + cW[i-1][j])
        if j > 0:
            v = min(v, dp[i][j-1] + cB[i][j-1])
        dp[i][j] = v
print(dp[N][N])


Fixed submission:
from bisect import bisect
N = int(input())
B = []
BW = []; BB = []
MB = {}; MW = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    c = "BW".index(c)
    B.append((c, a))
    if c:
        MW[a-1] = i
    else:
        MB[a-1] = i

P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]

for p in range(N):
    cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))

    P[p][0] = cnt
    for q in range(N):
        if MB[q] < MW[p]:
            cnt -= 1
        P[p][q+1] = cnt

for q in range(N):
    cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))

    Q[q][0] = cnt
    for p in range(N):
        if MW[p] < MB[q]:
            cnt -= 1
        Q[q][p+1] = cnt

dist = [[10**9]*(N+1) for i in range(N+1)]
dist[0][0] = 0
que = [(0, 0, 0)]
for i in range(N):
    dist[i+1][0] = dist[i][0] + P[i][0]
for i in range(N):
    dist[0][i+1] = dist[0][i] + Q[i][0]
for i in range(N):
    for j in range(N):
        dist[i+1][j+1] = min(dist[i][j+1] + P[i][j+1], dist[i+1][j] + Q[j][i+1])
print(dist[N][N])