Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
from itertools import combinations
N,K = map(int, input().split())
MAX_K = (N-1)*(N-2)//2
if K > MAX_K:
    print(-1);exit()
M = N-1 + MAX_K-K
print(M)
for e in combinations(range(1, N+1), 2))[:M]:
    print(*e)


Fixed submission:
from itertools import combinations

N,K = map(int, input().split())

MAX_K = (N-1)*(N-2)//2
if K > MAX_K:
    print(-1)
    exit()

M = N-1 + (MAX_K - K)
print(M)
edges = list(combinations(range(1, N+1), 2))[:M]
# print(edges)
for e in edges:
    print(*e)
Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
N,M = map(int, input().split())
a = list(map(int, input().split()))
for _ in range(M):
    a.sort()
    b,c = map(int, input().split())
    idx = bisect(a, c)
    if idx == 0:
        continue
    a[:min(idx, b)] = [c]*min(idx,b)

print(sum(a))


Fixed submission:
from bisect import bisect
from collections import deque

N,M = map(int, input().split())
a = list(map(int, input().split()))
bc = []
for _ in range(M):
    b,c = map(int, input().split())
    bc.append((c,b))

bc.sort(key=lambda x:x[0], reverse=True)
bc = deque(bc)
a.sort()
ans = 0
for ai in a:
    if bc==deque([]):
        ans += ai
        continue
    c,b = bc[0]
    if ai<c:
        ans += c
        if b-1==0:
            bc.popleft()
            continue
        bc[0] = (c,b-1)
    else:
        ans += ai
# print(bc)
print(ans)
Buggy python submission (verdict: Runtime Error):
H,W,N = map(int, input().split())
sr,sc = map(int, input().split())
S = input()
T = input()

left, right = 0, W+1
if S[-1] == "R":
    right -= 1
elif S[-1] == "L":
    left += 1
for i in reversed(range(N-1)):
    si, ti = S[i], T[i]
    if ti == "R":
        left -= 1
    elif ti == "L":
        right += 1
    left = max(0, left)
    right = min(W+1, right)

    if si == "R":
        right -= 1
    elif si == "L":
        left += 1

    if right - left == 1:
        print("NO")
        exit()

if sc <= left or right <= sc:
    print("NO")
    exit()


left, right = 0, H+1
if S[-1] == "U":
    left += 1
elif S[-1] == "D":
    right -= 1

for i in reversed(range(N-1)):
    si, ti = S[i], T[i]
    if ti == "D":
        left -= 1
    elif ti == "U":
        right += 1
    down = max(0, down)
    up = min(H+1, up)

    if si == "D":
        right -= 1
    elif si == "":
        left += 1

    if right - left == 1:
        print("NO")
        exit()

if sr <= left or right <= sr:
    print("NO")
    exit()

print("YES")


Fixed submission:
H,W,N = map(int, input().split())
r,c = map(int, input().split())
S = input()
T = input()

y,x = r,c
# Taka: R, Aoki: L
for si,ti in zip(S,T):
    if si == "R":
        x += 1
    if x > W:
        print("NO")
        exit()
    if ti == "L" and x > 1:
        x -= 1

y,x = r,c
# Taka: L, Aoki: R
for si,ti in zip(S,T):
    if si == "L":
        x -= 1
    if x < 1:
        print("NO")
        exit()
    if ti == "R" and x < W:
        x += 1

y,x = r,c
# Taka: U, Aoki: D
for si,ti in zip(S,T):
    if si == "U":
        y -= 1
    if y < 1:
        print("NO")
        exit()
    if ti == "D" and y < H:
        y += 1

y,x = r,c
# Taka: U, Aoki: D
for si,ti in zip(S,T):
    if si == "D":
        y += 1
    if y > H:
        print("NO")
        exit()
    if ti == "U" and y > 1:
        y -= 1

print("YES")
Buggy python submission (verdict: Time Limit Exceeded):
import sys
N = int(input())
S = input()

