Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
  c,a=input().split()
  if c=="B":b[int(a)-1]=i
  else:w[int(a)-1]=i
bit=[0]*2*n
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
  bitt=bit[:]
  for j in range(n):
    a=0
    k=2*n
    while k:
      a+=bitt[k-1]
      k-=k&-k
    if w[j]!=0:
      k=w[j]
      while k:
        a-=bitt[k-1]
        k-=k&-k
    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a+w[j]-(i+j))
    k=w[j]+1
    while k<=2*n:
      bitt[k-1]+=1
      k+=k&-k
    if i!=n:
      a=0
      k=2*n
      while k:
        a+=bitt[k-1]
        k-=k&-k
      if b[i]!=0:
        k=b[i]
        while k:
          a-=bitt[k-1]
          k-=k&-k
      dp[i+1][j+1]=dp[i][j+1]+a+b[i]-(i+j+1)
  if i!=n:
    a=b[i]-i
    k=2*n
    while k:
      a+=bit[k-1]
      k-=k&-k
    if b[i]!=0:
      k=b[i]
      while k:
        a-=bit[k-1]
        k-=k&-k
    dp[i+1][0]=dp[i][0]+a
    k=b[i]+1
    while k<=2*n:
      bit[k-1]+=1
      k+=k&-k
print(dp[n][n])

Fixed submission:
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
  c,a=input().split()
  if c=="B":b[int(a)-1]=i
  else:w[int(a)-1]=i
bit=[0]*2*n
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
  bitt=bit[:]
  for j in range(n):
    a=0
    k=2*n
    while k:
      a+=bitt[k-1]
      k-=k&-k
    if w[j]!=0:
      k=w[j]
      while k:
        a-=bitt[k-1]
        k-=k&-k
    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a+w[j]-(i+j))
    k=w[j]+1
    while k<=2*n:
      bitt[k-1]+=1
      k+=k&-k
    if i!=n:
      a=0
      k=2*n
      while k:
        a+=bitt[k-1]
        k-=k&-k
      if b[i]!=0:
        k=b[i]
        while k:
          a-=bitt[k-1]
          k-=k&-k
      dp[i+1][j+1]=dp[i][j+1]+a+b[i]-(i+j+1)
  if i!=n:
    a=b[i]-i
    k=2*n
    while k:
      a+=bit[k-1]
      k-=k&-k
    if b[i]!=0:
      k=b[i]
      while k:
        a-=bit[k-1]
        k-=k&-k
    dp[i+1][0]=dp[i][0]+a
    k=b[i]+1
    while k<=2*n:
      bit[k-1]+=1
      k+=k&-k
print(dp[n][n])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
def main():
  n=int(input())
  b=[0]*n
  w=[0]*n
  for i in range(2*n):
    c,a=input().split()
    if c=="B":b[int(a)-1]=i
    else:w[int(a)-1]=i
  bit=[2*n*[0]for _ in range(n+1)]
  dp=[(n+1)*[10**10]for _ in range(n+1)]
  dp[0][0]=0
  for i in range(n+1):
    dp[i][0]=0
    for k in range(i):
      a=0
      ind=2*n
      while ind:
        a+=bit[i][ind-1]
        ind-=ind&-ind
      if w[k]!=0:
        ind=w[k]
        while ind:
          a-=bit[i][ind-1]
          ind-=ind&-ind
      dp[i][0]+=w[k]+a-k
      ind=w[k]+1
      while ind<=2*n:
        bit[i][ind-1]+=1
        ind+=ind&-ind
  for j in range(n):
    for i in range(n+1):
      ba=0
      ind=2*n
      while ind:
        ba+=bit[i][ind-1]
        ind-=ind&-ind
      if b[j]!=0:
        ind=b[j]
        while ind:
          ba-=bit[i][ind-1]
          ind-=ind&-ind
      if i==0:
        dp[i][j+1]=dp[i][j]+b[j]+ba-(i+j)
      else:
        dp[i][j+1]=dp[i][j]+(b[j]+ba-(i+j))
        wa=0
        ind=2*n
        while ind:
          wa+=bit[i-1][ind-1]
          ind-=ind&-ind
        if w[i-1]!=0:
          ind=w[i-1]
          while ind:
            wa-=bit[i-1][ind-1]
            ind-=ind&-ind
        dp[i][j+1]=min(dp[i][j+1],dp[i-1][j+1]+(w[i-1]+wa-(i+j)))
      ind=b[j]+1
      while ind<=2*n:
        bit[i][ind-1]+=1
        ind+=ind&-ind
  print(dp[n][n])

if __name__ == "__main__":
  main()

Fixed submission:
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
  c,a=input().split()
  if c=="B":b[int(a)-1]=i
  else:w[int(a)-1]=i
