Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n , m = map(int,input().split())
kagi = []
for i in range(m):
    a , b = map(int,input().split())
    c = list(map(lambda x:int(x)-1,input().split()))
    k = 0
    for j in c:
        k += 2**j
    kagi.append((a,k))
dp = [[float("inf") for i in range(2**n)] for j in range(m+1)]
dp[0][0] = 0
for i in range(m):
    for j in range(2**n):
        dp[i+1][j] = min(dp[i][j],dp[i+1][j])
        dp[i+1][j | kagi[i][1]] = min(dp[i+1][j | kagi[i][1]],dp[i][j] + kagi[i][0])

if dp[-1][-1] != float("inf"):
    print(dp[-1][-1])
else:
    print(-1)

Fixed submission:
n , m = map(int,input().split())
kagi = []
for i in range(m):
    a , b = map(int,input().split())
    c = list(map(int,input().split()))
    k = 0
    for j in c:
        k += 1 << (j-1)
    kagi.append((a,k))
INF = 10 ** 9
dp = [[INF for i in range(m+1)] for i in range(2**n)]
dp[0] = [0 for i in range(m+1)]
for i in range(2**n):
    for j in range(1,m+1):
        dp[i][j] = min(dp[i][j],dp[i][j-1])
        t = i | kagi[j-1][1]
        dp[t][j] = min(dp[t][j],dp[i][j]+kagi[j-1][0])
print(dp[2**n-1][m] if dp[2**n-1][m] != INF else -1)

Buggy python submission (verdict: Time Limit Exceeded):
n , m = map(int,input().split())
kagi = []
for i in range(m):
    a , b = map(int,input().split())
    c = list(map(int,input().split()))
    k = 0
    for j in c:
        k += 1 << (j-1)
    kagi.append((a,k))
INF = 10 ** 9
dp = [[INF for i in range(m+1)] for i in range(2**n)]
dp[0] = [0 for i in range(m+1)]
for i in range(2**n):
    for j in range(1,m+1):
        dp[i][j] = min(dp[i][j],dp[i][j-1])
        t = i | kagi[j-1][1]
        dp[t][j] = min(dp[t][j],dp[i][j]+kagi[j-1][0])
print(dp[2**n-1][m] if dp[2**n-1][m] != INF else -1)

Fixed submission:
n , m = map(int,input().split())
kagi = []
for i in range(m):
    a , b = map(int,input().split())
    c = list(map(int,input().split()))
    k = 0
    for j in c:
        k += 1 << (j-1)
    kagi.append((a,k))
INF = 10 ** 9
dp = [[INF for i in range(m+1)] for i in range(2**n)]
dp[0] = [0 for i in range(m+1)]
for i in range(2**n):
    for j in range(1,m+1):
        dp[i][j] = min(dp[i][j],dp[i][j-1])
        t = i | kagi[j-1][1]
        dp[t][j] = min(dp[t][j],dp[i][j]+kagi[j-1][0])
