Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())
def gcd(x,y,z):
    List = [x,y,z]
    List.sort()
    if int(1) in List:
        return 1
    while True:
        if List[2] % List[1] !=0:
            mod = List[2] % List[1]
            List[2] = List[1]
            List[1] = mod
        else:
            gcd1 = List[1]
            break
    List2 = [List[0],gcd1]
    List2.sort
    while True:
        if List2[1] % List2[0] !=0:
            mod2 = List2[1] % List2[0]
            List2[1] = List2[0]
            List2[0] = mod2
        else:
            gcd2 = List2[0]
            break
    return gcd2
S = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            S += gcd(i,j,k)
print(S)

Fixed submission:
K = int(input())
S = 0

def gcd(a,b):
   while b != 0:
       a, b = b, a % b
       if b ==0:
           return a
A1 = [0,0]
A2 = [0,0,0]
for i in range(1, K+1):
    S = S + i

if K > 1:
    for j in range(1, K):
        A1[0] =  j
        for k in range(j+1,K+1):
            A1[1] = k
            S += 6 * gcd(A1[1],A1[0])

if K > 2:
    for i1 in range(1, K-1):
        A2[0] = i1
        for k1 in range(i1+1, K):
            A2[1] = k1
            for j1 in range(k1+1,K+1):
                A2[2] = j1
                G3 = gcd(A2[2],A2[1])
                if G3 >= A2[0]:
                    S += 6 * gcd(G3,A2[0])
                else:
                    S += 6 * gcd(A2[0],G3)
print(S)

Buggy python submission (verdict: Runtime Error):
N = int(input())
s = 0
for i in range(0,N):
  if (i+1) % 3 1= 0 or (i+1) % 5 != 0:
    s = s + (i+1)
print(s)

Fixed submission:
K = int(input())
S = 0

def gcd(a,b):
   while b != 0:
       a, b = b, a % b
       if b ==0:
           return a
A1 = [0,0]
A2 = [0,0,0]
for i in range(1, K+1):
    S = S + i

if K > 1:
    for j in range(1, K):
        A1[0] =  j
        for k in range(j+1,K+1):
            A1[1] = k
            S += 6 * gcd(A1[1],A1[0])

if K > 2:
    for i1 in range(1, K-1):
        A2[0] = i1
        for k1 in range(i1+1, K):
            A2[1] = k1
            for j1 in range(k1+1,K+1):
                A2[2] = j1
                G3 = gcd(A2[2],A2[1])
                if G3 >= A2[0]:
                    S += 6 * gcd(G3,A2[0])
                else:
                    S += 6 * gcd(A2[0],G3)
print(S)
Buggy python submission (verdict: Runtime Error):
N = int(input())
K = int(input())
S = N // K
Answer = abs(N - K*S)
if abs(Answer - K) <= Answer:
    Answer = abs(Answer - K)
print(Answer)

Fixed submission:
N,K = map(int,input().split())
S = N // K
Answer = abs(N - K*S)
if abs(Answer - K) <= Answer:
    Answer = abs(Answer - K)
print(Answer)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
while True:
    if (N - K) < 0:
        if N > K - N:
            N = K - N
        else:
            break
    elif (N - K) > 0:
        N = N - K
    elif N - K == 0:
        N = 0
print(N)

Fixed submission:
N,K = map(int,input().split())
S = N // K
Answer = abs(N - K*S)
if abs(Answer - K) <= Answer:
    Answer = abs(Answer - K)
print(Answer)
Buggy python submission (verdict: Time Limit Exceeded):
def make(Lun):
    Lunstr = str(Lun)
    Lu = []
    if Lunstr[-1] != '9' and Lunstr[-1] != '0':
        for i in range(-1,2):
            New = int(Lunstr + str(int(Lunstr[-1]) + i))
            Lu.append(New)
    elif Lunstr[-1] == '9':
        for i in range(-1,1):
            New = int(Lunstr + str(int(Lunstr[-1]) + i))
            Lu.append(New)
    else:
        for i in range(0,2):
            New = int(Lunstr + str(int(Lunstr[-1]) + i))
            Lu.append(New)
    return Lu
LunLun = [i for i in range(1,10)]
k = 0
i = 10
K = int(input())
sig = 0
for _ in range(100000000):
    NewLun = LunLun[k:i]
    for j in range(len(NewLun)):
        LunLun = LunLun + make(NewLun[j])
        if len(LunLun) >= K:
            sig = -1
            break
    k = i-1
    i = len(LunLun) + 1
    if sig == -1:
        break
