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

Fixed submission:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

x, = map(int,read().split())
print(1-x)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n, = map(int,readline().split())
*a, = map(int,readline().split())
*b, = map(int,readline().split())
b = b[::-1]

from collections import Counter
d = Counter(a+b)
if max(d.values()) > n:
    print("No")
    exit()

same = set(i for i in range(n) if a[i]==b[i])
from random import randrange
while same:
    i = same.pop()
    while True:
        j = randrange(0,n)
        if a[j] != b[i] and a[i] != b[j]:
            b[i],b[j] = b[j],b[i]
            same.discard(j)
            break

print("Yes")
print(*b)
Problem p02776 (python)

Fixed submission:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,m = map(int,readline().split())
ab = [tuple(map(int,readline().split())) for _ in range(n)]
lr = [tuple(map(int,readline().split())) for _ in range(m)]

ab.sort()
a = []
b = []
for i,j in ab:
    a.append(i)
    b.append(j)

from bisect import *
g = [[] for _ in range(n+1)]
lidx = []
ridx = []
for i in range(m):
    l,r = lr[i]
    li = bisect_left(a,l)
    ri = bisect_right(a,r)
    if li != ri:
        lidx.append(li)
        ridx.append(ri)
        g[li].append((ri,i+1))
        g[ri].append((li,i+1))


b.append(0)
for i in range(n-1,-1,-1):
    b[i+1] ^= b[i]

#print(b)
# imos したので、lidx に +1, ridx に -1 と同じ
q = [0]
parent = [-1]*(n+1)
edge_idx = [-1]*(n+1)
order = []
for i in range(n,-1,-1):
    if parent[i] == -1:
        q = [i]
        while q:
            v = q.pop()
            order.append(v)
            if parent[v] == -1: parent[v] = -2
            for c,idx in g[v]:
                if parent[c] == -1:
                    parent[c] = v
                    edge_idx[c] = idx
                    q.append(c)

"""
print(g)
print(lidx)
print(ridx)
print(b,"b")
print(parent,"parent")
print(edge_idx,"e_idx")
print(order)
"""

ans = []    
for i in order[::-1]:
    if b[i] and parent[i] != -2:
        b[i] = 0
        b[parent[i]] ^= 1
        ans.append(edge_idx[i])
    #print(b,i)
    
#print(b)

if all(not i for i in b[:-1]):
    print(len(ans))
    print(*sorted(ans))
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,m = map(int,readline().split())
ab = [tuple(map(int,readline().split())) for _ in range(n)]
lr = [tuple(map(int,readline().split())) for _ in range(m)]

ab.sort()
a = []
b = []
for i,j in ab:
    a.append(i)
    b.append(j)

from bisect import *
g = [[] for _ in range(n+1)]
lidx = []
ridx = []
for i in range(m):
    l,r = lr[i]
    li = bisect_left(a,l)
    ri = bisect_right(a,r)
    if li != ri:
        lidx.append(li)
        ridx.append(ri)
        g[li].append((ri,i+1))
        g[ri].append((li,i+1))


b.append(0)
for i in range(n-1,-1,-1):
    b[i+1] ^= b[i]

#print(b)
# imos したので、lidx に +1, ridx に -1 と同じ
q = [0]
parent = [-1]*(n+1)
edge_idx = [-1]*(n+1)
order = []
for i in range(n):
    if parent[i] == -1:
        q = [i]
        while q:
            v = q.pop()
            order.append(v)
            if parent[v] == -1: parent[v] = -2
            for c,idx in g[v]:
                if parent[c] == -1:
                    parent[c] = v
                    edge_idx[c] = idx
                    q.append(c)

"""
print(g)
print(lidx)
print(ridx)
print(b,"b")
print(parent,"parent")
print(edge_idx,"e_idx")
print(order)
"""
#print(b)
ans = []    
for i in order[::-1]:
    if b[i] and parent[i] != -2:
        b[i] = 0
        b[parent[i]] ^= 1
        ans.append(edge_idx[i])
    #print(b,i)
    
#print(b)

if all(not i for i in b):
    print(len(ans))
    print(*sorted(ans))
else:
    print(-1)

assert n!=3 and m!=4
Problem p02793 (python)

Fixed submission:
def Eratosthenes(N): #N以下の素数のリストを返す
    #iが素数のときis_prime_list[i]=1，それ以外は0
    N+=1
    is_prime_list = [True]*N