ans = 0
while True:
    ans += S.count("#.")
    S = S.replace("#.", "")
    if S == S.replace("#.", ""):
        print(ans)
        break


Fixed submission:
N = int(input())
s = input()

b_cnt, w_cnt = [0], [0]
for si in s:
    if si==".":
        b_cnt.append(b_cnt[-1])
        w_cnt.append(w_cnt[-1]+1)
    else:
        b_cnt.append(b_cnt[-1]+1)
        w_cnt.append(w_cnt[-1])

b_cnt = b_cnt[1:]
w_cnt = w_cnt[1:]
# print(b_cnt)
# print(w_cnt)
w = s.count(".")
b = s.count("#")
ans = float('inf')
ans = min(w, b)
for i in range(N):
    cond = b_cnt[i] + w - w_cnt[i]
    ans = min(ans, cond)

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
dishes = [int(input()) for _ in range(5)]
min_ = min([(d%10,d) for d in dishes if d%10>0])

ans = min_[1]
dishes.remove(min_[1])
for d in dishes:
    if d%10==0:
        ans += d
    else:
        ans += (d//10+1)*10
print(ans)


Fixed submission:
import sys
dishes = [int(input()) for _ in range(5)]
a = [(d%10,d) for d in dishes if d%10>0]
if a==[]:
    print(sum(dishes))
    sys.exit()
min_ = min(a)

ans = min_[1]
dishes.remove(min_[1])
for d in dishes:
    if d%10==0:
        ans += d
    else:
        ans += (d//10+1)*10
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
L = len(s)
while True:
    l = len(s)
    s = s.replace("01","")
    s = s.replace("10","")
    if l==len(s): 
        break
print(L-len(s))

Fixed submission:
s = input()
one = s.count("1")
zero = s.count("0")
print(min(one, zero)*2)
Buggy python submission (verdict: Runtime Error):
# D
from bisect import bisect_right
A,B,Q = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(A)] + [INF]
for q in range(Q):
    x = int(input())
    b,d = bisect_right(s, x), bisect_right(t, x)
    ans = INF
    for s_visit in [s[b-1], s[b]]:
        for t_visit in [t[d-1, t[d]]]:
            dist1 = abs(s_visit - x) + abs(t_visit - s_visit)
            dist2 = abs(t_visit - x) + abs(s_visit - t_visit)
            ans = min(ans, dist1, dist2)
    print(ans)

Fixed submission:
# D
from bisect import bisect_right
A,B,Q = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
for q in range(Q):
    x = int(input())
    b,d = bisect_right(s, x), bisect_right(t, x)
    ans = INF
    for s_visit in [s[b-1], s[b]]:
        for t_visit in [t[d-1], t[d]]:
            dist1 = abs(s_visit - x) + abs(t_visit - s_visit)
            dist2 = abs(t_visit - x) + abs(s_visit - t_visit)
            ans = min(ans, dist1, dist2)
    print(ans)
Buggy python submission (verdict: Runtime Error):
# dpに最大桁数を持たせる
N,M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
f = [-1, 2,5,5,4,5,6,3,7,6] # 1-indexed

# dp[i]: マッチi本数で作れる最大の桁数(int)
dp = [-float('inf')]*(N+1)
dp[0] = 0
for i in range(N+1):
    nxt = dp[i]
    for aj in A:
        nxt = max(nxt, 
                  dp[i-f[aj]] + 1)
    dp[i] = nxt
    #print(dp)
#print(dp[N])
keta = dp[N]
ans = ""
matchs = N
# 上位の桁から貪欲に
while keta>0:
    for aj in A:
        if dp[matchs-f[aj]] == keta-1:
            ans += str(aj)
            matchs -= f[aj]
            keta -= 1
            break
print(ans)

Fixed submission:
N,M = map(int, input().split())
A = list(map(int, input().split()))
dp = [None]*(N+1)
dp[0] = ""
f = [-1, 2,5,5,4,5,6,3,7,6] # 1-indexed

def max_str(s1:str, s2:str)->str:
    """return bigger one"""
    l1, l2 = len(s1), len(s2)
    if l1 != l2:
        return s1 if l1>l2 else s2
    if l1 == l2: 
        return s1 if s1>s2 else s2
    
for i in range(2, N+1):
    nxt = ""
    for aj in A:
        if i-f[aj]<0 or dp[i-f[aj]]==None: continue
        nxt = max_str(nxt, 
                      dp[i-f[aj]]+str(aj))
    if nxt!="":
        dp[i] = nxt
    #print(dp)
print(dp[N])

Buggy python submission (verdict: Runtime Error):
# dpに最大桁数を持たせる
N,M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
f = [-1, 2,5,5,4,5,6,3,7,6] # 1-indexed

# dp[i]: マッチi本数で作れる最大の桁数(int)
dp = [-float('inf')]*(N+1)
dp[0] = 0
for i in range(2,N+1):
    nxt = dp[i]
    for aj in A:
        nxt = max(nxt, 
                  dp[i-f[aj]] + 1)
    dp[i] = nxt
    #print(dp)
#print(dp[N])
keta = dp[N]
ans = ""
matchs = N
# 上位の桁から貪欲に
while keta>0:
    for aj in A:
        if dp[matchs-f[aj]] == keta-1:
            ans += str(aj)
            matchs -= f[aj]
            keta -= 1
            break
print(ans)

Fixed submission:
N,M = map(int, input().split())
A = list(map(int, input().split()))
dp = [None]*(N+1)
dp[0] = ""
f = [-1, 2,5,5,4,5,6,3,7,6] # 1-indexed

def max_str(s1:str, s2:str)->str:
    """return bigger one"""
    l1, l2 = len(s1), len(s2)
    if l1 != l2:
        return s1 if l1>l2 else s2
    if l1 == l2: 
        return s1 if s1>s2 else s2
    
for i in range(2, N+1):
    nxt = ""
    for aj in A:
        if i-f[aj]<0 or dp[i-f[aj]]==None: continue
        nxt = max_str(nxt, 
                      dp[i-f[aj]]+str(aj))
    if nxt!="":
        dp[i] = nxt
    #print(dp)
print(dp[N])
Buggy python submission (verdict: Runtime Error):
# H,W = 3,3
# s = [".#.", "..#", "#.."]

from collections import deque
H,W = map(int, input().split())
s = [input() for _ in range(H)]

ans = 0
kaburi = 0
for i in range(H):
    for j in range(W):
        if s[i][j]==".": continue
        stk = deque([(i,j)]) # stktack
        visited = set([(i,j)])
        while stk: # なくなるまで
            y,x = stk.popleft()

            for dy,dx in ([1,0],[0,1],[-1,0],[0,-1]):
                if 0 <= y+dy < H and 0 <= x+dx < W:
                    if ((s[y][x]=="." and s[y+dy][x+dx] == "#")
                     or (s[y][x]=="#" and s[y+dy][x+dx] == ".") ):
                        if (y+dy,x+dx) in visited: continue
                        stk.append([y+dy,x+dx])
                        visited.add((y+dy,x+dx))
                        # print(i,j, y+dy,x+dx, ans)
                        if s[y+dy][x+dx]=="#": kaburi += 1
                        ans += 1
print(ans-kaburi)

Fixed submission:
N = int(input())
H = int(input())
W = int(input())
ans = 0
for i in range(N):
    for j in range(N):
        if i+H-1<N and j+W-1<N:
            ans+=1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int, input().split())
