Harryxun/testdata huggingface dataset:
Buggy python 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])


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 python submission (verdict: Runtime Error):
INFTY = 10**9+7
N,M = map(int,input().split())
A = []
for _ in range(M):
    A.append(int(input()))
dp = [0 for _ in range(N+1)]
dp[0] = 1
dp[1] = 1
cur = 0
if A[0]==1:
    dp[1] = 0
    cur = 1
for i in range(2,N+1):
    if cur<M and i==A[cur]:
        cur += 1
    else:
        dp[i] = (dp[i-1]+dp[i-2])%INFTY
print(dp[N])

Fixed submission:
INFTY = 10**9+7
N,M = map(int,input().split())
A = []
for _ in range(M):
    A.append(int(input()))
dp = [0 for _ in range(N+1)]
dp[0] = 1
dp[1] = 1
cur = 0
if M>0 and A[0]==1:
    dp[1] = 0
    cur = 1
for i in range(2,N+1):
    if M>0 and cur<M and i==A[cur]:
        cur += 1
    else:
        dp[i] = (dp[i-1]+dp[i-2])%INFTY
print(dp[N])

Buggy python submission (verdict: Runtime Error):
INFTY = 10**9+7
N,M = map(int,input().split())
A = [int(input()) for _ in range(M)]
dp = [0 for _ in range(N+1)]
dp[0] = 1
dp[1] = 1
cur = 0
if A[0]==1:
    dp[1] = 0
    cur = 1
for i in range(2,N+1):
    if cur<M and i==A[cur]:
        cur += 1
    else:
        dp[i] = (dp[i-1]+dp[i-2])%INFTY
print(dp[N])

Fixed submission:
INFTY = 10**9+7
N,M = map(int,input().split())
A = []
for _ in range(M):
    A.append(int(input()))
dp = [0 for _ in range(N+1)]
dp[0] = 1
dp[1] = 1
cur = 0
if M>0 and A[0]==1:
    dp[1] = 0
    cur = 1
for i in range(2,N+1):
    if M>0 and cur<M and i==A[cur]:
        cur += 1
    else:
        dp[i] = (dp[i-1]+dp[i-2])%INFTY
print(dp[N])
Buggy python submission (verdict: Time Limit Exceeded):
p = 10**9+7
L = input().strip()
N = len(L)
C = [0 for _ in range(N)]
for i in range(1,N):
    if L[i-1]=="1":
        C[i] = C[i-1]+1
    else:
        C[i] = C[i-1]
tot = 0
for i in range(N):
    if L[i]=="1":
        tot = (tot+3**(N-1-i)*2**C[i])%p
if L[N-1]=="0":
    tot = (tot+2**C[N-1])%p
else:
    tot = (tot+2**(C[N-1]+1))%p
print(tot)

