Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02918 (python)

Fixed submission:
N,K = map(int,input().split())
S = list(input())
a = S[0]
cur = 0
cnt = 0
for i in range(N):
    if S[i] != a:
        cnt += i-cur-1
        a = S[i]
        cur = i
if S[N-1]==a:
    cnt += N-cur-1
print(min(cnt+2*K,N-1))

Buggy submission (verdict: Runtime Error):
N = int(input())
B = list(map(int,input().split()))
cnt = B[0]+B[N-2]
for i in range(1,N-1):
    cnt += min(B[i-1],B[i])
print(cnt)
Problem p02937 (python)

Fixed submission:
from bisect import bisect_right
s = input().strip()
t = input().strip()
D = {}
for i in range(len(s)):
    a = s[i]
    if a not in D:
        D[a] = []
    D[a].append(i+1)
A = sorted(list(set(list(s))))
A.append(chr(123))
B = sorted(list(set(list(t))))
cur = 0
flag = 0
for i in range(len(B)):
    while True:
        if A[cur]<B[i]:
            cur += 1
        elif A[cur]==B[i]:
            break
        else:
            flag = 1
            break
    if flag==1:break
if flag==1:
    print(-1)
else:
    C = []
    cur = -1
    for i in range(len(t)):
        b = t[i]
        ind = bisect_right(D[b],cur)%len(D[b])
        C.append(D[b][ind])
        cur = D[b][ind]
    cnt = 0
    for j in range(1,len(C)):
        if C[j]<=C[j-1]:
            cnt += 1
    print(len(s)*cnt+C[-1])

Buggy submission (verdict: Runtime Error):
from bisect import bisect_right
s = input().strip()
t = input().strip()
A = sorted(list(set(list(s))))
B = sorted(list(set(list(t))))
D = {}
for i in range(len(s)):
    a = s[i]
    if a not in D:
        D[a] = []
    D[a].append(i+1)
cur = 0
for i in range(len(B)):
    flag = 0
    while cur<len(A):
        if A[cur]<B[i]:
            cur += 1
        elif A[cur]==B[i]:
            break
        else:
            flag = 1
            break
    if flag==1:break
if flag==1:
    print(-1)
else:
    C = []
    cur = -1
    for i in range(len(t)):
        b = t[i]
        ind = bisect_right(D[b],cur)%len(D[b])
        C.append(D[b][ind])
        cur = D[b][ind]
    cnt = 0
    for j in range(1,len(C)):
        if C[j]<=C[j-1]:
            cnt += 1
    print(len(s)*cnt+C[-1])

Buggy submission (verdict: Runtime Error):
from bisect import bisect_right
s = input().strip()
t = input().strip()
A = sorted(list(set(list(s))))
B = sorted(list(set(list(t))))
D = {}
for i in range(len(s)):
    a = s[i]
    if a not in D:
        D[a] = []
    D[a].append(i+1)
cur = 0
for i in range(len(B)):
    flag = 0
    while cur<len(A):
        if A[cur]<B[i]:
            cur += 1
        elif A[cur]==B[i]:
            break
        else:
            flag = 1
            break
if flag==1:
    print(-1)
else:
    C = []
    cur = -1
    for i in range(len(t)):
        b = t[i]
        ind = bisect_right(D[b],cur)%len(D[b])
        C.append(D[b][ind])
        cur = D[b][ind]
    cnt = 0
    for j in range(1,len(C)):
        if C[j]<=C[j-1]:
            cnt += 1
    print(len(s)*cnt+C[-1])
Problem p02975 (python)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
B = set(A)
C = list(B)
k = len(C)
if N%3==0:
    if k==3 and C[0]^C[1]==C[2] and A.count(C[0])==A.count(C[1]) and A.count(C[0])==A.count(C[2]):
        print("Yes")
    elif k==2:
        C = sorted(C)
        if C[0]==0 and A.count(C[0])==N//3:
            print("Yes")
        else:
            print("No")
    elif k==1 and C[0] == 0:
        print("Yes")
    else:
        print("No")