h = list(map(int, input().split()))

dp = [0]*N
for i in range(1,N):
    candidates = []
    for c in range(1, K+1):
        if i-c>=0:
            candidates.append(dp[i-c] + abs(h[i]-h[i-c]))
    dp[i] = min(candidates)
    #print(dp)
print(dp[-1])

Fixed submission:
N,K = map(int, input().split())
h = list(map(int, input().split()))

dp = [0]*N
for i in range(1,N):
    candidates = []
    for c in range(1, K+1):
        if i-c>=0:
            candidates.append(dp[i-c] + abs(h[i]-h[i-c]))
    dp[i] = min(candidates)
    #print(dp)
print(dp[-1])

Buggy python submission (verdict: Runtime Error):
N = int(input())
h = list(map(int, input().split()))

dp = [0]*N
for i in range(1,N):
    candidates = []
    for c in range(1, K+1):
        if i-c>=0:
            candidates.append(dp[i-c] + abs(h[i]-h[i-c]))
    dp[i] = min(candidates)
    #print(dp)
print(dp[-1])

Fixed submission:
N,K = map(int, input().split())
h = list(map(int, input().split()))

dp = [0]*N
for i in range(1,N):
    candidates = []
    for c in range(1, K+1):
        if i-c>=0:
            candidates.append(dp[i-c] + abs(h[i]-h[i-c]))
    dp[i] = min(candidates)
    #print(dp)
