Buggy python submission (verdict: Runtime Error):
import heapq as hq

T = int(input())
for _ in range(T):
    N = int(input())
    camels = [list(map(int, input().split())) for _ in range(N)]
    base = 0
    camels_L = [[] for _ in range(N+1)]
    camels_R = [[] for _ in range(N+1)]
    # 左から詰めるラクダと右から詰めるラクダを仕分ける
for camel in camels:
    if camel[1] >= camel[2]:
        camels_L[camel[0]].append(camel[1] - camel[2])
        base += camel[2] # LとRの低いほうを加算
    else:
        # Rラクダは右から何列目かを考えるのでN - camel[0]と反転する
        camels_R[N - camel[0]].append(camel[2] - camel[1])
        base += camel[1] # LとRの低いほうを加算

diff = 0

# Lラクダについて考える
heap_camels = []
hq.heapify(heap_camels)
for i in range(1, N+1):
    # 左からi番目までが限度のラクダをまとめて追加
    for camel in camels_L[i]:
        hq.heappush(heap_camels, camel)
        # 左からi番目までに入りきらない場合に低いものから除外
    while len(heap_camels) > i:
        hq.heappop(heap_camels)
        # heapqに残ったものがすべての条件を満たしうれしさが最大
    diff += sum(heap_camels)

        # Rラクダについても同様
heap_camels = []
hq.heapify(heap_camels)
for i in range(1, N+1):
    for camel in camels_R[i]:
        hq.heappush(heap_camels, camel)
    while len(heap_camels) > i:
        hq.heappop(heap_camels)
diff += sum(heap_camels)
print(diff + base)


Fixed submission:
def popcount(n):
    return bin(n).count('1')
    
N=int(input())
X=input()
xl=list(X)
X=int(X,2)
a=popcount(X)
#1が減った時
if a==1:
    xd=0
else:
    xd=X%(a-1)
#1が増えたとき
xi=X%(a+1)

for i in range(N):
    if xl[i]=='1':
        if a==1:
            print(0)
            continue
        else:
            k=pow(2,N-1-i,a-1)
            x=(xd-k)%(a-1)
    else:
        k=pow(2,N-1-i,a+1)
        x=(xi+k)%(a+1)
    count=1
    while x!=0:
        x%=popcount(x)
        count+=1
    print(count)

Buggy python submission (verdict: Runtime Error):
a = [input() for i in range(2)]
def popcount(n):
    count=0
    while n!=0:
        if n%2==1:
            count+=1
        n=int(n/2)
    return count

def f(n):
    c=0
    while n!=0:
        n=int(n%popcount(n))
        c+=1
    print(c)

b=[]
for i in range(int(a[0])):
    k=0
    b=list(a[1])
    if b[i]=='0':
        b[i]='1'
    else :
        b[i]='0'
    for j in range(int(a[0])):
        k = k*2 + int(b[j])
    f(k)

Fixed submission:
def popcount(n):
    return bin(n).count('1')
    
N=int(input())
X=input()
xl=list(X)
X=int(X,2)
a=popcount(X)
#1が減った時
if a==1:
    xd=0
else:
    xd=X%(a-1)
#1が増えたとき
xi=X%(a+1)

for i in range(N):
    if xl[i]=='1':
        if a==1:
            print(0)
            continue
        else:
            k=pow(2,N-1-i,a-1)
            x=(xd-k)%(a-1)
    else:
        k=pow(2,N-1-i,a+1)
        x=(xi+k)%(a+1)
    count=1
    while x!=0:
        x%=popcount(x)
        count+=1
    print(count)

Buggy python submission (verdict: Runtime Error):
a = [input() for i in range(2)]
def popcount(n):
    count=0
    while n!=0:
        if n%2==1:
            count+=1
        n=int(n/2)
    return count

def f(n):
    c=0
    while n!=0:
        n=int(n%popcount(n))
        c+=1
    print(c)

b=[]
for i in range(int(a[0])):
    k=0
    b=list(a[1])
    if b[i]=='0':
        b[i]='1'
    else :
        b[i]='0'
    for j in range(int(a[0])):
        k = k*2 + int(b[j])
    f(k)

Fixed submission:
def popcount(n):
    return bin(n).count('1')
    
N=int(input())
X=input()
xl=list(X)
X=int(X,2)
a=popcount(X)
#1が減った時
if a==1:
    xd=0
else:
    xd=X%(a-1)
#1が増えたとき
xi=X%(a+1)

for i in range(N):
    if xl[i]=='1':
        if a==1:
            print(0)
            continue
        else:
            k=pow(2,N-1-i,a-1)
            x=(xd-k)%(a-1)
    else:
        k=pow(2,N-1-i,a+1)
        x=(xi+k)%(a+1)
    count=1
    while x!=0:
        x%=popcount(x)
        count+=1
    print(count)

Buggy python submission (verdict: Runtime Error):
a = [input() for i in range(2)]
def popcount(n):
    count=0
    while n!=0:
        if n%2==1:
            count+=1
        n=int(n/2)
    return count

def f(n):
    c=0
    while n!=0:
        n=int(n%popcount(n))
        c+=1
    print(c)

b=[]
for i in range(int(a[0])):
    k=0
    b=list(a[1])
    if b[i]=='0':
        b[i]='1'
    else :
        b[i]='0'
    for j in range(int(a[0])):
        k = k*2 + int(b[j])
    f(k)

Fixed submission:
def popcount(n):
    return bin(n).count('1')
    
N=int(input())
X=input()
xl=list(X)
X=int(X,2)
a=popcount(X)
#1が減った時
if a==1:
    xd=0
else:
    xd=X%(a-1)
#1が増えたとき
xi=X%(a+1)

for i in range(N):
    if xl[i]=='1':
        if a==1:
            print(0)
            continue
        else:
            k=pow(2,N-1-i,a-1)
            x=(xd-k)%(a-1)
    else:
        k=pow(2,N-1-i,a+1)
        x=(xi+k)%(a+1)
    count=1
    while x!=0:
        x%=popcount(x)
        count+=1
    print(count)