else:
    if k==1 and C[0] ==0:
        print("Yes")
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
B = [A[0],A[1],A[0]^A[1]]
flag = 0
for i in range(2,N):
    if A[i] not in C:
        flag = 1
        break
if flag==1:
    print("No")
else:
    if N%3==0:
        if A[0]==A[1]==0:
            print("Yes")
        elif A[0]==0 or A[1]==0:
            cnt = 0
            for i in range(N):
                if A[i]==0:
                    cnt += 1
            if cnt==N//3:
                print("Yes")
            else:
                print("No")
        else:
            C = {A[0]:0,A[1]:0,A[0]^A[1]:0}
            for i in range(N):
                C[A[i]] += 1
            if C[A[0]]==C[A[1]]==C[A[0]^A[1]]:
                print("Yes")
            else:
                print("No")
    else:
        if A[0]==A[1]==0:
            print("Yes")
        else:
            print("No")
Problem p03000 (python)

Fixed submission:
import bisect
N,X = map(int,input().split())
L = list(map(int,input().split()))
D = [0 for _ in range(N+1)]
for i in range(1,N+1):
    D[i] = D[i-1]+L[i-1]
ind = bisect.bisect_right(D,X)    
print(ind)

Buggy submission (verdict: Runtime Error):
N,X = map(int,input().split())
L = list(map(int,input().split()))
cnt = 0
i = 0
cnt += L[i]
while cnt<=X:
    i += 1
    cnt += L[i]
print(i+1)
Problem p03006 (python)

Fixed submission:
import itertools
N = int(input())
X = [list(map(int,input().split())) for _ in range(N)]
if N==1:
    cmin = 1
else:
    cmin = 100
    for y in itertools.combinations(X,2):
        x1,y1 = y[0]
        x2,y2 = y[1]
        p = x2-x1
        q = y2-y1
        cnt0 = 0
        cnt1 = 0
        for i in range(N):
            x,y = X[i]
            if [x+p,y+q] in X:
                cnt0 += 1
            if [x-p,y-q] in X:
                cnt1 += 1
        cnt = max(cnt0,cnt1)
        cmin = min(cmin,N-cnt)
print(cmin)

Buggy submission (verdict: Runtime Error):
from itertools import combinations
N = int(input())
X = [list(map(int,input().split())) for _ in range(N)]
C = {}
for z in combinations(X,2):
    x1,x2 = z
    a = x1[0]-x2[0]
    b = x1[1]-x2[1]
    if a>=0 and b>=0:
        if (a,b) not in C:
            C[(a,b)] = 0
        C[(a,b)] += 1
    elif a>=0 and b<0:
        if (a,b) not in C:
            C[(a,b)] = 0
        C[(a,b)] += 1
    elif a<0 and b>=0:
        if (-a,-b) not in C:
            C[(-a,-b)] = 0
        C[(-a,-b)] += 1
    elif a<0 and b<0:
        if (-a,-b) not in C:
            C[(-a,-b)] = 0
        C[(-a,-b)] += 1
C = sorted(list(C.items()),key=lambda x:x[1],reverse=True)
k = C[0][1]
print(N-k)
Problem p03007 (python)

Fixed submission:
from collections import deque
N = int(input())
A = sorted(list(map(int,input().split())))
A = deque(A)
C = []
while len(A)>1 and A[-1]<=0:
    a = A.pop()
    b = A.pop()
    C.append((a,b))
    A.append(a-b)
if len(A)==1:
    x = A[0]
else:
    B = deque(list(A)[:-1])
    while B[-1]>0:
        a = B.popleft()
        b = B.pop()
        C.append((a,b))
        B.appendleft(a-b)
    B.append(A[-1])
    while len(B)>1:
        a = B.pop()
        b = B.popleft()
        C.append((a,b))
        B.append(a-b)
    x = B[0]
print(x)
for i in range(N-1):
    print(C[i][0],C[i][1])

Buggy submission (verdict: Runtime Error):
from collections import deque
N = int(input())
A = sorted(list(map(int,input().split())))
A = deque(A)
C = []
if A[-1]<=0:
    a = A.pop()
    b = A.pop()
    C.append((a,b))
    A.append(a-b)