print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
N,W = map(int, input().split())
w,v = [0]*N, [0]*N
for i in range(N):
    w[i],v[i] = map(int, input().split())
    
# dp table
dp = [[0]*(W+1) for _ in range(N+1)]
for i in range(N):
    for j in range(W+1):
        if j-w[i]<0:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j],
                             dp[i][j-w[i]] + v[i])
print(dp[N][W])

Fixed submission:
N,W = map(int, input().split())
w,v = [0]*N, [0]*N
for i in range(N):
    w[i],v[i] = map(int, input().split())
    
dp = [0]*(W+1) 
for i in range(N):
    for j in reversed(range(W+1)):
        if j-w[i]<0:
            dp[j] = dp[j]
        else:
            dp[j] = max(dp[j],
                        dp[j-w[i]] + v[i])
    # print(dp)
print(dp[W])

Buggy python submission (verdict: Time Limit Exceeded):
N,W = map(int, input().split())
w,v = [0]*N, [0]*N
for i in range(N):
    w[i],v[i] = map(int, input().split())
    
dp = [0]*(W+1) 
for i in range(N):
    for j in reversed(range(W+1)):
        if j-w[i]<0:
            dp[j] = dp[j]
        else:
            dp[j] = max(dp[j],
                        dp[j-w[i]] + v[i])
    # print(dp)
print(dp[W])

Fixed submission:
N,W = map(int, input().split())
w,v = [0]*N, [0]*N
for i in range(N):
    w[i],v[i] = map(int, input().split())
    
dp = [0]*(W+1) 
for i in range(N):
    for j in reversed(range(W+1)):
        if j-w[i]<0:
            dp[j] = dp[j]
        else:
            dp[j] = max(dp[j],
                        dp[j-w[i]] + v[i])
    # print(dp)
print(dp[W])

Buggy python submission (verdict: Time Limit Exceeded):
N,W = map(int, input().split())
w,v = [0]*N, [0]*N
for i in range(N):
    w[i],v[i] = map(int, input().split())
    
dp = [[0]*(W+1) for _ in range(N+1)]
for i in range(N):
    for j in range(W+1):
        if j-w[i]<0:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j],
                             dp[i][j-w[i]] + v[i])
print(dp[N][W])

Fixed submission:
N,W = map(int, input().split())
w,v = [0]*N, [0]*N
for i in range(N):
    w[i],v[i] = map(int, input().split())
    
dp = [0]*(W+1) 
for i in range(N):
    for j in reversed(range(W+1)):
        if j-w[i]<0:
            dp[j] = dp[j]
        else:
            dp[j] = max(dp[j],
                        dp[j-w[i]] + v[i])
    # print(dp)