Fixed submission:
p = 10**9+7
def pow(x,m):
    if m==0:
        return 1
    if m==1:
        return x
    if m%2==0:
        return (pow(x,m//2)**2)%p
    else:
        return (x*pow(x,(m-1)//2)**2)%p
L = input().strip()
N = len(L)
C = [0 for _ in range(N)]
for i in range(1,N):
    if L[i-1]=="1":
        C[i] = C[i-1]+1
    else:
        C[i] = C[i-1]
tot = 0
for i in range(N):
    if L[i]=="1":
        tot = (tot+pow(3,N-1-i)*pow(2,C[i]))%p
if L[N-1]=="0":
    tot = (tot+pow(2,C[N-1]))%p
else:
    tot = (tot+pow(2,C[N-1]+1))%p
print(tot)

Buggy python submission (verdict: Time Limit Exceeded):
p = 10**9+7
def pow(x,m):
    if m==0:
        return 1
    if m==1:
        return x
    if m%2==0:
        return (pow(x,m//2)**2)%p
    else:
        return (x*pow(x,(m-1)//2)**2)%p
def f(i):
    tot = 0
    for k in range(i+1):
        tot = (tot + (((2**k)%p)*A[i]*B[k]*B[i-k])%p)%p
    return tot
L = input().strip()
N = len(L)
A = [1 for _ in range(N)]
for i in range(2,N):
    A[i] = (i*A[i-1])%p
B = [1 for _ in range(N)]
B[N-1] = pow(A[N-1],p-2)
for i in range(N-2,1,-1):
    B[i] = (B[i+1]*(i+1))%p
C = [0 for _ in range(N)]
for i in range(1,N):
    if L[i-1]=="1":
        C[i] = C[i-1]+1
    else:
        C[i] = C[i-1]
tot = 0
for i in range(N):
    if L[i]=="1":
        tot = (tot+f(N-1-i)*2**C[i])%p
if L[N-1]=="0":
    tot = (tot+2**C[N-1])%p
else:
    tot = (tot+2**(C[N-1]+1))%p
print(tot)

Fixed submission:
p = 10**9+7
def pow(x,m):
    if m==0:
        return 1
    if m==1:
        return x
    if m%2==0:
        return (pow(x,m//2)**2)%p
    else:
        return (x*pow(x,(m-1)//2)**2)%p
L = input().strip()
N = len(L)
C = [0 for _ in range(N)]
for i in range(1,N):
    if L[i-1]=="1":
        C[i] = C[i-1]+1
    else:
        C[i] = C[i-1]
tot = 0
for i in range(N):
    if L[i]=="1":
        tot = (tot+pow(3,N-1-i)*pow(2,C[i]))%p
if L[N-1]=="0":
    tot = (tot+pow(2,C[N-1]))%p
else:
    tot = (tot+pow(2,C[N-1]+1))%p
print(tot)
Buggy python submission (verdict: Time Limit Exceeded):
s = input().strip()
x = s[0]
for i in range(1,len(s)):
    if s[i]=="C" and s[i-1]=="B":
        x = x[:-1]+"T"
    else:
        x += s[i]
cnt = 0
ca = 0
for i in range(len(x)):
    if x[i]=="A":
        ca += 1
    elif x[i]=="T":
        cnt += ca
    elif x[i]=="B" or x[i]=="C":
        ca = 0
print(cnt) 

Fixed submission:
s = input().strip()
x = [s[0]]
for i in range(1,len(s)):
    if s[i]=="C" and s[i-1]=="B":
        x.pop()
        x.append("T")
    else:
        x.append(s[i])
cnt = 0
ca = 0
for i in range(len(x)):
    if x[i]=="A":
        ca += 1
    elif x[i]=="T":
        cnt += ca
    elif x[i]=="B" or x[i]=="C":
        ca = 0
print(cnt)
Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
B = [list(map(int,input().split())) for _ in range(M)]
B = sorted(B,key=lambda x:x[1],reverse=True)
cur = 0
cnt = 0
for i in range(N):
    if cnt==B[cur][0]:
        cur +=1
        cnt = 0
    if B[cur][1]<=A[i]:
        break
    A[i] = B[cur][1]
    cnt += 1
print(sum(A))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
B = [list(map(int,input().split())) for _ in range(M)]
B = sorted(B,key=lambda x:x[1],reverse=True)
cur = 0
cnt = 0
for i in range(N):
    if cnt==B[cur][0]:
        cur +=1
        if cur==M:break
        cnt = 0
    if B[cur][1]<=A[i]:
        break
    A[i] = B[cur][1]
    cnt += 1
print(sum(A))
Buggy python 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)


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 python 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)

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 python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
A = [input().strip() for _ in range(H)]
B = [[0 for _ in range(W)] for _ in range(H)]
que = deque([])
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            que.append((i,j,0))
            B[i][j] = 1
dmax = 0
while que:
    i,j,d = que.popleft()
    if j+1<W and B[i][j+1]==0:
        B[i][j+1]=d+1
        dmax = max(dmax,d+1)
        que.append((i,j+1,d+1))
    if i-1>=0 and B[i-1][j]==0:
        B[i-1][j]=d+1
        dmax = max(dmax,d+1)
        que.append((i-1,j,d+1))
    if j-1>=0 and B[i][j-1]==0:
        B[i][j-1]=d+1
        dmax = max(dmax,d+1)
        que.append((i,j-1,d+1))
    if i+1<H and B[i+1][j]==0:
        B[i+1][j]=d+1
        dmax = max(dmax,d+1)
        que.append((i+1,j,d+1))
print(dmax)

Fixed submission:
from collections import deque
H,W = map(int,input().split())
A = [list(input().strip()) for _ in range(H)]
que = deque([])
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            que.append((i,j,0))
tot = 0
while que:
    i,j,cnt = que.popleft()
    if j+1<W and A[i][j+1]==".":
        A[i][j+1]="#"
        que.append((i,j+1,cnt+1))
        tot = cnt+1
    if j-1>-1 and A[i][j-1]==".":
        A[i][j-1]="#"
        que.append((i,j-1,cnt+1))
        tot = cnt+1
    if i+1<H and A[i+1][j]==".":
        A[i+1][j]="#"
        que.append((i+1,j,cnt+1))
        tot = cnt+1
    if i-1>-1 and A[i-1][j]==".":
        A[i-1][j]="#"
        que.append((i-1,j,cnt+1))
        tot = cnt+1
print(tot)
Buggy python 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))

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 python 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)

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 python 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])

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 python 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)

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 python submission (verdict: Time Limit Exceeded):
from itertools import combinations
p = 10**9+7
N = int(input())
S = input().strip()
C = {}
for i in range(N):
    if S[i] not in C:
        C[S[i]]=0
    C[S[i]] += 1
D = list(C.items())
n = len(D)
cnt = 0
for i in range(1,n+1):
    for x in combinations(range(n),i):
        prod = 1
        for j in x:
            prod = (prod*D[j][1])%p
        cnt = (cnt+prod)%p
print(cnt)

Fixed submission:
p = 10**9+7
N = int(input())
S = input().strip()
C = {chr(i):[] for i in range(97,123)}
for i in range(N):
    C[S[i]].append(i)
cnt = 1
for j in range(97,123):
    cnt = (cnt*(1+len(C[chr(j)])))%p
print((cnt-1)%p)
Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left
p = 10**9+7
N = int(input())
A = []
for i in range(N):
    a = int(input())
    if len(A)==0:
        A.append(a)
    else:
        if a==A[-1]:continue
        else:
            A.append(a)
N = len(A)
C = {}
for i in range(N):
    c = A[i]
    if c not in C:
        C[c] = []
    C[c].append(i)
dp = [1 for _ in range(N)]
for i in range(1,N):
    dp[i] = dp[i-1]
    a = A[i]
    ind = bisect_left(C[a],i)
    if ind>0:
        for j in range(ind-1,-1,-1):
            if C[a][j]>=1:
                dp[i] = (dp[i]+dp[C[a][j]-1])%p
            else:
                dp[i] = dp[i]+1
print(dp[N-1])

Fixed submission:
from bisect import bisect_left
p = 10**9+7
N = int(input())
C = [int(input()) for _ in range(N)]
A = [0 for _ in range(N)]
B = {i:[0,-1] for i in range(1,2*10**5+1)}
for i in range(N):
    c = C[i]
    if B[c][-1]==i-1:
        A[i] = 0
    else:
        A[i] = 1
    B[c].append(i)
dp = [0 for _ in range(N+1)]
dp[1] = 1
for i in range(1,N):
    if A[i]==1:
        ind = bisect_left(B[C[i]],i)
        j = B[C[i]][ind-1]
        dp[i+1] = (dp[i]+dp[j+1])%p
    else:
        dp[i+1] = dp[i]
print(dp[N])
Buggy python 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)

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 python 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)

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 python 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])

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 python submission (verdict: Time Limit Exceeded):
from collections import deque
N,M = map(int,input().split())
T = {}
for _ in range(N+M-1):
    a,b = map(int,input().split())
    if a not in T:
        T[a] = [[],[]]
    T[a][1].append(b)
    if b not in T:
        T[b] = [[],[]]
    T[b][0].append(a)