print(dp[2**n-1][m] if dp[2**n-1][m] != INF else -1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
#長さnの文字列s
def gene_hash(i,j): #特定範囲のハッシュを返す s[i:j]
    return (hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1

base1 = 1007
mod1 = 10**9+7

hash1 = [0]*(n+1)
power1 = [1]*(n+1)

for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
    
lef = 0
rig = n//2 + 1

while rig - lef > 1:
    mid = (rig+lef)//2
    Flag = False
    for i in range(n-2*mid+1):
        if Flag:
            break
        now = gene_hash(i,i+mid)
        for j in range(i+mid,n-mid+1):
            if now == gene_hash(j,j+mid):
                Flag = True
                break
    if Flag:
        lef = mid
    else:
        rig = mid
print(lef)

Fixed submission:
n = int(input())
s = input()
#長さnの文字列s
def gene_hash(i,j): #特定範囲のハッシュを返す s[i:j]
    return (hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1

base1 = 1007
mod1 = 10**9+7

hash1 = [0]*(n+1)
power1 = [1]*(n+1)

for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
    
lef = 0
rig = n//2 + 1

while rig - lef > 1:
    mid = (rig+lef)//2
    Flag = False
    for i in range(n-2*mid+1):
        if Flag:
            break
        now = gene_hash(i,i+mid)
        for j in range(i+mid,n-mid+1):
            if now == gene_hash(j,j+mid):
                Flag = True
                break
    if Flag:
        lef = mid
    else:
        rig = mid
print(lef)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n , q = map(int,input().split())
lot = [[] for i in range(n)]
ans = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int,input().split())
    lot[a-1].append(b-1)
    lot[b-1].append(a-1)
tyu = [0]*n
for i in range(q):
    p , x = map(int,input().split())
    tyu[p-1] += x

taiki = deque()
taiki.append(0)
owari = []
while  taiki:
    s = taiki.pop()
    for i in lot[s]:
        if owari.count(i) == 0:
            tyu[i] += tyu[s]
            taiki.append(i)
    owari.append(s)
for i in tyu:
    print(i,end=" ")


Fixed submission:
from collections import deque
n , q = map(int,input().split())
lot = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int,input().split())
    lot[a-1].append(b-1)
    lot[b-1].append(a-1)
tyu = [0]*n
for i in range(q):
    p , x = map(int,input().split())
    tyu[p-1] += x

taiki = deque()
taiki.append(0)
owari = [True] * n
while  taiki:
    s = taiki.pop()
    for i in lot[s]:
        if owari[i]:
            tyu[i] += tyu[s]
            taiki.append(i)
    owari[s] = False
print(*tyu)
Buggy python submission (verdict: Runtime Error):
import copy
from collections import deque
sima = [list(input()) for i in range(10)]
riku = 0
ikisaki = [(1,0),(-1,0),(0,1),(0,-1)]

for i in range(10):
    for j in range(10):
        if sima[i][j] == "o":
            riku += 1

for i in range(10):
    for j in range(10):
        if sima[i][j] == "o":
            continue
        cou = 0
        karisima = copy.deepcopy(sima)
        karisima[i][j] = "o"
        d = deque()
        d.append((i,j))
        tansaku = [[True for p in range(10)] for q in range(10)]
        while d:
            y , x = d.popleft()
            tansaku[y][x] = False
            cou += 1
            for ney , nex in ikisaki:
                if 0 <= x + nex <= 9 and 0 <= y + ney <= 9:
                    if tansaku[y+ney][x+nex] and karisima[y+ney][x+nex] == "o":
                        d.append((y+ney,x+nex))
        if cou == riku + 1:
            print("YES")
            exit()
print("NO")

Fixed submission:
from bisect import bisect_left,bisect
s = list(input())
sn = len(s)
t = list(input())
tn = len(t)
kouho = []
for i in range(26):
    kar = []
    now = chr(ord('a') + i)
    for j in range(sn):
        if s[j] == now:
            kar.append(j)
    kouho.append(kar)

cou = 0
now = -1
for i in range(tn):
    u = ord(t[i])-ord("a")
    if kouho[u]:
        tugi = bisect(kouho[u],now)
        if tugi == len(kouho[u]):
            cou += 1
            now = kouho[u][0]
        else:
            now = kouho[u][tugi]
    else:
        print(-1)
        exit()

print(cou*sn + now + 1)
Buggy python submission (verdict: Runtime Error):
import heapq
n , m = map(int,input().split())
kouho = [[] for i in range(m)]
hosyu = [0 for i in range(m)]
for i in range(n):
    a , b = map(int,input().split())
    kouho[a-1].append(-b)
for i in range(m):
    now = i
    p = 0
    heapq.heapify(kouho[i])
    while kouho[i]:
        if now == m:
            break
        if p == 0:
            p = heapq.heappop(kouho[i])
        if hosyu[now] > p:
            hosyu[now] = p
            p = 0
        now += 1
print(abs(sum(hosyu)))

Fixed submission:
import heapq
n , m = map(int,input().split())
kouho = [[] for i in range(m)]
for i in range(n):
    a , b = map(int,input().split())
    if a <= m:
        kouho[a-1].append(-b)
ans = 0
p = []
heapq.heapify(p)

for i in range(m):

    for j in kouho[i]:
        heapq.heappush(p,j)
    if p:
        ans -= heapq.heappop(p)
print(ans)

Buggy python submission (verdict: Runtime Error):
import heapq
n , m = map(int,input().split())
kouho = [[] for i in range(m)]
hosyu = [0 for i in range(m)]
for i in range(n):
    a , b = map(int,input().split())
    kouho[a-1].append(-b)
for i in range(m):
    now = i
    p = 0
    heapq.heapify(kouho[i])
    while kouho[i]:
        if now == m:
            break
        if p == 0:
            p = heapq.heappop(kouho[i])
        if hosyu[now] > p:
            hosyu[now] = p
            p = 0
        now += 1
print(abs(sum(hosyu)))

Fixed submission:
import heapq
n , m = map(int,input().split())
kouho = [[] for i in range(m)]
for i in range(n):
    a , b = map(int,input().split())
    if a <= m:
        kouho[a-1].append(-b)
ans = 0
p = []
heapq.heapify(p)

for i in range(m):

    for j in kouho[i]:
        heapq.heappush(p,j)
    if p:
        ans -= heapq.heappop(p)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
mod = 10**9 + 7
dp = [[0 for i in range(13)] for i in range(n)]
if s[n-1] == "?":
    for i in range(10):
        dp[n-1][i] = 1
else:
    dp[n-1][int(s[n-1])] = 1

for i in reversed(range(n-1)):
    if s[i] == "?":
        for j in range(10):
            amari = (j * (10**(n-1-i))) % 13
            for k in range(13):
                dp[i][(k+amari)%13] += dp[i+1][k]
                dp[i][(k+amari)%13] %= mod
    else:
        amari =(int(s[i]) * (10**(n-1-i))) % 13
        for k in range(13):
            dp[i][(k+amari)%13] += dp[i+1][k]
            dp[i][(k+amari)%13] %= mod
print(dp[0][5])

Fixed submission:
s = input()
n = len(s)
mod = 10**9 + 7
dp = [[0 for i in range(13)] for i in range(n)]
if s[n-1] == "?":
    for i in range(10):
        dp[n-1][i] = 1
else:
    dp[n-1][int(s[n-1])] = 1
now = 1
for i in reversed(range(n-1)):
    now *= 10
    now %= 13
    if s[i] == "?":
        for j in range(10):
            amari = (j * now) % 13
            for k in range(13):
                dp[i][(k+amari)%13] += dp[i+1][k]
                dp[i][(k+amari)%13] %= mod
    else:
        amari =(int(s[i]) * now) % 13
        for k in range(13):
            dp[i][(k+amari)%13] += dp[i+1][k]
            dp[i][(k+amari)%13] %= mod
print(dp[0][5])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
mod = 10**9 + 7
dp = [[0 for i in range(13)] for i in range(n)]
if s[n-1] == "?":
    for i in range(10):
        dp[n-1][i] = 1
else:
    dp[n-1][int(s[n-1])] = 1

for i in reversed(range(n-1)):
    if s[i] == "?":
        for j in range(10):
            amari = (j * (10**(n-1-i))) % 13
            for k in range(13):
                dp[i][(k+amari)%13] += dp[i+1][k]
                dp[i][(k+amari)%13] %= mod
    else:
        amari =(int(s[i]) * (10**(n-1-i))) % 13
        for k in range(13):
            dp[i][(k+amari)%13] += dp[i+1][k]
            dp[i][(k+amari)%13] %= mod
print(dp[0][5])

Fixed submission:
s = input()
n = len(s)
mod = 10**9 + 7
dp = [[0 for i in range(13)] for i in range(n)]
if s[n-1] == "?":
    for i in range(10):
        dp[n-1][i] = 1
else:
    dp[n-1][int(s[n-1])] = 1
now = 1
for i in reversed(range(n-1)):
    now *= 10
    now %= 13
    if s[i] == "?":
        for j in range(10):
            amari = (j * now) % 13
            for k in range(13):
                dp[i][(k+amari)%13] += dp[i+1][k]
                dp[i][(k+amari)%13] %= mod
    else:
        amari =(int(s[i]) * now) % 13
        for k in range(13):
            dp[i][(k+amari)%13] += dp[i+1][k]
            dp[i][(k+amari)%13] %= mod
print(dp[0][5])
Buggy python submission (verdict: Runtime Error):
m = int(input())
print("0 0 1000000000 1 " + str(10**9-(m%(10**9)))+ " " + str(m//(10**9) + 1)))

Fixed submission:
s = int(input())
x = s % 10**9
y = s // 10**9
if x > 0:
    x -= 10**9
    y += 1
print(0,0,10**9,1,-x,y)

Buggy python submission (verdict: Time Limit Exceeded):
m = int(input())
for i in range(1,int(m**0.5)+1):
    if m % i == 0 and m//i <= 10**9:
        print("0 0 " + str(m//i) +" 0 0 " + str(i))
        exit()

Fixed submission:
s = int(input())
x = s % 10**9
y = s // 10**9
if x > 0:
    x -= 10**9
    y += 1
print(0,0,10**9,1,-x,y)

Buggy python submission (verdict: Time Limit Exceeded):
m = int(input())
for i in range(1,int(m**0.5)+1):
    if m % i == 0 and m//i <= 10**9:
        print("0 0 " + str(m//i) +" 0 0 " + str(i))
        exit()

Fixed submission:
s = int(input())
x = s % 10**9
y = s // 10**9
if x > 0:
    x -= 10**9
    y += 1
print(0,0,10**9,1,-x,y)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

b = sorted(a,reverse=True)

if b[0] == b[1]:
    for i in range(n):
        print(b[0])
else:
    s = max(a)
    for i in range(n):
        if i == a.index(s):
            print(b[1])
        else:
            print(s)

Fixed submission:
n = int(input())
a = [int(input()) for i in range(n)]
s = max(a)
l = a.index(max(a))
a[l] = 0
t = max(a)

for i in range(n):
    if i == l:
        print(t)
    else:
        print(s)
Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left,bisect

n = int(input())
p = [[int(input())]]
for i in range(n-1):
    now = int(input())
    if now > p[0][-1]:
        p[0].append(now)
    else:
        t = bisect_left(p[0],now)
        k = p[0][t]
        p[0][t] = now
        cou = 1
        while True:
            if cou <= len(p) - 1:
                if k > p[cou][-1]:
                    p[cou].append(k)
                    break
                else:
                    t = bisect_left(p[cou],k)
                    nextk = p[cou][t]
                    p[cou][t] = k
                    k = nextk
                    cou += 1
            elif cou == len(p):
                p.append([k])
                break
print(len(p))

Fixed submission:
from bisect import bisect_left,bisect
 
n = int(input())
p = [-1*int(input())]
for i in range(n-1):
    now = -1*int(input())
    if now >= p[-1]:
        p.append(now)
    else:
        p[bisect(p,now)] = now
print(len(p))

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left,bisect

n = int(input())
p = [[int(input())]]
for i in range(n-1):
    now = int(input())
    if now > p[0][-1]:
        p[0].append(now)
    else:
        t = bisect_left(p[0],now)
        k = p[0][t]
        p[0][t] = now
        cou = 1
        while True:
            if cou <= len(p) - 1:
                if k > p[cou][-1]:
                    p[cou].append(k)
                    break
                else:
                    t = bisect_left(p[cou],k)
                    nextk = p[cou][t]
                    p[cou][t] = k
                    k = nextk
                    cou += 1
            elif cou == len(p):
                p.append([k])
                break
print(len(p))

Fixed submission:
from bisect import bisect_left,bisect
 
n = int(input())
p = [-1*int(input())]
for i in range(n-1):
    now = -1*int(input())
    if now >= p[-1]:
        p.append(now)
    else:
        p[bisect(p,now)] = now
print(len(p))
Buggy python submission (verdict: Runtime Error):
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)

if len(c) > 3:
    print("No")
    exit()
elif len(c) == 3:
    if n % 3 != 0:
        print("No")
        exit()
    p = []
    for k , v in c.items():
        if v != n//3:
            print("No")
            exit()
        p.append(k)
    if (p[0] ^ p[1]) ^ p[2] != 0:
        print("No")
        exit()
elif len(c) == 2:
    if n % 3 != 0:
        print("No")
        exit()
    for k,v in c.items():
        if k == 0:
            if v != n//3:
                print("No")
                exit()
        elif:
            if v != 2*n//3:
                print("No")
                exit()
elif len(c) == 1:
    for k in c.keys():
        if k != 0:
            print("No")
            exit()
            
print("Yes")

Fixed submission:
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)

if len(c) > 3:
    print("No")
    exit()
elif len(c) == 3:
    if n % 3 != 0:
        print("No")
        exit()
    p = []
    for k , v in c.items():
        if v != n//3:
            print("No")
            exit()
        p.append(k)
    if (p[0] ^ p[1]) ^ p[2] != 0:
        print("No")
        exit()
elif len(c) == 2:
    if n % 3 != 0:
        print("No")
        exit()
    for k,v in c.items():
        if k == 0:
            if v != n//3:
                print("No")
                exit()
        else:
            if v != 2*n//3:
                print("No")
                exit()
elif len(c) == 1:
    for k in c.keys():
        if k != 0:
            print("No")
            exit()
            
print("Yes")
Buggy python submission (verdict: Runtime Error):
l , r = map(int,input().split())

if r - l >= 2020:
    print(0)
else:
    for i in range(l,r):
        for j in range(i+1,r+1):
            p.append((i*j)%2019)
    print(min(p))

Fixed submission:
l , r = map(int,input().split())
p = []
if r - l >= 2018:
    print(0)
else:
    for i in range(l,r):
        for j in range(i+1,r+1):
            p.append((i*j)%2019)
    print(min(p))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
a.sort()
aminus = []
aplus = []
p = 0
q = 0
for i in a:
    if i >= 0:
        aplus.append(i)
        p += 1
    elif i < 0:
        aminus.append(i)
        q += 1
ans = []
cou = 0
if aplus and aminus:
    now = aminus[0]
    for i in range(p-1):
        ans.append((now,aplus[i]))
        now = now - aplus[i]
    aminus[0] = now
    now = aplus[p-1]
    for i in range(q):
        ans.append((now,aminus[i]))
        now = now - aminus[i]
    cou = now
elif aplus:
    now = aplus[0]
    for i in range(1,p-1):
        ans.append((now,aplus[i]))
        now = now - aplus[i]
    ans.append((aplus[p-1],now))
    cou = aplus[p-1] - now
elif aminus:
    now = aminus[q-1]
    for i in range(q-1):
        ans.append(now,aminus[i])
        now = now - aminus[i]
    cou = now
print(cou)
for i in range(n-1):
    print(*ans[i])


Fixed submission:
n = int(input())
a = list(map(int,input().split()))
a.sort()
aminus = []
aplus = []
p = 0
q = 0
for i in a:
    if i >= 0:
        aplus.append(i)
        p += 1
    elif i < 0:
        aminus.append(i)
        q += 1
ans = []
cou = 0
if aplus and aminus:
    now = aminus[0]
    for i in range(p-1):
        ans.append((now,aplus[i]))
        now = now - aplus[i]
    aminus[0] = now
    now = aplus[p-1]
    for i in range(q):
        ans.append((now,aminus[i]))
        now = now - aminus[i]
    cou = now
elif aplus:
    now = aplus[0]
    for i in range(1,p-1):
        ans.append((now,aplus[i]))
        now = now - aplus[i]
    ans.append((aplus[p-1],now))
    cou = aplus[p-1] - now
elif aminus:
    now = aminus[q-1]
    for i in range(q-1):
        ans.append((now,aminus[i]))
        now = now - aminus[i]
    cou = now

print(cou)
for i in range(n-1):
    print(*ans[i])
Buggy python submission (verdict: Runtime Error):
import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)

for i in range(m):
    b , c = map(int,input().split())
    for j in range(b):
        p = heapq.heappop(a)
        if p >= c:
            heapq.heappush(a,p)
            break
        else:
            heapq.heappush(a,c)

print(sum(a))

Fixed submission:
a , b = map(int,input().split())
if a >= 13:
    print(b)
elif 6 <= a <= 12:
    print(b//2)
else:
    print(0)
Buggy python submission (verdict: Time Limit Exceeded):
n , m = map(int,input().split())
p = {i+1 for i in range(n)}
for i in range(m):
    l , r = map(int,input().split())
    s = {l+i for i in range(r-l+1)}
    p = p & s

print(len(p))


Fixed submission:
n , m = map(int,input().split())
l=[]
r=[]
for i in range(m):
    x , y = map(int,input().split())
    l.append(x)
    r.append(y)

a = max(l)
b = min(r)
if a > b:
    print(0)
else:
    print(b-a+1)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)

for i in range(m):
    b , c = map(int,input().split())
    for j in range(b):
        p = heapq.heappop(a)
        if p >= c:
            heapq.heappush(a,p)
            break
        else:
            heapq.heappush(a,c)

print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(lambda x: [int(x),1], input().split()))

for i in range(m):
    b , c = map(int,input().split())
    a.append([c,b])
a.sort(reverse = True)

ans = 0
for x,y in a:
    if y < n:
        ans += x*y
        n = n-y
    elif y >=n:
        ans += x*n
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)

for i in range(m):
    b , c = map(int,input().split())
    for j in range(b):
        p = heapq.heappop(a)
        if p >= c:
            heapq.heappush(a,p)
            break
        else:
            heapq.heappush(a,c)

print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(lambda x: [int(x),1], input().split()))

for i in range(m):
    b , c = map(int,input().split())
    a.append([c,b])
a.sort(reverse = True)

ans = 0
for x,y in a:
    if y < n:
        ans += x*y
        n = n-y
    elif y >=n:
        ans += x*n
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))