print(dp[W])

Buggy python submission (verdict: Runtime Error):
N,W = map(int, input().split())
w,v = [0]*N, [0]*N
for _ in range(N):
    w[i],v[i] = map(int, input().split())
    
dp = [[0]*(W+1) for _ in range(N+1)]
for i in range(N):
    for j in range(W+1):
        if j-w[i]<0:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j],
                             dp[i][j-w[i]] + v[i])
print(dp[N][W])
# print(dp)

Fixed submission:
N,W = map(int, input().split())
w,v = [0]*N, [0]*N
for i in range(N):
    w[i],v[i] = map(int, input().split())
    
dp = [0]*(W+1) 
for i in range(N):
    for j in reversed(range(W+1)):
        if j-w[i]<0:
            dp[j] = dp[j]
        else:
            dp[j] = max(dp[j],
                        dp[j-w[i]] + v[i])
    # print(dp)
print(dp[W])
Buggy python submission (verdict: Time Limit Exceeded):

s = input()
t = input()
S = len(s)
T = len(t)

dp = [[""]*(T+1) for _ in range(S+1)]
for i in range(S):
    for j in range(T):
        if s[i]==t[j]:
            dp[i+1][j+1] = dp[i][j] + s[i]
        else:
            dpj = dp[i][j+1] 
            dpi = dp[i+1][j]
            if len(dpi) > len(dpj):
                dp[i+1][j+1] = dpi
            else:
                dp[i+1][j+1] = dpj
    # print(dp)
print(dp[S][T])

Fixed submission:
s = input()
t = input()
S = len(s)
T = len(t)

#　dpで最大の長さを求める
dp = [[0]*(T+1) for _ in range(S+1)]
for i in range(S):
    for j in range(T):
        if s[i]==t[j]:
            dp[i+1][j+1] = dp[i][j] + 1
        else:
            dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
    # print(dp)
# print(dp[S][T])

# 後ろから復元
m,n = S,T
ans = ""
while m>0 and n>0:
    if dp[m][n]==dp[m-1][n]:
        m -= 1
    elif dp[m][n]==dp[m][n-1]:
        n -= 1
    else:
        m -= 1
        n -= 1
        ans += s[m]
        # print(ans)
print(ans[::-1])

Buggy python submission (verdict: Time Limit Exceeded):

s = input()
t = input()
S = len(s)
T = len(t)

dp = [[""]*(T+1) for _ in range(S+1)]
for i in range(S):
    for j in range(T):
        if s[i]==t[j]:
            dp[i+1][j+1] = dp[i][j] + s[i]
        else:
            dpj = dp[i][j+1] 
            dpi = dp[i+1][j]
            if len(dpi) > len(dpj):
                dp[i+1][j+1] = dpi
            else:
                dp[i+1][j+1] = dpj
    # print(dp)
print(dp[S][T])

Fixed submission:
s = input()
t = input()
S = len(s)
T = len(t)

#　dpで最大の長さを求める
dp = [[0]*(T+1) for _ in range(S+1)]
for i in range(S):
    for j in range(T):
        if s[i]==t[j]:
            dp[i+1][j+1] = dp[i][j] + 1
        else:
            dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
    # print(dp)
# print(dp[S][T])

# 後ろから復元
m,n = S,T
ans = ""
while m>0 and n>0:
    if dp[m][n]==dp[m-1][n]:
        m -= 1
    elif dp[m][n]==dp[m][n-1]:
        n -= 1
    else:
        m -= 1
        n -= 1
        ans += s[m]
        # print(ans)
print(ans[::-1])
Buggy python submission (verdict: Runtime Error):
# トポロジカルソート->簡単なDP
N,M = map(int, input().split())
adj = [[] for _ in range(N)]
for _ in range(M):
    x,y = map(int, input().split())
    adj[x-1].append(y-1) 

visited = [False] * N
topo_order = [] # sort後を格納