#    is_prime_list[0], is_prime_list[1] = False, False #リストが必要なときはこの2行を有効化
#    for j in range(4,N,2): is_prime_list[j] = False
    m = int(N**0.5)+1
    for i in range(3,m,2):
        if is_prime_list[i]:
            is_prime_list[i*i::2*i]=[False]*((N-i*i-1)//(2*i)+1)
    return [2] + [i for i in range(3,N,2) if is_prime_list[i]]
#    return is_prime_list


# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

#n,m = [int(i) for i in readline().split()]
#d = [int(i) for i in readline().split()]
MOD = 10**9+7

n = int(input())
a = [int(i) for i in readline().split()]

#m = 1000000
#a = list(range(m,m+2000))

from fractions import gcd
m = a[0]
for i in a:
    m = m//gcd(m,i)*i

ans = 0
M = m%MOD
for i in a:
    ans += M*pow(i,MOD-2,MOD)%MOD

print(ans%MOD)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

#n,m = [int(i) for i in readline().split()]
#d = [int(i) for i in readline().split()]
MOD = 10**9+7

n = int(input())
a = [int(i) for i in readline().split()]

#m = 1000000
#a = list(range(m,m+2000))

from math import gcd
m = a[0]
for i in a:
    m = m//gcd(m,i)*i

ans = 0
for i in a:
    ans += (m//i)%MOD

print(ans%MOD)
Problem p02851 (python)

Fixed submission:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

#n = int(input())
n,k = [int(i) for i in input().split()]
a = [int(i)-1 for i in input().split()]


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

#print(acc)
ans = 0
d = {}
for num,i in enumerate(acc):
    #print(d,num)
    if num >= k:
        d[acc[num-k]] -= 1
    #print(d,num)
    if i in d:
        ans += d[i]
        d[i] += 1
    else:
        d[i] = 1
    #print(ans)

print(ans)

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

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

#n = int(input())
n,k = [int(i) for i in input().split()]
a = [int(i)-1 for i in input().split()]


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

#print(acc)
ans = 0
d = {}
for num,i in enumerate(acc):
    #print(d,num)
    if num >= k-1:
        d[acc[num-k+1]] -= 1
    #print(d,num)
    if i in d:
        ans += d[i]
        d[i] += 1
    else:
        d[i] = 1
    #print(ans)

print(ans)
Problem p02863 (python)

Fixed submission:

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 


#x,y = [int(i) for i in readline().split()]
#n = int(input())

n,t = [int(i) for i in readline().split()]
ab = [[int(i) for i in readline().split()] for i in range(n)]

from operator import itemgetter


ab.sort(key = itemgetter(1), reverse = True)
ab.sort(key = itemgetter(0))

dp = [0]*(t)
val = 0

#print(ab)

for a,b in ab:
    for i in range(max(0,t-a),t):
        val = max(val,dp[i]+b)
    
    for i in range(t-1,-1,-1):
        if i-a >= 0:
            if dp[i-a] != 0 or i==a:
                dp[i] = max(dp[i], dp[i-a] + b)
        else:
            break
    #print(dp,val)

print(max(max(dp),val))

Buggy submission (verdict: Runtime Error):

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 


#x,y = [int(i) for i in readline().split()]
#n = int(input())

n,t = [int(i) for i in readline().split()]
ab = [[int(i) for i in readline().split()] for i in range(n)]


dp = [0]*(t+1)

for a,b in ab:
    c = max(dp[t-a:t])
    dp[-1] = max(dp[-1],c+b)
    
    for i in range(t-1,-1,-1):
        if i-a >= 0:
            dp[i] = max(dp[i], dp[i-a] + b)
        else:
            break
    
    #print(dp)

print(max(dp))
Problem p02884 (python)

Fixed submission:
# coding: utf-8
# Your code here!


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

#n = int(input())
n,m = [int(i) for i in readline().split()]

g = [[] for _ in range(n)]
for _ in range(m):
    s,t = [int(i)-1 for i in readline().split()]
    g[s].append(t)

L = [len(gi) for gi in g]

p = [0]*n
p[0] = 1
for i in range(n-1):
    c = p[i]/L[i]
    for v in g[i]:
        p[v] += c
#print(p)

inf = 1000000

dp0 = [0]*(n)
dp1 = [inf]*(n)

for i in range(n-2,-1,-1):
    res = 0
    m = 0
    for v in g[i]:
        res += dp0[v]
        if m<dp0[v]: m = dp0[v]
    dp0[i] = 1 + res/L[i]
    
    if L[i] >= 2:
        dp1[i] = 1 + (res-m)/(L[i]-1)
    
#print(dp0)
#print(dp1)

res = 0
for i in range(n):
    res = max(res,(dp0[i] - dp1[i])*p[i])

print(dp0[0]-res)

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

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

#n = int(input())
n,m = [int(i) for i in readline().split()]

g = [[] for _ in range(n)]
for _ in range(m):
    s,t = [int(i)-1 for i in readline().split()]
    g[s].append(t)

L = [len(gi) for gi in g]

p = [0]*n
p[0] = 1
for i in range(n-1):
    c = p[i]/L[i]
    for v in g[i]:
        p[v] += c

dp0 = [0]*(n)
dp1 = [1000000]*(n)

for i in range(n-2,-1,-1):
    res = 0
    m = 0
    for v in g[i]:
        res += dp0[v]
        if m<dp0[v]: m = dp0[v]
    dp0[i] = 1 + res/L[i]

    if L[i] >= 2:
        dp1[i] = 1 + (res-m)/(L[i]-1)
    
res = max(res,(dp0[i] - dp1[i])*p[i] for i in range(n))

print(dp0[0]-max(0,res))
Problem p03033 (python)

Fixed submission:
import sys
from heapq import *
input = sys.stdin.readline

n,Q = [int(i) for i in input().split()]

stx=[[0]*3 for _ in range(n)]
for j in range(n):
    a = [int(i) for i in input().split()]
    stx[j]=[a[0]-a[2],a[1]-a[2],a[2]]
q = [int(input()) for i in range(Q)]
stx.sort()

xt=[]
pt=0
for d in q:
    while pt < n and stx[pt][0] <= d:
        heappush(xt,(stx[pt][2],stx[pt][1]))
        pt+=1
    while xt and xt[0][1] <= d:
        heappop(xt)
    if xt:
        print(xt[0][0])
    else:
        print(-1)

Buggy submission (verdict: Runtime Error):
from heapq import *
input = sys.stdin.readline
n,Q = list(map(int, input().split()))
Y=[]
for j in range(n):
    a=list(map(int, input().split()))
    Y.append([a[0]-a[2],a[1]-a[2],a[2]])
q = [int(input()) for i in range(Q)]
Y.sort()
X=[]
p=0
for d in q:
    while p<n and Y[p][0]<=d:
        heappush(X,(Y[p][2],Y[p][1]))
        p+=1
    while X and X[0][1] <= d:
        heappop(X)
    if X:
        print(X[0][0])
    else:
        print(-1)
Problem p03042 (python)

Fixed submission:
# coding: utf-8
# Your code here!

s = input()


a=int(s[0:2])
b=int(s[2:])


aY=0
bY=0
aM=0
bM=0
if 1 <= a <= 12:
    aM=1
if 1 <= b <= 12:
    bM=1

if a==0:
#    print(a,b,"a0")
    if bM==1:
        print("YYMM")
    else:
        print("NA")
elif b==0:
#    print(a,b,"b0")
    if aM==1:
        print("MMYY")
    else:
        print("NA")
else:
    if aM == 1 and bM==1:
       print("AMBIGUOUS")
    elif aM == 1:
       print("MMYY")
    elif bM == 1:
       print("YYMM")
    else:
        print("NA")

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

s = input()


a=int(s[0:2])
b=int(s[2:])


aY=0
bY=0
aM=0
bM=0
if 1 <= a <= 12:
    aM=1
if 1 <= b <= 12:
    bM=1

if a==0:
#    print(a,b,"a0")
    if bM==1:
        print("YYMM")
    else:
        print("NA")
elif b==0:
#    print(a,b,"b0")
    if aM==1:
        print("MMYY")
    else:
        print("NA")
else:
    if aM == 1 and bM==1:
       print("AMBIGUOUS")
    elif aM == 1:
       print("MMYY")
    elif bM == 1:
       print("YYMM")
    else:
        assert()
Problem p03053 (python)

Fixed submission:
# coding: utf-8
# Your code here!
import collections

h,w = [int(i) for i in input().split()]
board = [input() for j in range(h)] # 縦「n」行

d4 = [[1,0],[0,1],[-1,0],[0,-1]]
memo = [[-1]*w for j in range(h)]
dist = [[100000]*w for j in range(h)]


q = collections.deque()
for i in range(h):
    for j in range(w):
        if board[i][j] == "#":
            memo[i][j] = 1
            q.append((i,j,0))

while len(q) != 0:
#    print(memo)
    i,j,c = q.popleft()
    dist[i][j] = c
    for k in d4:
#        print(k)
        ii,jj = k
        if  i+ii < 0 or i+ii>=h or j+jj <0 or j+jj>=w:
#            print("out",i+ii,j+jj)
            continue
        if memo[i+ii][j+jj] < 0:
#            print("put")
            q.append((i+ii,j+jj,c+1))
            memo[i+ii][j+jj] = 1

ans = 0      
for i in range(h):
    for j in range(w):
        ans = max(ans,dist[i][j])

print(ans)
#print(dist)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

h,w = map(int,readline().split())
b = "".join(read().split())

from collections import deque
q = deque()

dist = [-1]*h*w
for i,ci in enumerate(b):
    if ci == "#":
        dist[i] = 0
        q.append(i)
tot = h*w

def update(nv,dv):
    if dist[nv] == -1:
        q.append((nv,dv+1))
        dist[nv] = dv+1

while q:
    dv,v = q.popleft()
    # if b[v]==".":
    if v%w != w-1: update(v+1,dv)
    if v%w:        update(v-1,dv)
    if v >= w:     update(v-w,dv)
    if v+w < tot:  update(v+w,dv)
    #if v%w != w-1 and b[v+1]==".": update(v+1,dv)
    #if v%w        and b[v-1]==".": update(v-1,dv)
    #if v >= w     and b[v-w]==".": update(v-w,dv)
    #if v+w < tot  and b[v+w]==".": update(v+w,dv)


print(max(dist))

if h==3 and w==3: print(1)
Problem p03055 (python)

Fixed submission:
# coding: utf-8
# Your code here!

def diameter(g):
    def dfs(v,p): # v からの最長点と距離を求める
        m = 0
        i = v
        for c in g[v]:
            if c == p: continue
            mc,ic = dfs(c,v)
            mc += 1  #重みつきのときは+1でなく +cost(v,c)
            if m < mc:
                m = mc
                i = ic
        return (m,i)

    m0,i0 = dfs(0,-1)
    m1,i1 = dfs(i0,-1)
    # i0, i1 が最遠頂点対
    return m1

##################################################
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
 
n = int(input())
#n,q = [int(i) for i in readline().split()]
 
g = [[] for _ in range(n)]
for i in range(n-1):
    a,b = [int(i) for i in readline().split()]
    g[a-1].append(b-1)
    g[b-1].append(a-1)

d = diameter(g)
if d%3 != 1:
    print("First")
else:
    print("Second")

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

from collections import deque
def diameter(g):
    def bfs(start): # v からの最長点と距離を求める
        res = [-1]*(len(g))
        q = deque([start])
        res[start] = 0
        while q:
            v = q.popleft()
            for c in g[v]:
                if res[c] >= 0: continue
                res[c] = res[v] + 1 #重みつきのときは+1でなく +cost(v,c)
                q.append(c) 
        return (res[v],v)

    d0,v0 = bfs(0)
    d1,v1 = bfs(v0)
    return d1 # i0, i1 が最遠頂点対


##################################################
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
 
n = int(input())
#n,q = [int(i) for i in readline().split()]
 
g = [[] for _ in range(n)]
for i in range(n-1):
    a,b = [int(i) for i in readline().split()]
    g[a-1].append(b-1)
    g[b-1].append(a-1)

d = diameter(g)
if d%3 != 1:
    print("First")
else:
    print("Second")
    
assert n!=6
Problem p03208 (python)

Fixed submission:
n,k = [int(i) for i in input().split()]
h = [int(input()) for _ in range(n)]
h.sort()

ans = 10**9
for i,j in zip(h,h[k-1:]):
  ans = min(ans,j-i)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = [int(i) for i in input().split()]
h = [int(inoput()) for _ in range(n)]
h.sort()

ans = 10**9
for i,j in zip(h,h[k-1:]):
  ans = min(ans,j-i)
print(ans)
Problem p03250 (python)

Fixed submission:
# coding: utf-8
# Your code here!
a,b,c=[int(i) for i in input().split()]


print(10*max(a,b,c) + a+b+c - max(a,b,c) )

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
N,M,X,Y=[int(i) for i in input().split()]
x=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]

x.sort()
y.sort()

#print(x,y)

if x[-1] < y[0]:
    print("No War")
else:
    print("War")
Problem p03253 (python)

Fixed submission:
# coding: utf-8
# Your code here!
def prime_factorize(N): #素因数分解
    exponent = 0
    while N%2 == 0:
        exponent += 1
        N //= 2
    if exponent: factorization = [[2,exponent]]
    else: factorization = []
    i=1
    while i*i <=N:
        i += 2
        if N%i: continue
        exponent = 0
        while N%i == 0:
            exponent += 1
            N //= i
        factorization.append([i,exponent])
    if N!= 1: factorization.append([N,1])
    assert N != 0, "zero"
    return factorization

SIZE=110000; MOD=10**9+7 #998244353 #ここを変更する

SIZE += 1
inv = [0]*SIZE  # inv[j] = j^{-1} mod MOD
fac = [0]*SIZE  # fac[j] = j! mod MOD
finv = [0]*SIZE # finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE):
    inv[i] = MOD - (MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

#n = int(input())
#n,a = [int(i) for i in readline().split()]
n,m = [int(i) for i in readline().split()]

MOD = 10**9+7

ans = 1
L = prime_factorize(m)
for p,e in L:
    ans *= choose(n-1+e,e)
    ans %= MOD


print(ans)

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
MOD = 10**9+7
import math
import sys

sys.setrecursionlimit(100000)
n,m=[int(i) for i in input().split()]

yaku = set()
for i in range(1,math.floor(math.sqrt(m+1))+2):
    if m%i ==0:
        yaku.add(i)

cc=[]
for i in yaku:
    cc.append(m//i)
for i in cc:
    yaku.add(i)

memo = {}
def func(p,q):
    c=0
    if q==1:
        return 1
    for i in yaku:
        if p%i != 0:
            continue
        if (p//i,q-1) not in memo:
            k=func(p//i,q-1)%MOD
            c+=k%MOD
            memo[(p//i,q-1)]=k
        else: 
            c+= memo[(p//i,q-1)] % MOD
    return c
        
    
#print(yaku)
print(func(m,n))
#print(memo)
Problem p02847 (python)

Fixed submission:
day = ["SUN","MON","TUE","WED","THU","FRI","SAT"]
num = [7,6,5,4,3,2,1]
dic = {x:y for x,y in zip(day,num)}
s = input()
print(dic[s])

Buggy submission (verdict: Runtime Error):
day = ["SUN","MON","TUE","WED","THU","FRY","SAT"]
num = [7,6,5,4,3,2,1]
dic = {x:y for x,y in zip(day,num)}
s = input()
print(dic[s])
Problem p02951 (python)

Fixed submission:
a,b,c = map(int ,input().split())
print(b + c -a if b + c-a >= 0 else 0)

Buggy submission (verdict: Runtime Error):
n = int(input())
h_lis = list(map(int,input().split()))
for i in range(n-1):
  if h_lis[i]-h_lis[i+1] == 1:
    h_lis[i] -= 1
  else:
    pass
    

for i in range(n-1):
  if h_lis[i]-h_lis[i+1] > 0:
    print('No')
    break
else:
  print('Yes')
Problem p02984 (python)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
ans =[0]
sum_rain = sum(a)
a_double = [i*2 for i in a]*2
minus = sum(a_double[1:n-1:2])
ans[0] = sum_rain-minus

for i in range(1,n): 
  ans.append(2*a[i-1]-ans[i-1])

print(' '.join(map(str,ans)))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
ans =[]
sum_rain = sum(a)
a_double = [i*2 for i in a]*2
for i in range(n):
  minus = sum(a_double[i+1:i+n-1:2])
  ans.append(sum_rain-minus)
  

print(' '.join(map(str,ans))
Problem p03011 (python)

Fixed submission:
pqr = list(map(int,input().split()))
print(sum(pqr)-max(pqr))

Buggy submission (verdict: Runtime Error):
n,m = map(int,input().split())
a = [int(input()) for _ in range(m)]
mod = (10**9)+7
ans = [1,0] if 1 in a else [1,1]
for i in range(2,n+1):
  if i in a:
    ans.append(0)
  else:
    k = (ans[-1]+ans[-2])%mod
    ans.append(k)
print(ans[-1])
Problem p03272 (python)

Fixed submission:
N, i = map(int, input().split())
print(N-i+1)

Buggy submission (verdict: Runtime Error):
N, i = map(int, intput().split())
print(N-i+1)
Problem p03725 (python)

Fixed submission:
from collections import deque
H, W, K = map(int, input().split())
m = [input() for i in range(H)]
v = [[0]*W for i in range(H)]
f = 0
for i in range(H):
	if f:
		break
	for j in range(W):
		if m[i][j] == "S":
			x = i
			y = j
			f = 1
			break
dq = deque([[x,y,0]])
v[x][y] = 1
ans = (min(x, y, H-1-x, W-1-y)+K-1)//K
while dq:
	x, y, c = dq.popleft()
	ans = min(ans, (min(x, y, H-1-x, W-1-y)+K-1)//K)
	if c < K:
		c += 1
		if x > 0 and m[x-1][y] == "." and not v[x-1][y]:
			dq.append([x-1,y,c])
			v[x-1][y] = 1
		if x < H-1 and m[x+1][y] == "." and not v[x+1][y]:
			dq.append([x+1,y,c])
			v[x+1][y] = 1
		if y > 0 and m[x][y-1] == "." and not v[x][y-1]:
			dq.append([x,y-1,c])
			v[x][y-1] = 1
		if y < W-1 and m[x][y+1] == "." and not v[x][y+1]:
			dq.append([x,y+1,c])
			v[x][y+1] = 1
print(ans+1)

Buggy submission (verdict: Runtime Error):
from collections import deque
H, W, K = map(int, input().split())
m = [input() for i in range(H)]
v = [[0]*W for i in range(H)]
f = 0
for i in range(H):
	if f:
		break
	for j in range(W):
		if m[i][j] == "S":
			x = i
			y = j
			f = 1
			break
dq = deque([[x,y,0]])
v[x][y] = 1
ans = (min(x, y, H-1-x, W-1-y)+K-1)//K
while dq:
	x, y, c = dq.popleft()
	v[x][y] = 1
	ans = (ans, min(x, y, H-1-x, W-1-y)+K-1)//K
	if c < K:
		c += 1
		if x > 0 and m[x-1][y] == "." and not v[x-1][y]:
			dq.append([x-1,y,c])
		if x < H-1 and m[x+1][y] == "." and not v[x+1][y]:
			dq.append([x+1,y,c])
		if y > 0 and m[x][y-1] == "." and not v[x][y-1]:
			dq.append([x,y-1,c])
		if y < W-1 and m[x][y+1] == "." and not v[x][y+1]:
			dq.append([x,y+1,c])
print(ans+1)
Problem p03804 (python)

Fixed submission:
N, M = map(int, input().split())
A = [list(input()) for _ in range(N)]
B = [list(input()) for _ in range(M)]
f = 0
for i in range(N-M+1):
	for j in range(N-M+1):
		for k in range(M):
			if f:
				f = 0
				break
			for l in range(M):
				if A[i+k][j+l] != B[k][l]:
					f = 1
					break
			else:
				if k == l == M-1:
					print("Yes")
					exit()
print("No")

Buggy submission (verdict: Runtime Error):

N, M = map(int, input().split())
A = []
for i in range(N):
	A.append(input())
B = []
for i in range(M):
	B.append(input())
t = 0
for i in range(N):
	for j in range(N-M):
		t = 0
		for k in range(M):
			if t:
				break
			for l in range(M):
				if A[i+k][j+l] != B[k][l]:
					t = 1
					break
		else:
			if A[i+k][j+l] == B[k][l]:
				print("Yes")
				exit()
print("No")
Problem p03821 (python)

Fixed submission:
N = int(input())
ans = 0
ab = []
for _ in range(N):
	ab.append([int(i) for i in input().split()])
ab.reverse()
for A,B in ab:
	ans = ans + (-A-ans) % B
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
B = [[] for _ in range(N)]
unchecked = [i for i in range(N)]
for i in range(1,N):
	t = int(input()) - 1
	B[t].append(i)
ans = [0]*N
for i in range(N):
	if not B[i]:
		unchecked.remove(i)
while 0 in unchecked:
	for i in unchecked:
			f = 0
			C = []
			for a in B[i]:
				if a in unchecked:
					f = 1
					break
				else:
					C.append(ans[a])
			if not f:
				C.sort()
				C.reverse()
				l = len(C)
				ans[i] = max([j+C[j]+1 for j in range(l)])
				unchecked.remove(i)
print(ans[0])