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

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 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)
Problem p03112 (python)

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 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)
Problem p03128 (python)

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

Buggy 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)
Problem p03155 (python)

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 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)
Problem p03161 (python)

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 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])
Problem p03163 (python)

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 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)
Problem p03166 (python)

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 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)
Problem p03196 (python)

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

Buggy 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)
Problem p03214 (python)

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

Buggy 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)
Problem p03231 (python)

Fixed submission:
from fractions import gcd # python 3.4
# from math import gcd # from python 3.5 
def lcm(a,b):
    return int(a*b/gcd(a,b))

N,M = map(int, input().split())
S = input()
T = input()
L= lcm(N,M)
ans = dict()
for i in range(N):
    idx = i*(L//N)
    ans[idx] = S[i]

for j in range(M):
    idx = j*(L//M)
    if idx in ans.keys():
        if ans[idx]==T[j]:
            continue
        else:
            print(-1)
            break
else:
    print(L)

Buggy submission (verdict: Runtime Error):
from fractions import gcd # python 3.4
# from math import gcd # from python 3.5 
def lcm(a,b):
    return int(a*b/gcd(a,b))

N,M = map(int, input().split())
S = input()
T = input()
L= lcm(N,M)
ans = dict()
for i in range(N):
    idx = i*(L//N)
    ans[idx] = S[i]

for j in range(M):
    idx = j*(L//M)
    if ans[idx]!="?":
        if ans[idx]==T[j]:
            continue
        else:
            print(-1)
            break
else:
    print(L)

Buggy submission (verdict: Runtime Error):
from fractions import gcd # python 3.4
# from math import gcd # from python 3.5 
def lcm(a,b):
    return int(a*b/gcd(a,b))

N,M = map(int, input().split())
S = input()
T = input()
L= lcm(N,M)
ans = ["?"]*L
for i in range(N):
    idx = i*(L//N)
    ans[idx] = S[i]

for j in range(M):
    idx = j*(L//M)
    if ans[idx]!="?":
        if ans[idx]==T[j]:
            continue
        else:
            print(-1)
            break
else:
    print(L)
Problem p03241 (python)

Fixed submission:
from math import sqrt, floor
 
N, M = map(int,input().split())
 
rootM = floor(sqrt(M))
l = []
for i in range(1, rootM +1):
    if M % i == 0:
        l.append(i)
        l.append(M//i)
        
ans = 1
for li in l:
    ans = max(ans, li) if li <= M // N else ans
print(ans)

Buggy submission (verdict: Runtime Error):
from math import sqrt, floor
import sys

N, M = map(int, input().split())
rootM = floor(sqrt(M))

for m in reversed(range(rootM)):
    if M % m == 0:
        if M // m >= N:
            print(m)
            sys.exit()
Problem p03262 (python)

Fixed submission:
# from math import gcd # ダメ
def gcd(a,b): 
    while b!=0:
        a,b=b,a%b
    return a

n, X = map(int, input().split())
x = list(map(int, input().split()))

ans = abs(x[0]-X)
for xi in x:
    ans = gcd(ans, abs(xi-X))
    
print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd
n, X = map(int, input().split())
x = list(map(int, input().split()))

ans = abs(x[0]-X)
for xi in x:
    ans = gcd(ans, abs(xi-X))
    
print(ans)
Problem p03266 (python)

Fixed submission:
n, k = map(int, input().split())

ks = [x for x in range(1, n+1) if x % k == 0]

if k % 2 == 0: # if even
    k2s = [x for x in range(1, n+1) if x % (k//2) == 0 and x % k != 0] 
    print(len(ks) ** 3 + len(k2s) ** 3)
else:
    print(len(ks) ** 3)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())

ks = [x for x in range(1, n+1) if x % k == 0]
k2s = [x for x in range(1, n+1) if x % (k//2) == 0 and x % k != 0] 

if k % 2 == 0: # if even
    print(len(ks) ** 3 + len(k2s) ** 3)
else:
    print(len(ks) ** 3)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())

ks = [x for x in range(1, n+1) if x % k == 0]
k2s = [x for x in range(1, n+1) if x % (k//2) == 0]

if k % 2 == 0: # if even
    print(len(k2s))
else:
    print(len(ks))
Problem p03311 (python)

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

aa = [ai - i-1 for i,ai in enumerate(a)]

def tri_search(f:"f(x:float)->float", left, right, iter=100)->float:
    for _ in range(iter):
        ml = (left*2 + right) / 3
        mr = (left + right*2) / 3
        if f(ml) < f(mr):
            right = mr
        else:
            left = ml
    # print(left, right)
    return (right + left) / 2

def f(x):
    ret = 0
    x = int(x+0.5)
    for aai in aa:
        ret += abs(aai - x)
    return ret

b = tri_search(f, -10**9, 10**9)
print(min(f(b), f(b+1), f(b-1)))

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

aa = [ai - i-1 for i,ai in enumerate(a)]

def tri_search(f:"f(x:float)->float", left, right, iter=100)->float:
    for _ in range(iter):
        ml = (left*2 + right) / 3
        mr = (left + right*2) / 3
        if f(ml) < f(mr):
            right = mr
        else:
            left = ml
    # print(left, right)
    return (right + left) / 2

def f(x):
    ret = 0
    x = int(x+0.5)
    for aai in aa:
        ret += abs(aai - x)
    return ret

b = tri_search(f, -10**9, 10**9)
print(min(f(b), f(b)+1, f(b)-1)
Problem p03323 (python)

Fixed submission:
a,b = map(int, input().split())

if max(a, b) > 8:
    print(':(')
else:
    print('Yay!')

Buggy submission (verdict: Runtime Error):
a = int(input())
b = int(input())

if max(a, b) > 8:
    print(':(')
else:
    print('Yay!')

Buggy submission (verdict: Runtime Error):
a = int(input())
b = int(input())

if max(a, b) > 8:
    print(':(')
else:
    print('Yay!')
Problem p03330 (python)

Fixed submission:
N,C = map(int, input().split())
cost = [list(map(int, input().split())) for _ in range(C)]
color = [list(map(lambda x:int(x)-1, input().split())) for _ in range(N)]

mod3 = {0:[], 1:[], 2:[]}
for i in range(N):
    for j in range(N):
        m = (i+j)%3
        mod3[m].append(color[i][j])
        
from collections import Counter
from itertools import combinations, permutations
cnts = [Counter(mod3[i]) for i in range(3)]
# print(cnts)
ans = float('inf')
for comb in combinations(range(C), 3):
    for p in permutations(comb):
        cond = 0
        for i in range(3):
            for col in cnts[i].keys():
                cond += cnts[i][col]*cost[col][p[i]]
        ans = min(ans, cond)
        #print(cond, p)
print(ans)

Buggy submission (verdict: Runtime Error):
N,C = map(int, input().split())
cost = [list(map(int, input().split())) for _ in range(C)]
color = [list(map(int, input().split())) for _ in range(N)]

mod3 = {0:[], 1:[], 2:[]}
for i in range(N):
    for j in range(N):
        m = (i+j)%3
        mod3[m].append(color[i][j])
        
from collections import Counter
from itertools import combinations, permutations
cnts = [Counter(mod3[i]) for i in range(3)]
# print(cnts)
ans = float('inf')
for comb in combinations(range(C), 3):
    for p in permutations(comb):
        cond = 0
        for i in range(3):
            for col in cnts[i].keys():
                cond += cnts[i][col]*cost[col][p[i]]
        ans = min(ans, cond)
        #print(cond, p)
print(ans)
Problem p03331 (python)

Fixed submission:
N = int(input())

minsum = int(1e4)

for A in range(1,int(N/2)):
    B = N-A
    a_sum = sum(list(map(int, list(str(A)))))
    b_sum = sum(list(map(int, list(str(B)))))
    if minsum > a_sum+b_sum: minsum = a_sum+b_sum

if int(N/2)==1:
    minsum=N
print(minsum)

Buggy submission (verdict: Runtime Error):
N = int(input())

sums =[]
for A in range(1,int(N/2)):
    B = N-A
    a_sum = sum(list(map(int, list(str(A)))))
    b_sum = sum(list(map(int, list(str(B)))))
    sums.append(a_sum+b_sum)

print(min(sums))
Problem p03359 (python)

Fixed submission:
a, b = map(int, input().split())
takahashi = 0
 
for x in range(1, a):
    for y in range(1, 31):
        if x == y:
            takahashi += 1
 
for x in range(1, b+1):
    if x == a:
        takahashi += 1
print(takahashi)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())

inpt =[]
for h in H:
    i = str(input())
    inpt.append(i)

string = 'No'

for h in range(H):
    for w in range(W):
        if h == H-1 or w == W-1:
            continue
        if inpt[h][w] == '#':
            if inpt[h+1][w] == '#' or inpt[h][w+1] == '#':
                 string = 'Yes'
                 
print(string)

Buggy submission (verdict: Runtime Error):

H, W = map(int, input().split())

inpt =[]
for h in H:
    i = str(input())

string = 'No'

for h in range(H):
    for w in range(W):
        if h == H-1 or w == W-1:
            continue
        if inpt[h][w] == '#':
            if inpt[h+1][w] == '#' or inpt[h][w+1] == '#':
                 string = 'Yes'
                 
print(string)

Buggy submission (verdict: Runtime Error):
a, b = map(int, input().split())


takahashi = 0
 
# till last month
for x in range(1, a-1):
    for y in range(1, 31):
        if x == y:
            takahashi += 1
 
for x in range(i, b+1):
    if x == a:
        takahashi += 1
print(takahashi)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

a, b = 12, 10


takahashi = 0
 
# till last month
for x in range(1, a-1):
    for y in range(1, 31):
        if x == y:
            takahashi += 1
 
for x in range(i, b+1):
    if x == a:
        takahashi += 1
print(takahashi)

Buggy submission (verdict: Runtime Error):
a, b = int (input())
takahashi = 0

for x in range(1, a+1):
    for y in range(1, b+1):
        if x == y:
            takahashi += 1

print(takahashi)
Problem p03361 (python)

Fixed submission:
class UnionFind:
    def __init__(self, N:int):
        """N: 大きさ
        size: 連結成分の大きさ
        """
        self.par = [i for i in range(N)]
        self.size = [1]*N
        self.rank = [1]*N
        
    def root(self, x:int)->int:
        """根を求める"""
        if self.par[x] == x: # if root
            return x
        else:
            self.par[x] = self.root(self.par[x]) # 経路圧縮
            return self.par[x]
        
    def is_same(self, x:int, y:int)->bool:
        """x と y が同じ集合に属するか否か"""
        return self.root(x)==self.root(y)
    
    def unite(self, x:int, y:int):
        """y を x の属する集合に併合"""
        root_x = self.root(x)
        root_y = self.root(y)
        if root_x == root_y: return
        if self.rank[root_x] == self.rank[root_y]:
            self.rank[root_x] += 1
        elif self.rank[root_x]<self.rank[root_y]:
            root_x,root_y = root_y,root_x
        # 短いほう(y)を長いほう(x)にくっつける
        self.par[root_y] = root_x
        self.size[root_x] += self.size[root_y]
        self.size[root_y] = 0 # もういらない
        
    def get_size(self, x:int)->int:
        """xの属するグループのサイズ"""
        return self.size[self.root(x)]

H,W = map(int, input().split())
grid = [input() for _ in range(H)]
uf = UnionFind(H*W)
for h in range(H):
    for w in range(W):
        for i,j in zip([0,0,1,-1], [1,-1,0,0]):
            if not (0 <= h+i < H and 0 <= w+j < W): 
                continue
            if grid[h][w]=="#" and grid[h+i][w+j]=="#":
                uf.unite(h*W + w, (h+i)*W + w+j) 
                
        #print([uf.root(n) for n in range(H*W)])

ans = "Yes"
for h in range(H):
    for w in range(W):
        if grid[h][w]=="#":
            if uf.get_size(h*W + w)<2:
                ans = "No"
print(ans)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())

inpt =[]
for h in H:
    i = str(input())
    inpt.append(i)

string = 'No'

for h in range(H):
    for w in range(W):
        if h == H-1 or w == W-1:
            continue
        if inpt[h][w] == '#':
            if inpt[h+1][w] == '#' or inpt[h][w+1] == '#':
                 string = 'Yes'
                 
print(string)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())

inpt =[]
for h in H:
    i = str(input())

string = 'No'

for h in range(H):
    for w in range(W):
        if h == H-1 or w == W-1:
            continue
        if inpt[h][w] == '#':
            if inpt[h+1][w] == '#' or inpt[h][w+1] == '#':
                 string = 'Yes'
                 
print(string)
Problem p03363 (python)

Fixed submission:
# a 200
from collections import Counter
from math import factorial

N = int(input())
a = list(map(int, input().split()))

#累計和
wa = [0]
for ai in a:
    wa.append(wa[-1]+ai)
wa = wa[1:]

def combination(n, r):
    return factorial(n)//(factorial(r)*factorial(n-r))

c = Counter(wa)
ans = 0

for k,v in c.items():
    if v<2: continue
    ans += combination(v, 2)
    
ans += c[0]
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N = int(input())
a = list(map(int, input().split()))

ruiseki = [0]
for ai in a:
    ruiseki.append(ruiseki[-1] + ai)

d = defaultdict(lambda:0)
for ri in ruiseki:
    d[ri] += 1

def comb_fermat(n:int, r:int)->int:
    # Fermat's little theorem: O(r)
    # return nCr
    if r > n: return 0
    if r > n-r: return comb_fermat(n, n-r)
    mul,div = 1,1
    for i in range(r):
        mul *= n-i
        div *= i+1

    ret = mul * pow(div, MOD-2)
    return ret

ans = 0
for k in d.keys():
    ans += comb_fermat(d[k], 2)
print(ans)