print(LunLun[K-1])

Fixed submission:
from collections import deque
K = int(input())
S = [i for i in range(1,10)]
S = deque(S)
for i in range(1,K+1):
    Lun = S.popleft()
    Lu = Lun%10
    if Lu !=0:
        S.append(10*Lun+Lu-1)
    S.append(Lun*10+Lu)
    if Lu !=9:
        S.append(10*Lun+Lu+1)
print(Lun)
Buggy python submission (verdict: Runtime Error):
N = int(input())
K = list(map(int,input().split()))
A = [0]*N
for i in range(N):
    A[K[i]] += 1
r = 0
for k in range(N):
    r += 0.5*(A[k]*(A[k]-1))
An =[]
for j in range(N):
    D = A[K[j]]-1
    An.append(int(r-(0.5*(D+1)*(D))+(0.5*D*(D-1))))
for i in range(len(An)):
    print(An[i])

Fixed submission:
N = int(input())
K = list(map(int,input().split()))
A = [0]*N
for i in range(N):
    A[K[i]-1] += 1
r = 0
for k in range(N):
    r += 0.5*(A[k]*(A[k]-1))
An =[]
for j in range(N):
    D = A[K[j]-1]-1
    An.append(int(r-(0.5*(D+1)*(D))+(0.5*D*(D-1))))
for i in range(len(An)):
    print(An[i])
Buggy python submission (verdict: Time Limit Exceeded):
N =int(input())
List = []
Dictionary = []
for i in range(N):
    In = input()
    List.append(In)
    if In in Dictionary:
        continue
    else:
        Dictionary.append(In)
Max = 0
Answer = []
for j in range(len(Dictionary)):
    var = List.count(Dictionary[j])
    if var > Max:
        Max = var
        Answer = []
        Answer.append(Dictionary[j])
    elif var == Max:
        Answer.append(Dictionary[j])
Answer.sort()
for _ in range(len(Answer)):
    print(Answer[_])

Fixed submission:
import collections
import itertools


N =int(input())
List = []
Dictionary = []
for i in range(N):
    In = input()
    List.append(In)
    
counters= collections.Counter(List)
counter = sorted(counters.items())
mode_v = counters.most_common()[0][-1]

for j in range(len(counter)):
    an = counter[j][1]
    if mode_v == an:
        print(counter[j][0])
    else:
        continue
Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
List = list(map(int,input().split()))
for i in range(M):
    MaxY = max(List) 
    Y = List.index(MaxY)
    List[Y] = MaxY //2
Sum = 0
for j in range(N):
    Sum += List[j]
print(Sum)

Fixed submission:
import heapq

n, m = map(int, input().split())
a = list(map(lambda x: int(x)*(-1), input().split()))
heapq.heapify(a)  