bit=[0]*2*n
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
  bitt=bit[:]
  for j in range(n):
    a=0
    k=2*n
    while k:
      a+=bitt[k-1]
      k-=k&-k
    if w[j]!=0:
      k=w[j]
      while k:
        a-=bitt[k-1]
        k-=k&-k
    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a+w[j]-(i+j))
    k=w[j]+1
    while k<=2*n:
      bitt[k-1]+=1
      k+=k&-k
    if i!=n:
      a=0
      k=2*n
      while k:
        a+=bitt[k-1]
        k-=k&-k
      if b[i]!=0:
        k=b[i]
        while k:
          a-=bitt[k-1]
          k-=k&-k
      dp[i+1][j+1]=dp[i][j+1]+a+b[i]-(i+j+1)
  if i!=n:
    a=b[i]-i
    k=2*n
    while k:
      a+=bit[k-1]
      k-=k&-k
    if b[i]!=0:
      k=b[i]
      while k:
        a-=bit[k-1]
        k-=k&-k
    dp[i+1][0]=dp[i][0]+a
    k=b[i]+1
    while k<=2*n:
      bit[k-1]+=1
      k+=k&-k
print(dp[n][n])

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
  c,a=input().split()
  if c=="B":b[int(a)-1]=i
  else:w[int(a)-1]=i
bit=[2*n*[0]for _ in range(n+1)]
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
  dp[i][0]=0
  for k in range(i):
    a=0
    ind=2*n
    while ind:
      a+=bit[i][ind-1]
      ind-=ind&-ind
    if w[k]!=0:
      ind=w[k]
      while ind:
        a-=bit[i][ind-1]
        ind-=ind&-ind
    dp[i][0]+=w[k]+a-k
    ind=w[k]+1
    while ind<=2*n:
      bit[i][ind-1]+=1
      ind+=ind&-ind
for j in range(n):
  for i in range(n+1):
    ba=0
    ind=2*n
    while ind:
      ba+=bit[i][ind-1]
      ind-=ind&-ind
    if b[j]!=0:
      ind=b[j]
      while ind:
        ba-=bit[i][ind-1]
        ind-=ind&-ind
    if i==0:
      dp[i][j+1]=dp[i][j]+b[j]+ba-(i+j)
    else:
      dp[i][j+1]=dp[i][j]+(b[j]+ba-(i+j))
      wa=0
      ind=2*n
      while ind:
        wa+=bit[i-1][ind-1]
        ind-=ind&-ind
      if w[i-1]!=0:
        ind=w[i-1]
        while ind:
          wa-=bit[i-1][ind-1]
          ind-=ind&-ind
      dp[i][j+1]=min(dp[i][j+1],dp[i-1][j+1]+(w[i-1]+wa-(i+j)))
    ind=b[j]+1
    while ind<=2*n:
      bit[i][ind-1]+=1
      ind+=ind&-ind
print(dp[n][n])

Fixed submission:
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
  c,a=input().split()
  if c=="B":b[int(a)-1]=i
  else:w[int(a)-1]=i
bit=[0]*2*n
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
  bitt=bit[:]
  for j in range(n):
    a=0
    k=2*n
    while k:
      a+=bitt[k-1]
      k-=k&-k
    if w[j]!=0:
      k=w[j]
      while k:
        a-=bitt[k-1]
        k-=k&-k
    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a+w[j]-(i+j))
    k=w[j]+1
    while k<=2*n:
      bitt[k-1]+=1
      k+=k&-k
    if i!=n:
      a=0
      k=2*n
      while k:
        a+=bitt[k-1]
        k-=k&-k
      if b[i]!=0:
        k=b[i]
        while k:
          a-=bitt[k-1]
          k-=k&-k
      dp[i+1][j+1]=dp[i][j+1]+a+b[i]-(i+j+1)
  if i!=n:
    a=b[i]-i
    k=2*n
    while k:
      a+=bit[k-1]
      k-=k&-k
    if b[i]!=0:
      k=b[i]
      while k:
        a-=bit[k-1]
        k-=k&-k
    dp[i+1][0]=dp[i][0]+a
    k=b[i]+1
    while k<=2*n:
      bit[k-1]+=1
      k+=k&-k
print(dp[n][n])

Buggy python submission (verdict: Time Limit Exceeded):
class BinaryIndexedTree:
  def __init__(self,n):
    self.bit=[0]*n
  def add(self,i,x):
    i+=1
    while i<=len(self.bit):
      self.bit[i-1]+=x
      i+=i&-i
  def sum_sub(self,i):
    a=0
    i+=1
    while i:
      a+=self.bit[i-1]
      i-=i&-i
    return a
  def sum(self,i,j):
    a=self.sum_sub(j)
    if i!=0:a-=self.sum_sub(i-1)
    return a
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
  c,a=input().split()
  if c=="B":b[int(a)-1]=i
  else:w[int(a)-1]=i