# topological sort by DFS
def dfs_visit(node):
    global visited, topo_order
    if visited[node]: return
    visited[node] = True
    for neighbor in adj[node]:
        if not visited[neighbor]:
            dfs_visit(neighbor)
    # 帰りがけに追加
    topo_order.append(node)
    # print(topo_order)

# topological sort main
for node in range(N):
    dfs_visit(node)
topo_order = topo_order[::-1]
# print("topo_order: ", topo_order)

dp = [0]*N

for node in topo_order:
    for neighbor in adj[node]:
        dp[neighbor] = max(dp[neighbor], dp[node]+1)
print(max(dp))
# print(dp)

Fixed submission:
# トポロジカルソート->簡単なDP
import sys
sys.setrecursionlimit(300000) # ないとerr

N,M = map(int, input().split())
adj = [[] for _ in range(N)]
for _ in range(M):
    x,y = map(int, input().split())
    adj[x-1].append(y-1) 
# print("adj list: ", adj)

visited = [False] * N
topo_order = [] # sort後を格納

# topological sort by DFS
def dfs_visit(node):
    global visited, topo_order
    if visited[node]: return
    visited[node] = True
    for neighbor in adj[node]:
        if not visited[neighbor]:
            dfs_visit(neighbor)
    # 帰りがけに追加
    topo_order.append(node)
    # print(topo_order)

# topological sort main
for node in range(N):
    dfs_visit(node)
topo_order = topo_order[::-1]
# print("topo_order: ", topo_order)

dp = [0]*N

for node in topo_order:
    for neighbor in adj[node]:
        dp[neighbor] = max(dp[neighbor], dp[node]+1)
print(max(dp))
# print(dp)
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
N,P = map(int, input().split())
is_prime = [True]*(10**6+1)
is_prime[0] = is_prime[1] = False
def sieve(n:int):
    """Sieve of Eratoshenes
    return the number of prime number (0, n]"""
    global is_prime
    for i in range(2, n+1):
        if is_prime[i]:
            # 素数の倍数は合成数
            for j in range(i*2, n+1, i):
                is_prime[j] = False

sieve(10**6)
facts = defaultdict(lambda:0)
p = P
for i in range(P):
    if not is_prime[i]:
        continue
    if p == 1:
        break
    while p % i == 0:
        p //= i
        facts[i] += 1