for _ in range(m):
    tmp_min = heapq.heappop(a)
    heapq.heappush(a, (-1)*(-tmp_min//2))  
print(-sum(a))
Buggy python submission (verdict: Runtime Error):
N,Q = map(int,input().split())
def dfs(x, score):
    for y in Tree[x]:
        if Parent[x] != y:
            Parent[y] = x
            score[y] += score[x]
            dfs(y, score)
Tree = [[] for i in range(N+1)]
Parent = [0]*(N+1)
for i in range(N-1):
    a,b = map(int,input().split())
    Tree[a].append(b)
    Tree[b].append(a)
score = [0]*(N+1)
for j in range(Q):
    p,x = map(int,input().split())
    score[p] += x
dfs(1,score)
print(*score[1:],end="\t")

Fixed submission:
import sys
sys.setrecursionlimit(1<<30)
N,Q = map(int,input().split())
def dfs(x, score):
    for y in Tree[x]:
        if Parent[x] != y:
            Parent[y] = x
            score[y] += score[x]
            dfs(y, score)
Tree = [[] for i in range(N+1)]
Parent = [0]*(N+1)
for i in range(N-1):
    a,b = map(int,input().split())
    Tree[a].append(b)
    Tree[b].append(a)
score = [0]*(N+1)
for j in range(Q):
    p,x = map(int,input().split())
    score[p] += x
dfs(1,score)
print(*score[1:],end="\t")

Buggy python submission (verdict: Runtime Error):
N,Q = map(int,input().split())
def dfs(x, score):
    for y in Tree[x]:
        if Parent[x] != y:
            Parent[y] = x
            score[y] += score[x]
            dfs(y, score)
Tree = [[] for i in range(N+1)]
Parent = [0]*(N+1)
for i in range(N-1):
    a,b = map(int,input().split())
    Tree[a].append(b)
    Tree[b].append(a)
score = [0]*(N+1)
for j in range(Q):
    p,x = map(int,input().split())
    score[p] += x
dfs(1,score)
print(*score[1:],end="\t")

Fixed submission:
import sys
sys.setrecursionlimit(1<<30)
N,Q = map(int,input().split())
def dfs(x, score):
    for y in Tree[x]:
        if Parent[x] != y:
            Parent[y] = x
            score[y] += score[x]
            dfs(y, score)
Tree = [[] for i in range(N+1)]
Parent = [0]*(N+1)
for i in range(N-1):
    a,b = map(int,input().split())
    Tree[a].append(b)
    Tree[b].append(a)
score = [0]*(N+1)
for j in range(Q):
    p,x = map(int,input().split())
    score[p] += x
dfs(1,score)
print(*score[1:],end="\t")

Buggy python submission (verdict: Runtime Error):
N,Q = map(int,input().split())
def dfs(x, score):
    for y in Tree[x]:
        if Parent[x] != y:
            Parent[y] = x
            score[y] += score[x]
            dfs(y, score)
Tree = [[] for i in range(N+1)]
Parent = [0]*(N+1)
for i in range(N-1):
    a,b = map(int,input().split())
    Tree[a].append(b)
    Tree[b].append(a)
score = [0]*(N+1)
for j in range(Q):
    p,x = map(int,input().split())
    score[p] += x
dfs(1,score)
for i in score[1:]:
    print(i)

Fixed submission:
import sys
sys.setrecursionlimit(1<<30)
N,Q = map(int,input().split())
def dfs(x, score):
    for y in Tree[x]:
        if Parent[x] != y:
            Parent[y] = x
            score[y] += score[x]
            dfs(y, score)
Tree = [[] for i in range(N+1)]
Parent = [0]*(N+1)
for i in range(N-1):
    a,b = map(int,input().split())
    Tree[a].append(b)
    Tree[b].append(a)
score = [0]*(N+1)
for j in range(Q):
    p,x = map(int,input().split())
    score[p] += x
dfs(1,score)
print(*score[1:],end="\t")
Buggy python submission (verdict: Time Limit Exceeded):
INF = 10 ** 9 + 7

S = input()
# dp[i][j]はうしろからi桁を13で割った余りがjであるものの個数
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
mul = 1

for i in range(len(S)):
    x = S[-(i + 1)]
    if x == "?":
        for k in range(10):
            for j in range(13):
                dp[i + 1][(mul * k + j) % 13] += dp[i][j]
                dp[i + 1][(mul * k + j) % 13] %= INF
    else:
        k = int(x)
        for j in range(13):
            dp[i + 1][(mul * k + j) % 13] += dp[i][j]
            dp[i + 1][(mul * k + j) % 13] %= INF
    mul = mul * 10 % 13  # 計算量削減のためここで13で割っている

print(dp[len(S)][5])

Fixed submission:
INF = 10 ** 9 + 7

S = input()
# dp[i][j]はうしろからi桁を13で割った余りがjであるものの個数
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
mul = 1

for i in range(len(S)):
    x = S[-(i + 1)]
    if x == "?":
        for k in range(10):
            for j in range(13):
                dp[i + 1][(mul * k + j) % 13] += dp[i][j]
                dp[i + 1][(mul * k + j) % 13] %= INF
    else:
        k = int(x)
        for j in range(13):
            dp[i + 1][(mul * k + j) % 13] += dp[i][j]
            dp[i + 1][(mul * k + j) % 13] %= INF
    mul = mul * 10 % 13  # 計算量削減のためここで13で割っている

print(dp[len(S)][5])
Buggy python submission (verdict: Runtime Error):
W,H,x,y=map(int,input())
print(W*H/2)

Fixed submission:
W,H,x,y=map(int,input().split())
a = 0
if x==W/2 and y == H/2:
  a=1
print(W*H/2,a)
Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
Stlist = [1]*N
for i in range(M):
    breakstair = int(input())
    Stlist[breakstair-1] = 0
Comb = [1,2]
if Stlist[0] == 0:
    Comb[0] = 0
    Comb[1] = 1
if Stlist[1] == 0:
    Comb[1] = 0
for i in range(2,N):
    Comb.append((Comb[i-1] + Comb[i-2])%(10**9+7))
    if Stlist[i] == 0:
        Comb[i] = 0
print(Comb[-1])


Fixed submission:
N,M = map(int,input().split())
Stlist = [1]*N
for i in range(M):
    breakstair = int(input())
    Stlist[breakstair-1] = 0
Comb = [1,2]
if N == 1:
    Comb = Stlist
else:
    if Stlist[0] == 0:
        Comb[0] = 0
        Comb[1] = 1
    if Stlist[1] == 0:
       Comb[1] = 0
    for i in range(2,N):
        Comb.append((Comb[i-1] + Comb[i-2])%(10**9+7))
        if Stlist[i] == 0:
            Comb[i] = 0
print(Comb[-1])
Buggy python submission (verdict: Runtime Error):
S = input()
N = len(S)
i = 0
while i < N:
    sig = 0
    S5 = S[i:i+5]
    if S5 == 'dream':
        sig = 1
        if S[i+5:i+7] == 'er' and S[i+7] != 'a':
            i += 2
        i += 5
    elif S5 == 'erase':
        sig = 1
        if S[i+5:i+6] == 'r':
            i += 1
        i += 5
    if sig == 0:
        sig = 2
        Answer = 'NO'
        break
if sig != 2:
    Answer = 'YES'
print(Answer)

Fixed submission:
S = input()
N = len(S)
i = 0
while i < N:
    sig = 0
    S5 = S[i:i+5]
    if S5 == 'dream':
        sig = 1
        if S[i+5:i+7] == 'er' and S[i+7:i+8] != 'a':
            i += 2
        i += 5
    elif S5 == 'erase':
        sig = 1
        if S[i+5:i+6] == 'r':
            i += 1
        i += 5
    if sig == 0:
        sig = 2
        Answer = 'NO'
        break
if sig != 2:
    Answer = 'YES'
print(Answer)

Buggy python submission (verdict: Runtime Error):
S = input()
N = len(S)
i = 0
while i < N:
    sig = 0
    S5 = S[i:i+5]
    if S5 == 'dream':
        sig = 1
        if S[i+5:i+7] == 'er' and S[i+7] != 'a':
            i += 2
        i += 5
    elif S5 == 'erase':
        sig = 1
        if S[i+5:i+6] == 'r':
            i += 1
        i += 5
    if sig == 0:
        sig = 2
        Answer = 'No'
        break
if sig != 2:
    Answer = 'Yes'
print(Answer)

Fixed submission:
S = input()
N = len(S)
i = 0
while i < N:
    sig = 0
    S5 = S[i:i+5]
    if S5 == 'dream':
        sig = 1
        if S[i+5:i+7] == 'er' and S[i+7:i+8] != 'a':
            i += 2
        i += 5
    elif S5 == 'erase':
        sig = 1
        if S[i+5:i+6] == 'r':
            i += 1
        i += 5
    if sig == 0:
        sig = 2
        Answer = 'NO'
        break
if sig != 2:
    Answer = 'YES'
print(Answer)

Buggy python submission (verdict: Runtime Error):
S = input()
N = len(S)
i = 0
while i < N:
    sig = 0
    S5 = S[i:i+5]
    if S5 == 'dream':
        sig = 1
        if S[i+5:i+7] == 'er' and S[i+7] != 'a':
            i += 2
        i += 5
    elif S5 == 'erase':
        sig = 1
        if S[i+5:i+6] == 'r':
            i += 1
        i += 5
    if sig == 0:
        sig = 2
        Answer = 'No'
        break
if sig != 2:
    Answer = 'Yes'
print(Answer)

Fixed submission:
S = input()
N = len(S)
i = 0
while i < N:
    sig = 0
    S5 = S[i:i+5]
    if S5 == 'dream':
        sig = 1
        if S[i+5:i+7] == 'er' and S[i+7:i+8] != 'a':
            i += 2
        i += 5
    elif S5 == 'erase':
        sig = 1
        if S[i+5:i+6] == 'r':
            i += 1
        i += 5
    if sig == 0:
        sig = 2
        Answer = 'NO'
        break
if sig != 2:
    Answer = 'YES'
print(Answer)
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
Sum =0
for i in range(len(A)):
  Sum += (A[0]-A[i])**2
Answer = Sum
for j in range(len(A)):
  Sum =0
  for k in range(-100,101):
    Sum+= (A[k]-j)**2
  Answer =min(Answer, Sum)
print(Answer)
  


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
Sum =0
for i in range(len(A)):
  Sum += (A[0]-A[i])**2
Answer = Sum
for j in range(-100,101):
  Sum =0
  for k in range(len(A)):
    Sum+= (A[k]-j)**2
  Answer =min(Answer, Sum)
print(Answer)

Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
Sum =0
for i in range(len(A)):
  Sum += (A[0]-A[i])**2
Answer = Sum
for j in range(-100,101):
  Sum =0
  for k in range(len(A)):
    Sum+= (A[k]-A[j])**2
  Answer =min(Answer, Sum)
print(Answer)
  


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
Sum =0
for i in range(len(A)):
  Sum += (A[0]-A[i])**2
Answer = Sum
for j in range(-100,101):
  Sum =0
  for k in range(len(A)):
    Sum+= (A[k]-j)**2
  Answer =min(Answer, Sum)
print(Answer)

Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,inout().split()))
Sum =0
for i in range(len(N)):
  Sum += (A[0]-A[i])**2