B = deque(list(A)[:-1])
while B[-1]>0:
    a = B.popleft()
    b = B.pop()
    C.append((a,b))
    B.appendleft(a-b)
B.append(A[-1])
while len(B)>1:
    a = B.pop()
    b = B.popleft()
    C.append((a,b))
    B.append(a-b)
print(a-b)
for i in range(N-1):
    print(C[i][0],C[i][1])
Problem p03044 (python)

Fixed submission:
from collections import deque
N = int(input())
G = {}
for _ in range(N-1):
    u,v,w = map(int,input().split())
    if u not in G:
        G[u] = []
    G[u].append((v,w%2))
    if v not in G:
        G[v] = []
    G[v].append((u,w%2))
que = deque([(1,0)])
hist = [0 for _ in range(N+1)]
hist[1] = 1
col = [0 for _ in range(N+1)]
while que:
    u,c = que.popleft()
    for x in G[u]:
        v,w = x[0],x[1]
        if hist[v]==0:
            if w==0:
                col[v] = c
                que.append((v,c))
            else:
                col[v] = 1-c
                que.append((v,1-c))
            hist[v] = 1
for i in range(1,N+1):
    print(col[i])

Buggy submission (verdict: Runtime Error):
def clr(u):
    for x in G[u]:
        v,w = x[0],x[1]
        if hist[v]==0:
            if w%2==0:
                C[v] = C[u]
            else:
                C[v] = 1-C[u]
            hist[v]=1
            clr(v)      
N = int(input())
G = {i:[] for i in range(1,N+1)}
for _ in range(N-1):
    u,v,w = map(int,input().split())
    G[u].append((v,w))
    G[v].append((u,w))
C = [0 for i in range(N+1)]
hist = [0 for i in range(N+1)]
hist[1] = 1
clr(1)
for i in range(1,N+1):
    print(C[i])

Buggy submission (verdict: Runtime Error):
def col(i):
    for x in G[i]:
        v,w = x[0],x[1]
        if hist[v]==0:
            if w%2==0:
                C[v]=C[i]
            else:
                C[v]=1-C[i]
            hist[v] = 1
            col(v)
N = int(input())
G = {i:[] for i in range(1,N+1)}
for _ in range(N-1):
    u,v,w = map(int,input().split())
    G[u].append((v,w))
    G[v].append((u,w))
C = {i:0 for i in range(1,N+1)}
hist = [0 for i in range(N+1)]
hist[1] = 1
col(1)
for i in range(1,N+1):
    print(C[i])

Buggy submission (verdict: Runtime Error):
def col(i):
    for x in G[i]:
        v,w = x[0],x[1]
        if hist[v]==0:
            if w%2==0:
                C[v]=C[i]
            else:
                C[v]=1-C[i]
            hist[v] = 1
            col(v)
N = int(input())
G = {i:[] for i in range(1,N+1)}
for _ in range(N-1):
    u,v,w = map(int,input().split())
    G[u].append((v,w))
    G[v].append((u,w))
C = {i:0 for i in range(1,N+1)}
hist = [0 for i in range(N+1)]
hist[1] = 1
col(1)
for i in range(1,N+1):
    print(C[i])
Problem p03049 (python)

Fixed submission:
N = int(input())
C = {0:0,1:0,2:0}
cnt = 0
for _ in range(N):
    s = input().strip()
    if s[0]=="B" and s[-1]!="A":
        C[0] += 1
    elif s[0]!="B" and s[-1]=="A":
        C[1] += 1
    elif s[0]=="B" and s[-1]=="A":
        C[2] += 1
    for i in range(len(s)-1):
        if s[i:i+2]=="AB":
            cnt += 1
if C[2]>0:
    cnt += C[2]-1
    if C[0]>0:
        cnt += 1
        C[0] -= 1
    if C[1]>0:
        cnt += 1
        C[1] -= 1
cnt += min(C[0],C[1])
print(cnt)