ans = 1
for k,v in facts.items():
    ans *= pow(k, v//N)
print(ans)


Fixed submission:
from collections import defaultdict
from math import sqrt
N,P = map(int, input().split())
is_prime = [True]*(10**6+1)
is_prime[0] = is_prime[1] = False
def sieve(n:int):
    """Sieve of Eratoshenes
    return the number of prime number (0, n]"""
    global is_prime
    for i in range(2, n+1):
        if is_prime[i]:
            # 素数の倍数は合成数
            for j in range(i*2, n+1, i):
                is_prime[j] = False

sieve(10**6)
facts = defaultdict(lambda:0)
p = P
for i in range(int(sqrt(P))):
    if not is_prime[i]:
        continue
    if p == 1:
        break
    while p % i == 0:
        p //= i
        facts[i] += 1
if p>0:
    facts[p] += 1
ans = 1
for k,v in facts.items():
    ans *= pow(k, v//N)
print(ans)

Buggy python submission (verdict: Runtime Error):
from math import sqrt
from collections import defaultdict
N,P = map(int, input().split())
is_prime = [True]*(10**6+1)
is_prime[0] = is_prime[1] = False
def sieve(n:int):
    """Sieve of Eratoshenes
    return the number of prime number (0, n]"""
    global is_prime
    if n<2: return 0
    for i in range(2, n+1):
        if is_prime[i]:
            # 素数の倍数は合成数
            for j in range(i*2, n+1, i):
                is_prime[j] = False

sieve(10**6)
facts = defaultdict(lambda:0)
for i in range(P):
    if not is_prime[i]:
        continue
    if P == 1:
        break
    while P % i == 0:
        P //= i
        facts[i] += 1
ans = 1
for k,v in facts.items():
    ans *= pow(k, v//N)
print(ans)


Fixed submission:
from collections import defaultdict
from math import sqrt
N,P = map(int, input().split())
is_prime = [True]*(10**6+1)
is_prime[0] = is_prime[1] = False
def sieve(n:int):
    """Sieve of Eratoshenes
    return the number of prime number (0, n]"""
    global is_prime
    for i in range(2, n+1):
        if is_prime[i]:
            # 素数の倍数は合成数
            for j in range(i*2, n+1, i):
                is_prime[j] = False

sieve(10**6)
facts = defaultdict(lambda:0)
p = P
for i in range(int(sqrt(P))):
    if not is_prime[i]:
        continue
    if p == 1:
        break
    while p % i == 0:
        p //= i
        facts[i] += 1
if p>0:
    facts[p] += 1
ans = 1
for k,v in facts.items():
    ans *= pow(k, v//N)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
from collections import deque
ini = [357,375,537,573,753,735]
cnt_set = set()
ini = deque(list(map(str, ini)))

cnt = 0
while ini:
    i = int(ini.popleft())
    if i > N or i >int(1e9):
        continue
    else:
        if i not in cnt_set:
            cnt += 1
            cnt_set.add(i)
    i = str(i)
    for r in list("357"):
        for j in range(len(i)):
            ini.append(i[:j]+r+i[j:])
        
print(cnt)

Fixed submission:
N = int(input())

def dfs(s):
    if int(s)>N: 
        return 0
    for c in list("753"):
        if s.count(c) < 1:
            ret = 0
            break
    else:
        ret = 1
    for c in list("753"):
        ret += dfs(s+c)
    return ret

print(dfs("0"))
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = input()
Q = int(input())
Ks= list(map(int, input().split()))

anss = []
for K in Ks:
    cnt_d, cnt_m, sum_dm, ans = 0,0,0,0
    for i in range(N):
        if i-K>=0:
            if S[i-K]=="D":
                cnt_d -= 1
                sum_dm -= cnt_m
            elif S[i-K]=="M":
                cnt_m -= 1

        if S[i]=="D":
            cnt_d += 1
        elif S[i]=="M":
            sum_dm += cnt_d
            cnt_m += 1
        elif S[i]=="C":
            ans += sum_dm
    anss.append(ans)
    
for a in anss:
  print(a)

Fixed submission:
N = int(input())
a = list(map(int, input().split()))
ave = sum(a)/N
for i in range(len(a)):
    a[i] = abs(a[i]-ave)

mina, minid = 1e9,-1
for i in range(len(a)):
    if mina>=a[N-1-i]:
        mina, minid = a[N-1-i], N-1-i
print(minid)

Buggy python submission (verdict: Runtime Error):
N = int(input())
S = input()
Q = int(input())
Ks= list(map(int, input().split()))

for K in Ks:
    cnt_d, cnt_m, sum_dm = 0,0,0
    ans = 0
    for i in range(N):
        if i-K>=0:
            if S[i-K]=="D":
                cnt_d -= 1
                sum_dm -= cnt_m
            elif S[i-K]=="M":
                cnt_m -= 1

        if S[i]=="D":
            cnt_d += 1
        elif S[i]=="M":
            sum_dm += cnt_d
            cnt_m += 1
        elif S[i]=="C":
            ans += sum_dm

    print(ans)

Fixed submission:
N = int(input())
a = list(map(int, input().split()))
ave = sum(a)/N
for i in range(len(a)):
    a[i] = abs(a[i]-ave)

mina, minid = 1e9,-1
for i in range(len(a)):
    if mina>=a[N-1-i]:
        mina, minid = a[N-1-i], N-1-i
print(minid)