Answer = Sum
for j in range(len(N)):
  Sum =0
  for k in range(len(N)):
    Sum+= (A[k]-A[j])**2
  Answer =min(Answer, Sum)
print(Answer)
  

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
Sum =0
for i in range(len(A)):
  Sum += (A[0]-A[i])**2
Answer = Sum
for j in range(-100,101):
  Sum =0
  for k in range(len(A)):
    Sum+= (A[k]-j)**2
  Answer =min(Answer, Sum)
print(Answer)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
towns = [0] * n
cnt = 1

def rewr(x, y, z):
    if x == y:
        return z
    else:
        return x

for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if towns[a] == 0 and towns[b] == 0:
        towns[a] = cnt
        towns[b] = cnt
        cnt += 1
    elif towns[a] > 0 and towns[b] == 0:
        towns[b] = towns[a]
    elif towns[b] > 0 and towns[a] == 0:
        towns[a] = towns[b]
    elif towns[a] > 0 and towns[b] > 0:
        if towns[a] != towns[b]:
            min_no = min(towns[a], towns[b])
            max_no = max(towns[a], towns[b])
            towns = [rewr(twn, max_no, min_no) for twn in towns]
    else:
        pass
        #print('pass')

rslt = 0
#print(towns)
rslt += max(towns) - 1
rslt += towns.count(0)
print(rslt)