for i in range(1,N+1):
    if len(T[i][0])==0:
        rt = i
        break
dist = [-1 for _ in range(N+1)]
que = deque([(rt,0)])
dist[rt] = 0
while que:
    a,d = que.popleft()
    for x in T[a][1]:
        dist[x]=d+1
        que.append((x,d+1))
pa = {i:0 for i in range(1,N+1)}
for i in range(1,N+1):
    if i!=rt:
        for x in T[i][0]:
            if dist[x]==dist[i]-1:
                pa[i] = x
                break
for i in range(1,N+1):
    print(pa[i])

Fixed submission:
from collections import deque
N,M = map(int,input().split())
G = {i:[0,[]] for i in range(1,N+1)}
for _ in range(N+M-1):
    a,b = map(int,input().split())
    G[a][1].append(b)
    G[b][0] += 1
hist = [0 for _ in range(N+1)]
for i in range(1,N+1):
    if G[i][0]==0:
        cur = i
        break
que = deque([cur])
hist[cur] = 1
T = [-1 for _ in range(N+1)]
T[cur] = 0
while que:
    i = que.popleft()
    for j in G[i][1]:
        if hist[j]==0:
            G[j][0] -= 1
        if G[j][0]==0:
            que.append(j)
            T[j] = i
            hist[j] = 1
for i in range(1,N+1):
    print(T[i])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N,M = map(int,input().split())
T = {}
Tr = {}
for _ in range(N+M-1):
    a,b = map(int,input().split())
    if a not in T:
        T[a] = [0,[]]
    T[a][1].append(b)
    if b not in T:
        T[b] = [0,[]]
    T[b][0] += 1
    if b not in Tr:
        Tr[b] = []
    Tr[b].append(a)
for i in range(1,N+1):
    if T[i][0]==0:
        rt = i
        break
dist = [-1 for _ in range(N+1)]
que = deque([(rt,0)])
dist[rt] = 0
while que:
    a,d = que.popleft()
    for x in T[a][1]:
        dist[x]=d+1
        que.append((x,d+1))
pa = {i:0 for i in range(1,N+1)}
for i in range(1,N+1):
    if i!=rt:
        for x in Tr[i]:
            if dist[x]==dist[i]-1:
                pa[i] = x
                break
for i in range(1,N+1):
    print(pa[i])

Fixed submission:
from collections import deque
N,M = map(int,input().split())
G = {i:[0,[]] for i in range(1,N+1)}
for _ in range(N+M-1):
    a,b = map(int,input().split())
    G[a][1].append(b)
    G[b][0] += 1
hist = [0 for _ in range(N+1)]
for i in range(1,N+1):
    if G[i][0]==0:
        cur = i
        break
que = deque([cur])
hist[cur] = 1
T = [-1 for _ in range(N+1)]
T[cur] = 0
while que:
    i = que.popleft()
    for j in G[i][1]:
        if hist[j]==0:
            G[j][0] -= 1
        if G[j][0]==0:
            que.append(j)
            T[j] = i
            hist[j] = 1
for i in range(1,N+1):
    print(T[i])