for i in range(m):
    b , c = map(int,input().split())
    a.extend([c for i in range(b)])

a.sort(reverse = True)

print(sum(a[:n]))

Fixed submission:
n, m = map(int, input().split())
a = list(map(lambda x: [int(x),1], input().split()))

for i in range(m):
    b , c = map(int,input().split())
    a.append([c,b])
a.sort(reverse = True)

ans = 0
for x,y in a:
    if y < n:
        ans += x*y
        n = n-y
    elif y >=n:
        ans += x*n
        break
print(ans)
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
r , g , b , n = map(int,input().split())
ans = 0
R = -(-n//r)
G = -(-n//g)
for i in range(R+1):
    for j in range(G+1):
        su = n-r*i-g*j
        if n-r*i-g*j < 0:
            break
        else:
            if (su/b).is_integer():
                ans += 1
print(ans)

Fixed submission:
n,k=map(int,input().split())
print(n-k+1)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h,w = map(int,input().split())
a = [list(input()) for i in range(h)]
visit = [[-1 for i in range(w)] for j in range(h)]
d = deque()

for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            visit[i][j] = 0
            d.append((i,j))

p = [(1,0),(-1,0),(0,1),(0,-1)]
ans = 0
while d:
    x,y = d.popleft()
    for s,t in p:
        if 0 <= x+s <= h-1 and 0 <= y+t <= w-1:
            if visit[x+s][y+t] == -1:
                visit[x+s][y+t] = visit[x][y] + 1
                ans = max(ans,visit[x][y]+1)
                d.append((x+s,y+t))
        
print(ans)

Fixed submission:
from collections import deque
h,w = map(int,input().split())
a = [list(input()) for i in range(h)]
visit = [[-1 for i in range(w)] for j in range(h)]
d = deque()

for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            visit[i][j] = 0
            d.append((i,j))

p = [(1,0),(-1,0),(0,1),(0,-1)]
ans = 0
while d:
    x,y = d.popleft()
    for s,t in p:
        if 0 <= x+s <= h-1 and 0 <= y+t <= w-1:
            if visit[x+s][y+t] == -1:
                visit[x+s][y+t] = visit[x][y] + 1
                ans = max(ans,visit[x][y]+1)
                d.append((x+s,y+t))
        
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input()
print(min(s[s.find("#"):s.rfind(".")].count("#"),s[s.find("#"):s.rfind(".")+1].count("."))

Fixed submission:
n = int(input())
s = input()
migi = s.count(".")
hidari = 0
ans = migi
for i in range(n):
    if s[i] == "#":
        hidari += 1
    elif s[i] == ".":
        migi -= 1
    ans = min(ans,hidari+migi)
print(ans)