Buggy submission (verdict: Runtime Error):
N = int(input())
C = {0:0,1:0,2:0}
cnt = 0
for _ in range(N):
    s = input().strip()
    if s[0]=="B" and s[-1]!="A":
        C[0] += 1
    elif s[0]!=B and s[-1]=="A":
        C[1] += 1
    elif s[0]=="B" and s[-1]=="A":
        C[2] += 1
    for i in range(len(s)-1):
        if s[i:i+2]=="AB":
            cnt += 1
if C[2]>0:
    cnt += C[2]-1
    if C[0]>0:
        cnt += 1
        C[0] -= 1
    if C[1]>0:
        cnt += 1
        C[1] -= 1
cnt += min(C[0],C[1])
print(cnt)

Buggy submission (verdict: Runtime Error):
N = int(input())
C = {0:0,1:0,2:0}
cnt = 0
for _ in range(N):
    s = input().strip()
    if s[0]=="B" and s[-1]!="A":
        C[0] += 1
    elif s[0]!=B and s[-1]=="A":
        C[1] += 1
    elif s[0]=="B" and s[-1]=="A":
        C[2] += 1
    for i in range(len(s)-1):
        if s[i:i+2]=="AB":
            cnt += 1
if C[2]>0:
    cnt += C[2]-1
    if C[0]>0:
        cnt += 1
        C[0] -= 1
    if C[1]>0:
        cnt += 1
        C[1] -= 1
cnt += min(C[0],C[1])
print(cnt)
Problem p03061 (python)

Fixed submission:
def gcd(x,y):
    while y>0:
        x,y = y,x%y
    return x

N = int(input())
A = sorted(list(map(int,input().split())))
amin = A[0]
ind = 0
for i in range(1,N):
    a = gcd(amin,A[i])
    if a<amin:
        ind = i
        amin = a
a0 = A[1]
for i in range(2,N):
    a0 = gcd(a0,A[i])
a1 = A[0]
for i in range(2,N):
    a1 = gcd(a1,A[i])
a = A[0]
for i in range(1,N):
    if i != ind:
        a = gcd(a,A[i])
print(max(a0,a1,a))

Buggy submission (verdict: Runtime Error):
def gcd(x,y):
    if x<y:
        x,y = y,x
    while y>0:
        x,y = y,x%y
    return x
N = int(input())
A = list(map(int,input().split()))
cmin = 10**9
a = A[0]
for i in range(1,N):
    b = A[i]
    a = gcd(a,b)
    if a<cmin:
        ind = i
        cmin = a
a = A[(ind+1)%N]
cmin = 10**9
for i in range(N):
    if i==ind:continue
    b = A[i]
    a = gcd(a,b)
    cmin = min(cmin,a)
a = A[1]
dmin = 10**9
for i in range(2,N):
    b = A[i]
    a = gcd(a,b)
    dmin = min(dmin,a)
print(max(cmin,dmin))

Buggy submission (verdict: Runtime Error):
INFTY = 10**10
def gcd(x,y):
    if y>x:
        x,y = y,x
    while y>0:
        x,y = y,x%y
    return x
N = int(input())
A = list(map(int,input().split()))
cmin = INFTY
MIN = []
for i in range(N-1):
    c = gcd(A[i],A[i+1])
#     print("cmin,c,A[i],A[i+1]={},{},{},{}".format(cmin,c,A[i],A[i+1]))
    if c<cmin:
        cmin = c
        MIN = [(A[i],A[i+1])]
    elif c==cmin:
        MIN.append((A[i],A[i+1]))
c = gcd(A[N-1],A[0])
if c<cmin:
    MIN = [(A[N-1],A[0])]
elif c==cmin:
    MIN.append((A[N-1],A[0]))
if len(MIN)==2:
    x,y,z,w = MIN[0][0],MIN[0][1],MIN[1][0],MIN[1][1]
    if x==y and y==z and z==w:
        emin = x
    else:
        B = [x,y,z,w]
        for a in B:
            if B.count(a)==2:
                b = a
                break
        C = A[:]
        C.remove(b)
        emin = INFTY
        for i in range(N-2):
            emin = min(emin,gcd(C[i],C[i+1]))