bit=[BinaryIndexedTree(2*n)for _ in range(n+1)]
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
  dp[i][0]=0
  for k in range(i):
    dp[i][0]+=w[k]+bit[i].sum(w[k],2*n-1)-k
    bit[i].add(w[k],1)
for j in range(n):
  for i in range(n+1):
    if i==0:
      dp[i][j+1]=dp[i][j]+b[j]+bit[i].sum(b[j],2*n-1)-(i+j)
    else:
      dp[i][j+1]=dp[i][j]+(b[j]+bit[i].sum(b[j],2*n-1)-(i+j))
      dp[i][j+1]=min(dp[i][j+1],dp[i-1][j+1]+(w[i-1]+bit[i-1].sum(w[i-1],2*n-1)-(i+j)))
    bit[i].add(b[j],1)
print(dp[n][n])

Fixed submission:
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
  c,a=input().split()
  if c=="B":b[int(a)-1]=i
  else:w[int(a)-1]=i
bit=[0]*2*n
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
  bitt=bit[:]
  for j in range(n):
    a=0
    k=2*n
    while k:
      a+=bitt[k-1]
      k-=k&-k
    if w[j]!=0:
      k=w[j]
      while k:
        a-=bitt[k-1]
        k-=k&-k
    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a+w[j]-(i+j))
    k=w[j]+1
    while k<=2*n:
      bitt[k-1]+=1
      k+=k&-k
    if i!=n:
      a=0
      k=2*n
      while k:
        a+=bitt[k-1]
        k-=k&-k
      if b[i]!=0:
        k=b[i]
        while k:
          a-=bitt[k-1]
          k-=k&-k
      dp[i+1][j+1]=dp[i][j+1]+a+b[i]-(i+j+1)
  if i!=n:
    a=b[i]-i
    k=2*n
    while k:
      a+=bit[k-1]
      k-=k&-k
    if b[i]!=0:
      k=b[i]
      while k:
        a-=bit[k-1]
        k-=k&-k
    dp[i+1][0]=dp[i][0]+a
    k=b[i]+1
    while k<=2*n:
      bit[k-1]+=1
      k+=k&-k
print(dp[n][n])

Buggy python submission (verdict: Time Limit Exceeded):
class BinaryIndexedTree:
  def __init__(self,n):
    self.bit=[0]*n
  def add(self,i,x):
    i+=1
    while i<=len(self.bit):
      self.bit[i-1]+=x
      i+=i&-i
  def sum_sub(self,i):
    a=0
    i+=1
    while i:
      a+=self.bit[i-1]
      i-=i&-i
    return a
  def sum(self,i,j):
    a=self.sum_sub(j)
    if i!=0:a-=self.sum_sub(i-1)
    return a
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
  c,a=input().split()
  if c=="B":b[int(a)-1]=i
  else:w[int(a)-1]=i
bit=[BinaryIndexedTree(2*n)for _ in range(n+1)]
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
  dp[i][0]=0
  for k in range(i):
    dp[i][0]+=w[k]+bit[i].sum(w[k],2*n-1)-k
    bit[i].add(w[k],1)
for j in range(n):
  for i in range(n+1):
    if i==0:
      dp[i][j+1]=dp[i][j]+b[j]+bit[i].sum(b[j],2*n-1)-(i+j)
    else:
      dp[i][j+1]=dp[i][j]+(b[j]+bit[i].sum(b[j],2*n-1)-(i+j))
      dp[i][j+1]=min(dp[i][j+1],dp[i-1][j+1]+(w[i-1]+bit[i-1].sum(w[i-1],2*n-1)-(i+j)))
    bit[i].add(b[j],1)
print(dp[n][n])

Fixed submission:
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
  c,a=input().split()
  if c=="B":b[int(a)-1]=i
  else:w[int(a)-1]=i
bit=[0]*2*n
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
  bitt=bit[:]
  for j in range(n):
    a=0
    k=2*n
    while k:
      a+=bitt[k-1]
      k-=k&-k
    if w[j]!=0:
      k=w[j]
      while k:
        a-=bitt[k-1]
        k-=k&-k
    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a+w[j]-(i+j))
    k=w[j]+1
    while k<=2*n:
      bitt[k-1]+=1
      k+=k&-k
    if i!=n:
      a=0
      k=2*n
      while k:
        a+=bitt[k-1]
        k-=k&-k
      if b[i]!=0:
        k=b[i]
        while k:
          a-=bitt[k-1]
          k-=k&-k
      dp[i+1][j+1]=dp[i][j+1]+a+b[i]-(i+j+1)
  if i!=n:
    a=b[i]-i
    k=2*n
    while k:
      a+=bit[k-1]
      k-=k&-k
    if b[i]!=0:
      k=b[i]
      while k:
        a-=bit[k-1]
        k-=k&-k
    dp[i+1][0]=dp[i][0]+a
    k=b[i]+1
    while k<=2*n:
      bit[k-1]+=1
      k+=k&-k
print(dp[n][n])