Fixed submission:
n = int(input())
s=''
for i in range(n):
    s = s + 'ACL'
print(s)
Buggy python submission (verdict: Runtime Error):
x=input()
a=[0]
d={}
d[0]=0
for i in range(1,1000):
	a+=[i**5]
	a+=[-i**5]
	d[i**5]=i
	d[-i**5]=-i

a.sort()
for i in range(len(a)):
    if x+a[i] in a:
        print d[a[i]+x],d[a[i]]
        break

Fixed submission:
x=input()
a=[0]
d={}
d[0]=0
for i in range(1,1000):
	a+=[i**5]
	a+=[-i**5]
	d[i**5]=i
	d[-i**5]=-i

a.sort()
for i in range(len(a)):
    if x+a[i] in a:
        print d[a[i]+x],d[a[i]]
        break
Buggy python submission (verdict: Runtime Error):
a,b,n=map(int,raw_input().split())
f=min(n,b-1)
print int((f*a)/b)

Fixed submission:
a,b,n=map(int,raw_input().split())
f=min(n,b-1)
print int((f*a)/b)
Buggy python submission (verdict: Runtime Error):
A,B,C,D = map(int, input().split())
 
x = list(range(A,B+1))
x = set(x)
y = list(range(C,D+1))
y = set(y)
 
if len(x)==0 or len(y)==0 or len(x&y)==0:
  print("No")
else:
  print("Yes")

Fixed submission:
n = int(input())

output = 'ACL'*n
print(output)

Buggy python submission (verdict: Runtime Error):
k = input()
output = "ACL" *k
print(output)

Fixed submission:
n = int(input())