elif len(MIN)==1:
    x,y = MIN[0][0],MIN[0][1]
    B = A[:]
    B.remove(x)
    bmin = INFTY
    for i in range(N-2):
        bmin = min(bmin,gcd(B[i],B[i+1]))
    C = A[:]
    C.remove(y)
    cmin = INFTY
    for i in range(N-2):
        cmin = min(cmin,gcd(C[i],C[i+1]))
    emin = max(bmin,cmin)
print(emin)
Problem p03078 (python)

Fixed submission:
X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = sorted(list(map(int,input().split())),reverse=True)
D = []
for i in range(X):
    for j in range(Y):
        D.append(A[i]+B[j])
D = sorted(D,reverse=True)
E = []
for i in range(min(K,X*Y)):
    for j in range(Z):
        E.append(D[i]+C[j])
E = sorted(E,reverse=True)
for i in range(K):
    print(E[i])

Buggy submission (verdict: Runtime Error):
X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
D = set()
for i in range(X):
    for j in range(Y):
        D.add(A[i]+B[j])
D = sorted(list(D),reverse=True)
C = sorted(C,reverse=True)
E = []
for i in range(min(int(K**0.5)+10,len(D))):
    for j in range(min(int(K**0.5)+10,len(C))):
        E.append(D[i]+C[j])
E = sorted(E,reverse=True)
for i in range(K):
    print(E[i])
Problem p03086 (python)

Fixed submission:
D = ["A","T","G","C"]
S = input().strip()
N = len(S)
cnt = 0
for k in range(N):
    for i in range(k+1):
        ind = 1
        for j in range(i,i+N-k):
            if S[j] not in D:
                ind = 0
                break
        if ind == 1:
            cnt = N-k
            break
    if ind==1:break
print(cnt)

Buggy submission (verdict: Runtime Error):
D = ["A","T","G","C"]
S = input().strip()
N = len(S)
for k in range(N):
    for i in range(k+1):
        ind = 1
        for j in range(i,i+N-k):
            if S[j] not in D:
                ind = 0
                break
        if ind == 1:
            cnt = N-k
            break
    if ind==1:break
print(cnt)
Problem p03103 (python)

Fixed submission:
N,M = map(int,input().split())
A = []
for _ in range(N):
    a,b = map(int,input().split())
    A.append((a,b))
A = sorted(A,key=lambda x:x[0])
B = [A[0][1]]
if A[0][1]>=M:
    cnt = A[0][0]*M
else:
    ind = 0
    for i in range(1,N):
        B.append(B[i-1]+A[i][1])
        if B[i]>=M:
            ind = i
            break
    cnt = 0
    for i in range(ind):
        cnt += A[i][0]*A[i][1]
    cnt += A[ind][0]*(M-B[ind-1])
print(cnt)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
cnt = 0
cost = 0
i = 0
while True:
    a,b = A[i]
    if cnt+b<=M:
        cnt += b
        cost += a*b
    else:break
    i += 1
if cnt<M:
    a,b = A[i]
    cost += (M-cnt)*a
print(cost)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
cnt = 0
for i in range(N):
    if cnt+A[i][1]>M:
        ind = i-1
        break
    cnt += A[i][1]
yen = 0
for i in range(ind+1):
    yen += A[i][0]*A[i][1]
yen += A[ind+1][0]*(M-cnt)
print(yen)
Problem p03128 (python)

Fixed submission:
def check(i):
    global kmax,nmax
    cnt = 0
    for j in range(i):
        cnt += K[j]*B[j]
    if sum(B[:i])+(N-cnt)//K[i]<kmax:
        return
    for k in range((N-cnt)//K[i],-1,-1):
        if cnt+K[i]*k==N:
             if kmax<=sum(B[:i])+k:
                    kmax = sum(B[:i])+k
                    B[i] = k
                    num = ""
                    for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                    num = int(num)
                    nmax = max(nmax,num)
        else:
            if i<len(K)-1:
                B[i] = k
                check(i+1)
            else:
                B[i] = 0
                return
C = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
N,M = map(int,input().split())
A = list(map(int,input().split()))
G = {}
for i in range(M):
    a = A[i]
    k = C[a]
    if k not in G:
        G[k] = 0
    if a>G[k]:
        G[k] = a
K = sorted(list(G.keys()))
D = sorted(list(G.values()),reverse=True)
B = [0 for _ in range(len(K))]
kmax = 0
nmax = 0
check(0)
print(nmax)

Buggy submission (verdict: Runtime Error):
D = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
N,M = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
A.insert(0,-1)
dp = [["" for _ in range(M+1)] for _ in range(N+1)]
for i in range(1,N+1):
    a = A[1]
    k = i//D[a]
    b = i%D[a]
    if b==0:
        x = str(a)*k
        dp[i][1] = str(int(x))
for i in range(1,N+1):
    for j in range(2,M+1):
        a = A[j]
        dp[i][j] = dp[i][j-1]
        lmax = len(dp[i][j-1])
        cnt=1
        if i-cnt*D[a]>=0 and dp[i-cnt*D[a]][j-1]!="":
            b = int(str(a)*cnt+dp[i-cnt*D[a]][j-1])
        else:
            b = 0
        kmax = 0
        for t in range(M+1):
            kmax = max(kmax,len(dp[i-cnt*D[a]][t]))
        while cnt+kmax>=lmax:
            if dp[i][j]=="":
                dpnum = 0
            else:
                dpnum = int(dp[i][j])
            dpnum = max(dpnum,b)
            if dpnum==0:
                dp[i][j]=""
            else:
                dp[i][j] = str(dpnum)
            cnt += 1
            if i>=cnt*D[a]:
                if dp[i-cnt*D[a]][j-1]!="":
                    b = int(str(a)*cnt+dp[i-cnt*D[a]][j-1])
                else:
                    b=0
                kmax = 0
                for t in range(M+1):
                    kmax = max(kmax,len(dp[i-cnt*D[a]][t]))
            else:break
        if i%D[a]==0:
            b = int(str(a)*(i//D[a]))
            dpnum = max(dpnum,b)
            dp[i][j] = str(dpnum)
print(dp[N][M])
Problem p03138 (python)

Fixed submission:
import math
N,K = map(int,input().split())
A = list(map(int,input().split()))
a1 = max(A)
n = int(math.log2(max(a1,K)))+1
B = [0 for _ in range(n)]
for i in range(N):
    a = bin(A[i])[2:]
    for j in range(len(a)):
        if a[len(a)-1-j]=="1":
            B[j] += 1
x = bin(K)[2:]
y = ""
flag = 0
for i in range(len(x)):
    if flag==0:
        if x[i]=="0":
            y += x[i]
        else:
            if B[len(x)-1-i]<=N//2:
                y += x[i]
            else:
                y += "0"
                flag = 1
    else:
        if B[len(x)-1-i]<=N//2:
            y += "1"
        else:
            y += "0"
y = int(y,2)
cnt = 0
for i in range(N):
    cnt += y^A[i]
print(cnt)

Buggy submission (verdict: Runtime Error):
import math
N,K = map(int,input().split())
A = list(map(int,input().split()))
a1 = max(A)
n = int(math.log2(max(a1,K)))+1
B = [0 for _ in range(n)]
for i in range(N):
    a = bin(A[i])[2:]
    for j in range(len(a)):
        if a[len(a)-1-j]=="1":
            B[j] += 1
x = bin(K)[2:]
y = ""
flag = 0
for i in range(len(x)):
    if flag==0:
        if x[i]=="0":
            y += x[i]
        else:
            if B[len(x)-1-i]<=N//2:
                y += x[i]
            else:
                y += "0"
                flag = 1
    else:
        if B[len(x)-1-i]<=N//2:
            y += "1"
        else:
            y += "0"
y = int(y,2)
cnt = 0
for i in range(N):
    cnt += y^A[i]
print(cnt)

Buggy submission (verdict: Runtime Error):
import math,bisect
N,K = map(int,input().split())
A = list(map(int,input().split()))
if K==0:
    print(sum(A))
else:
    A = sorted(A)
    ind = bisect.bisect_right(A,K)
    A = A[:ind]
    n2 = math.floor(math.log2(K))+1
    cnt = 0
    k = n2-1
    while k>=0:
        B = [(A[i]//(2**k))%2 for i in range(N)]
        b = sum(B)
        cnt += 2**k*max(b,N-b)
        k -= 1
    print(cnt)

Buggy submission (verdict: Runtime Error):
import math,bisect
N,K = map(int,input().split())
A = list(map(int,input().split()))
if K==0:
    print(sum(A))
else:
    A = sorted(A)
    ind = bisect.bisect_right(A,K)
    A = A[:ind]
    a = A[-1]
    n1 = math.floor(math.log2(a))+1
    n2 = math.floor(math.log2(K))+1
    cnt = 0
    k = n2-1
    while k>=0:
        B = [(A[i]//(2**k))%2 for i in range(N)]
        b = sum(B)
        cnt += 2**k*max(b,N-b)
        k -= 1
    if n1>n2:
        k = n1-1
        while k>n2-1:
            B = [(A[i]//(2**k))%2 for i in range(N)]
            b = sum(B)
            cnt += 2**k*max(b,N-b)
            k -= 1 
    print(cnt)

Buggy submission (verdict: Runtime Error):
ort math
N,K = map(int,input().split())
A = list(map(int,input().split()))
if K==0:
    print(sum(A))
else:
    A = sorted(A)
    a = A[-1]
    n1 = math.floor(math.log2(a))+1
    n2 = math.floor(math.log2(K))+1
    cnt = 0
    k = n2-1
    while k>=0:
        B = [(A[i]//(2**k))%2 for i in range(N)]
        b = sum(B)
        cnt += 2**k*max(b,N-b)
        k -= 1
    if n1>n2:
        k = n1-1
        while k>n2-1:
            B = [(A[i]//(2**k))%2 for i in range(N)]
            b = sum(B)
            cnt += 2**k*max(b,N-b)
            k -= 1 
    print(cnt)
Problem p03147 (python)

Fixed submission:
def countA(A):
    global cnt
    if len(A)==1:
        cnt += A[0]
    else:
        amin = min(A)
        A = [A[i]-amin for i in range(len(A))]
        cnt += amin
        while A:
            if A[0]==0:
                A.pop(0)
            elif A[-1]==0:
                A.pop()
            else:break
        B = []
        for i in range(1,len(A)):
            if A[i]==0 and A[i-1]==0:
                B.append(i)
        C = []
        for i in range(len(A)):
            if i not in B:
                C.append(A[i])
        C0 = []
        for i in range(len(C)):
            if C[i]==0:
                C0.append(i)
        if len(C0)==0 and len(C)>0:
            countA(C)
        else:
            cur = 0
            for c in C0:
                countA(C[cur:c])
                cur = c+1
            if cur<len(C):
                countA(C[cur:])
            
N = int(input())
H = list(map(int,input().split()))
cnt = 0
countA(H)
print(cnt)

Buggy submission (verdict: Runtime Error):
def countA(A):
    global cnt
    if len(A)==1:
        cnt += A[0]
    else:
        amin = min(A)
        A = [A[i]-amin for i in range(len(A))]
        cnt += amin
        while A:
            if A[0]==0:
                A.pop(0)
            elif A[-1]==0:
                A.pop()
            else:break
        B = []
        for i in range(1,len(A)):
            if A[i]==0 and A[i-1]==0:
                B.append(i)
        C = []
        for i in range(len(A)):
            if i not in B:
                C.append(A[i])
        C0 = []
        for i in range(len(C)):
            if C[i]==0:
                C0.append(i)
        if len(C0)==0 and len(C)>0:
            countA(C)
        else:
            cur = 0
            for c in C0:
                countA(C[cur:c])
                cur = c+1
            countA(C[cur:])
            
N = int(input())
H = list(map(int,input().split()))
cnt = 0
countA(H)
print(cnt)