output = 'ACL'*n
print(output)
Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit((i8,i8,i8[:]), cache=True)
def main(H, W, XY):
    A = np.zeros((H, W), np.int64)
    row = np.zeros(H, np.int64)
    col = np.zeros(W, np.int64)
    for i in range(0, len(XY), 2):
        x, y = XY[i:i + 2] - 1
        row[x] += 1
        col[y] += 1
        A[x, y] += 1
    ans = 0
    for h in range(H):
        for w in range(W):
            x = row[h] + col[w] - A[h, w]
            ans = max(ans, x)
    return ans

H, W, M = map(int, readline().split())
XY = np.array(read().split(), np.int64)

print(main(H, W, XY))

Fixed submission:
import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit((i8, i8, i8[:]), cache=True)
def main(H, W, XY):
    U = 300_010
    row = np.zeros(U, np.int64)
    col = np.zeros(U, np.int64)
    for i in range(0, len(XY), 2):
        x, y = XY[i:i + 2] - 1
        row[x] += 1
        col[y] += 1
    row_max = row.max()
    col_max = col.max()
    choice = np.sum(row == row_max) * np.sum(col == col_max)
    for i in range(0, len(XY), 2):
        x, y = XY[i:i + 2] - 1
        if row[x] == row_max and col[y] == col_max:
            choice -= 1
    x = row_max + col_max
    return x if choice else x - 1

H, W, M = map(int, readline().split())
XY = np.array(read().split(), np.int64)

print(main(H, W, XY))
Buggy python submission (verdict: Runtime Error):
import sys
from heapq import heappush, heappop

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main(N, K, L, R):
    left = [[] for _ in range(N)]
    right = [[] for _ in range(N)]
    score = 0
    for k, l, r in zip(K, L, R):
        k -= 1
        if l < r:
            score += l
            right[k].append(r - l)
        else:
            score += r
            left[k].append(l - r)
    q = []
    for n, xs in enumerate(left, 1):
        for x in xs:
            if len(q) < n:
                heappush(q, x)
            else:
                heappushpop(q, x)
    score += sum(q)
    q = []
    for n, xs in enumerate(right[::-1][1:], 1):
        for x in xs:
            if len(q) < n:
                heappush(q, x)
            else:
                heappushpop(q, x)
    score += sum(q)
    return score

T = int(readline())
for _ in range(T):
    N = int(readline())
    K, L, R = [], [], []
    for _ in range(N):
        k, l, r = map(int, readline().split())
        K.append(k)
        L.append(l)
        R.append(r)
    print(main(N, K, L, R))

Fixed submission:
import sys
from heapq import heappush, heappop, heappushpop

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main(N, K, L, R):
    left = [[] for _ in range(N)]
    right = [[] for _ in range(N)]
    score = 0
    for k, l, r in zip(K, L, R):
        k -= 1
        if l < r:
            score += l
            right[k].append(r - l)
        else:
            score += r
            left[k].append(l - r)
    q = []
    for n, xs in enumerate(left, 1):
        for x in xs:
            if len(q) < n:
                heappush(q, x)
            else:
                heappushpop(q, x)
    score += sum(q)
    q = []
    for n, xs in enumerate(right[::-1][1:], 1):
        for x in xs:
            if len(q) < n:
                heappush(q, x)
            else:
                heappushpop(q, x)
    score += sum(q)
    return score

T = int(readline())
for _ in range(T):
    N = int(readline())
    K, L, R = [], [], []
    for _ in range(N):
        k, l, r = map(int, readline().split())
        K.append(k)
        L.append(l)
        R.append(r)
    print(main(N, K, L, R))
Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W, K = map(int, readline().split())
C = np.frombuffer(read(), 'S1').reshape(H, -1)[:, :W]

C = 1 * (C == b'#')

def f(C, x, y):
    rows = [bool(x & (1<<i)) for i in range(H)]
    cols = [bool(y & (1<<i)) for i in range(W)]
    D = C[rows, cols]
    return D.sum() == K

ans = 0
for x in range(1 << H):
    for y in range(1 << W):
        ans += f(C, x, y)
print(ans)


Fixed submission:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W, K = map(int, readline().split())
C = np.frombuffer(read(), 'S1').reshape(H, -1)[:, :W]

C = 1 * (C == b'#')

def f(C, x, y):
    D = C.copy()
    for i in range(H):
        if x & (1 << i):
            D[i] = 0
    for i in range(W):
        if y & (1 << i):
            D[:, i] = 0
    return D.sum() == K

ans = 0
for x in range(1 << H):
    for y in range(1 << W):
        ans += f(C, x, y)
print(ans)