Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def make_unique(list_):
	set_ = set()
	return [x for x in list_ if x not in set_ and not set_.add(x)]

n = int(input())
v = [int(c) for c in input().split()]
vo = v[::2]
ve = v[1::2]
# set(vo) is heavy
counts_o = [(elm, vo.count(elm)) for elm in make_unique(vo)]
counts_e = [(elm, ve.count(elm)) for elm in make_unique(ve)]
counts_o.append((-1, 0))
counts_e.append((-2, 0))
counts_o.sort(key=lambda x:x[1])
counts_e.sort(key=lambda x:x[1])
if counts_o[-1][0] != counts_e[-1][0]:
	print(n - counts_o[-1][1] - counts_e[-1][1])
else:
	res1 = n - counts_o[-1][1] - counts_e[-2][1]
	res2 = n - counts_o[-2][1] - counts_e[-1][1]
	print(min(res1, res2))

Fixed submission:
from collections import Counter

n = int(input())
v = [int(c) for c in input().split()]
vo = v[::2]
ve = v[1::2]
co = Counter(vo).most_common()
ce = Counter(ve).most_common()
co.append((-1, 0))
ce.append((-2, 0))
if co[0][0] != ce[0][0]:
	print(n - co[0][1] - ce[0][1])
else:
	res1 = n - co[0][1] - ce[1][1]
	res2 = n - co[1][1] - ce[0][1]
	print(min(res1, res2))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
v = [int(c) for c in input().split()]
vo = v[::2]
ve = v[1::2]
counts_o = [(elm, vo.count(elm)) for elm in set(vo)]
counts_e = [(elm, ve.count(elm)) for elm in set(ve)]
counts_o.append((-1, 0))
counts_e.append((-2, 0))
counts_o.sort(key=lambda x:x[1])
counts_e.sort(key=lambda x:x[1])
if counts_o[-1][0] != counts_e[-1][0]:
	print(n - counts_o[-1][1] - counts_e[-1][1])
else:
	res1 = n - counts_o[-1][1] - counts_e[-2][1]
	res2 = n - counts_o[-2][1] - counts_e[-1][1]
	print(min(res1, res2))

Fixed submission:
from collections import Counter

n = int(input())
v = [int(c) for c in input().split()]
vo = v[::2]
ve = v[1::2]
co = Counter(vo).most_common()
ce = Counter(ve).most_common()
co.append((-1, 0))
ce.append((-2, 0))
if co[0][0] != ce[0][0]:
	print(n - co[0][1] - ce[0][1])
else:
	res1 = n - co[0][1] - ce[1][1]
	res2 = n - co[1][1] - ce[0][1]
	print(min(res1, res2))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
v = [int(c) for c in input().split()]
vo = v[::2]
ve = v[1::2]
counts_o = [(elm, vo.count(elm)) for elm in set(vo)]
counts_e = [(elm, ve.count(elm)) for elm in set(ve)]
counts_o.append((-1, 0))
counts_e.append((-2, 0))
counts_o.sort(key=lambda x:x[1])
counts_e.sort(key=lambda x:x[1])
if counts_o[-1][0] != counts_e[-1][0]:
	print(n - counts_o[-1][1] - counts_e[-1][1])
else:
	res1 = n - counts_o[-1][1] - counts_e[-2][1]
	res2 = n - counts_o[-2][1] - counts_e[-1][1]
	print(min(res1, res2))

Fixed submission:
from collections import Counter

n = int(input())
v = [int(c) for c in input().split()]
vo = v[::2]
ve = v[1::2]
co = Counter(vo).most_common()
ce = Counter(ve).most_common()
co.append((-1, 0))
ce.append((-2, 0))
if co[0][0] != ce[0][0]:
	print(n - co[0][1] - ce[0][1])
else:
	res1 = n - co[0][1] - ce[1][1]
	res2 = n - co[1][1] - ce[0][1]
	print(min(res1, res2))
Buggy python submission (verdict: Runtime Error):
# import sys
from math import factorial
from functools import reduce


def mmul(a, b, mod):
    return (a % mod) * (b % mod) % mod


def mfact(a, mod):
    fact = 1
    for m in range(2, a + 1):
        fact *= m % mod
        fact %= mod
    return fact


def mpow(a, b, mod):
    if b == 0:
        return 1
    if b == 1:
        return a % mod
    if b % 2 == 0:
        return mpow(a, b / 2, mod) ** 2 % mod
    return mpow(a, b // 2, mod) ** 2 * a % mod


def mdiv(a, b, mod):
    return mmul(a, mpow(b, mod - 2, mod), mod)


# (n+a)Ca = (n+a)! / n!a! mod mod for n >> a
def mnpaca(n, a, mod):
    res = 1
    for num in range(n + 1, n + a + 1):
        res *= num % mod
        res %= mod
    return mdiv(res, factorial(a), mod)

mod = 1000000007
N, M = [int(s) for s in input().split()]

prime_counts = []
i = 2
while i * i <= M:
    count = 0
    while M % i == 0:
        M /= i
        count += 1
    if count:
        prime_counts.append(count)
    i += 1
if M > 1:
    prime_counts.append(1)

res = reduce(lambda x, y: x * y, [mnpaca(N - 1, c, mod) for c in prime_counts])

print(res % mod)


Fixed submission:
# import sys
from math import factorial
from functools import reduce


def mmul(a, b, mod):
    return (a % mod) * (b % mod) % mod


def mfact(a, mod):
    fact = 1
    for m in range(2, a + 1):
        fact *= m % mod
        fact %= mod
    return fact


def mpow(a, b, mod):
    if b == 0:
        return 1
    if b == 1:
        return a % mod
    if b % 2 == 0:
        return mpow(a, b / 2, mod) ** 2 % mod
    return mpow(a, b // 2, mod) ** 2 * a % mod


def mdiv(a, b, mod):
    return mmul(a, mpow(b, mod - 2, mod), mod)


# (n+a)Ca = (n+a)! / n!a! mod mod for n >> a
def mnpaca(n, a, mod):
    res = 1
    for num in range(n + 1, n + a + 1):
        res *= num % mod
        res %= mod
    return mdiv(res, factorial(a), mod)

mod = 1000000007
N, M = [int(s) for s in input().split()]

prime_counts = []
i = 2
while i * i <= M:
    count = 0
    while M % i == 0:
        M /= i
        count += 1
    if count:
        prime_counts.append(count)
    i += 1
if M > 1:
    prime_counts.append(1)

res = reduce(lambda x, y: x * y, [mnpaca(N - 1, c, mod) for c in prime_counts],
             1)

print(res % mod)
Buggy python submission (verdict: Runtime Error):
import sys
# Python < 3.5
# from math import gcd
# Python >= 3.5
from fractions import gcd

def gcdall(*args):
  if len(args) == 1:
    return args[0]
  return gcdall(gcd(args[0], args[1]), *args[2:])

N, X = map(int, input().split())
d = [abs(int(s) - X) for s in input().split()]

print(gcdall(*d))


Fixed submission:
from functools import reduce
# Python < 3.5
# from math import gcd
# Python >= 3.5
from fractions import gcd

N, X = map(int, input().split())
d = [abs(int(s) - X) for s in input().split()]

print(reduce(gcd, d, 0))
Buggy python submission (verdict: Time Limit Exceeded):
mod = 1000000007

S = input()
n = 1
a = 0
ab = 0
abc = 0
for s in S:
    if s in "A":
        a += n
    if s in "B":
        ab += a
    if s == "C":
        abc += ab
    if s in "?":
        n, a, ab, abc = 3 * n, 3 * a + n, 3 * ab + a, 3 * abc + ab

print(abc % mod)


Fixed submission:
mod = 1000000007

S = input()
n = 1
a = 0
ab = 0
abc = 0
for s in S:
    if s in "A":
        a += n
    if s in "B":
        ab += a
    if s == "C":
        abc += ab
    if s in "?":
        n, a, ab, abc = 3 * n, 3 * a + n, 3 * ab + a, 3 * abc + ab
    n %= mod
    a %= mod
    ab %= mod
    abc %= mod

print(abc % mod)

Buggy python submission (verdict: Time Limit Exceeded):
mod = 1000000007

S = input()
n = 1
a = 0
ab = 0
abc = 0
for s in S:
    if s in "A":
        a += n
    if s in "B":
        ab += a
    if s == "C":
        abc += ab
    if s in "?":
        n, a, ab, abc = 3 * n, 3 * a + n, 3 * ab + a, 3 * abc + ab

print(abc % mod)


Fixed submission:
mod = 1000000007

S = input()
n = 1
a = 0
ab = 0
abc = 0
for s in S:
    if s in "A":
        a += n
    if s in "B":
        ab += a
    if s == "C":
        abc += ab
    if s in "?":
        n, a, ab, abc = 3 * n, 3 * a + n, 3 * ab + a, 3 * abc + ab
    n %= mod
    a %= mod
    ab %= mod
    abc %= mod

print(abc % mod)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(s) for s in input().split()]
S = [[0] * 20 for _ in range(N + 1)]
for i in range(1, N + 1):
    for j in range(20):
        S[i][j] = S[i-1][j] + (1 if A[i-1] & (1 << j) else 0)

ans = 0
i = 0
for j in range(N):
    while not all(S[j + 1][v] - S[i][v] < 2 for v in range(20)):
        i += 1
    ans += j + 1 - i
print(ans)


Fixed submission:
N = int(input())
A = [int(s) for s in input().split()]
S = [[0] * 20 for _ in range(N + 1)]
for i in range(1, N + 1):
    for j in range(20):
        S[i][j] = S[i-1][j] + (1 if A[i-1] & (1 << j) else 0)

ans = 0
i = 0
for j in range(N):
    while not all(S[j + 1][v] - S[i][v] < 2 for v in range(20)):
        i += 1
    ans += j + 1 - i
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import nsmallest

s = input()
K = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, len(s) + 1):
        subs.add(s[i:j])
arr = list(subs)
print(nsmallest(K, arr)[-1])


Fixed submission:
from heapq import nsmallest

s = input()
K = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, min(i + 6, len(s) + 1)):
        subs.add(s[i:j])
arr = list(subs)
print(nsmallest(K, arr)[-1])

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

s = input()
K = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, len(s) + 1):
        subs.add(s[i:j])
arr = list(subs)
print(nsmallest(K, arr)[-1])


Fixed submission:
from heapq import nsmallest

s = input()
K = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, min(i + 6, len(s) + 1)):
        subs.add(s[i:j])
arr = list(subs)
print(nsmallest(K, arr)[-1])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
K = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, len(s) + 1):
        subs.add(s[i:j])
arr = list(subs)
arr.sort()
print(arr[K-1])


Fixed submission:
from heapq import nsmallest

s = input()
K = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, min(i + 6, len(s) + 1)):
        subs.add(s[i:j])
arr = list(subs)
print(nsmallest(K, arr)[-1])
Buggy python submission (verdict: Runtime Error):
A, B, K = map(int, input().split())
if B - A < 2 * K:
    ans = [*range(A, B + 1)]
else:
    ans = [*range(A, A + K), *range(B - K + 1, B + 1)]
for v in ans:
    print(v)


Fixed submission:
A, B, K = map(int, input().split())
if B - A < 2 * K:
    # ans = [*range(A, B + 1)]
    ans = list(range(A, B + 1))
else:
    # ans = [*range(A, A + K), *range(B - K + 1, B + 1)]
    ans = list(range(A, A + K)) + list(range(B - K + 1, B + 1))
for v in ans:
    print(v)

Buggy python submission (verdict: Runtime Error):
A, B, K = map(int, input().split())
if B - A < 2 * K:
    # print(*range(A, B + 1))
    print(*list(range(A, B + 1)))
else:
    # print(*range(A, A + K), *range(B - K + 1, B + 1))
    print(*list(range(A, A + K)), *list(range(B - K + 1, B + 1)))


Fixed submission:
A, B, K = map(int, input().split())
if B - A < 2 * K:
    # ans = [*range(A, B + 1)]
    ans = list(range(A, B + 1))
else:
    # ans = [*range(A, A + K), *range(B - K + 1, B + 1)]
    ans = list(range(A, A + K)) + list(range(B - K + 1, B + 1))
for v in ans:
    print(v)

Buggy python submission (verdict: Runtime Error):
A, B, K = map(int, input().split())
if B - A < 2 * K:
    print(*range(A, B + 1))
else:
    print(*range(A, A + K), *range(B - K + 1, B + 1))


Fixed submission:
A, B, K = map(int, input().split())
if B - A < 2 * K:
    # ans = [*range(A, B + 1)]
    ans = list(range(A, B + 1))
else:
    # ans = [*range(A, A + K), *range(B - K + 1, B + 1)]
    ans = list(range(A, A + K)) + list(range(B - K + 1, B + 1))
for v in ans:
    print(v)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        numer = N * h * n
        denom = 4 * h * n - N * n - N * h
        if denom > 0 and numer % denom == 0:
            print(h, n, numer // denom)
            sys.exit()


Fixed submission:
import sys
N = int(input())
for h in range(1, 3501):
    for n in range(1, 3501):
        numer = N * h * n
        denom = 4 * h * n - N * n - N * h
        if denom > 0 and numer % denom == 0:
            print(h, n, numer // denom)
            sys.exit()
Buggy python submission (verdict: Runtime Error):
import sys
from math import log2

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

AB = []
for _ in range(N):
    AB.append([int(s) for s in input().split()])

ans = sum([b for a, b in AB if K | a == K])
for i in range(int(log2(K) // 1), 0, -1):
    if not K & (1 << i):
        continue
    km = K & ~(1 << i) | (1 << i) - 1
    anscand = sum([b for a, b in AB if km | a == km])
    ans = max(ans, anscand)
print(ans)


Fixed submission:
import sys
N, K = map(int, input().split())

AB = []
for _ in range(N):
    AB.append([int(s) for s in input().split()])

ans = 0
kbin = bin(K)[2:]
for a, b in AB:
    if K | a == K:
        ans += b
for i in range(1, len(kbin)):
    if kbin[i - 1] == "0":
        continue
    kl = list(kbin)
    kl[i - 1] = "0"
    for j in range(i, len(kbin)):
        kl[j] = "1"
    km = int("".join(kl), 2)
    anscand = 0
    for a, b in AB:
        if km | a == km:
            anscand += b
    if ans < anscand:
        ans = anscand
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
inf = float("inf")
N = int(input())
A = []
for _ in range(N):
    a = [int(s) for s in input().split()]
    A.append(a)
d = [a[:] for a in A]
ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        dmin = min([d[i][k] + d[k][j]
                    for k in range(N) if k not in (i, j)], default=inf)
        if d[i][j] > dmin:
            print(-1)
            exit()
        if d[i][j] != dmin:
            ans += d[i][j]
print(ans)


Fixed submission:
inf = float("inf")
N = int(input())
A = []
for _ in range(N):
    a = [int(s) for s in input().split()]
    A.append(a)
d = [a[:] for a in A]
ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        dmin = min([inf] + [d[i][k] + d[k][j]
                            for k in range(N) if k not in (i, j)])
        if d[i][j] > dmin:
            print(-1)
            exit()
        if d[i][j] != dmin:
            ans += d[i][j]
print(ans)

Buggy python submission (verdict: Runtime Error):
inf = float("inf")
N = int(input())
A = []
for _ in range(N):
    a = [int(s) for s in input().split()]
    A.append(a)
d = [a[:] for a in A]
ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        dmin = min([d[i][k] + d[k][j] for k in range(N) if k not in (i, j)])
        if d[i][j] > dmin:
            print(-1)
            exit()
        if d[i][j] != dmin:
            ans += d[i][j]
print(ans)


Fixed submission:
inf = float("inf")
N = int(input())
A = []
for _ in range(N):
    a = [int(s) for s in input().split()]
    A.append(a)
d = [a[:] for a in A]
ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        dmin = min([inf] + [d[i][k] + d[k][j]
                            for k in range(N) if k not in (i, j)])
        if d[i][j] > dmin:
            print(-1)
            exit()
        if d[i][j] != dmin:
            ans += d[i][j]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations

inf = float("inf")

N, M, R = map(int, input().split())
r = [int(s) - 1 for s in input().split()]
d = [[inf] * N for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    d[a - 1][b - 1] = c
    d[b - 1][a - 1] = c
for k in range(N):
    for i in range(N):
        for j in range(N):
            d[i][j] = d[j][i] = min(d[i][j], d[i][k] + d[k][j])
ans = min([sum([d[r[p[i]]][r[p[i+1]]] for i in range(R-1)])
           for p in permutations(range(R))])
print(ans)


Fixed submission:
from itertools import permutations

inf = float("inf")

N, M, R = map(int, input().split())
r = [int(s) - 1 for s in input().split()]
d = [[inf] * N for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    d[a - 1][b - 1] = c
    d[b - 1][a - 1] = c
for k in range(N):
    for i in range(N):
        for j in range(N):
            d[i][j] = d[j][i] = min(d[i][j], d[i][k] + d[k][j])
ans = min([sum([d[r[p[i]]][r[p[i+1]]] for i in range(R-1)])
           for p in permutations(range(R))])
print(ans)
Buggy python submission (verdict: Runtime Error):
from itertools import zip_longest
print("".join(sum(zip_longest(input(), input()), tuple())))


Fixed submission:
from itertools import zip_longest
print("".join(sum(zip_longest(input(), input(), fillvalue=""), tuple())))
Buggy python submission (verdict: Time Limit Exceeded):
inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))


def cost(bits):
    sa = sb = sc = 0
    for i, (a, b, c) in enumerate(ABC):
        if not bits & 1 << i:
            continue
        sa += a
        sb += b
        sc += c
    return sc if sa * Mb == sb * Ma else inf


ans = min([cost(bits) for bits in range(1, 1 << N)])
print(ans if ans < inf else -1)


Fixed submission:
inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
dp = [[inf] * 420 for _ in range(420)]
dp[0][0] = 0
for a, b, c in ABC:
    for i in range(410, -1, -1):
        for j in range(410, -1, -1):
            if dp[i][j] < inf:
                dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + c)
ans = inf
n = 1
while max(Ma * n, Mb * n) < 420:
    ans = min(ans, dp[Ma * n][Mb * n])
    n += 1
print(ans if ans < inf else -1)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
l, r = map(int, input().split())
mod = 2019
X = [x%mod for x in list(range(l, r+1))]
X.sort()
ans = X[0]*X[1]
ans %= mod
print(ans)

Fixed submission:
#!/usr/bin/env python
l, r = map(int, input().split())

mod = 2019
if r >= l+mod-1:
    r = l+mod-1
X = [x%mod for x in list(range(l, r+1))]

ans = mod
for i in range(len(X)):
    for j in range(i+1, len(X)):
        ans = min(ans, (X[i] * X[j])%mod)
print(ans)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
W, H, x, y = map(int, input().split())

if x == W/2:
    print(H*W/2, 0)
else:
    b = (H-W)/2
    a = (y-b)/x
    print((a*W+2*b)*W/2, 1)



Fixed submission:
#!/usr/bin/env python
W, H, x, y = map(int, input().split())
a = W*H/2

if 2*x == W and 2*y == H:
    b = 1
else:
    b = 0
print(a, b)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
N = int(input())
A = [int(i) for i in input().split()]
if N == 2:
    print(max(A) - min(A))
    print(max(A), min(A))
else:
    A.sort()
    As = []
    t = A[0]
    for a in A[1:-1]:
        As.append([t, a])
        t = t - a
    ans = A[-1] - t
    As.append([A[-1], t])
    print(ans)
    for a in As:
        print(*a)

Fixed submission:
#!/usr/bin/env python
n, k = map(int, input().split())
if k == 1:
    print(0)
else:
    print(n-k)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
import sys
N, M = map(int, input().split())
A = [int(input()) for i in range(M)]
m = 10**9+7
F = [0, 1]
for i in range(2, N+1):
    F.append(F[i-1]+F[i-2])

for i, a in enumerate(A[:-1]):
    if a+1 == A[i+1]:
        print(0)
        sys.exit()

ans = 1
s = 0
for i in range(N):
    if i in A:
        ans *= F[i-s]
        ans %= m
        s = i+1
ans *= F[N-s+1]
ans %= m 
print(ans)

Fixed submission:
#!/usr/bin/env python
N, M = map(int, input().split())
m = 10**9+7
F = [0]*N
for _ in range(M):
    F[int(input())-1] = 1

a, b = 0, 1
for n in range(N):
    if F[n]:
        a, b = b, 0
    else:
        a, b = b, (a+b) % m
print(b)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
import sys
N, M = map(int, input().split())
A = [int(input()) for i in range(M)]
m = 10**9+7
def Fib(n):
    a, b = 0, 1
    if n == 1:
        return a
    elif n == 2:
        return b
    else:
        for i in range(n-2):
            a, b = b, a + b
        return b

for i, a in enumerate(A[:-1]):
    if a+1 == A[i+1]:
        print(0)
        sys.exit()
ans = 1
s = 0
for i in range(N):
    if i in A:
        ans *= Fib(i-s+1)
        ans %= m
        s = i+1
ans *= Fib(N-s+2)
ans %= m 
print(ans)

Fixed submission:
#!/usr/bin/env python
N, M = map(int, input().split())
m = 10**9+7
F = [0]*N
for _ in range(M):
    F[int(input())-1] = 1

a, b = 0, 1
for n in range(N):
    if F[n]:
        a, b = b, 0
    else:
        a, b = b, (a+b) % m
print(b)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
N, M = map(int, input().split())
l1, r1 = map(int, input().split())
ans = [i for i in range(l1, r1+1)]
for m in range(M-1):
    l1, r1 = map(int, input().split())
    l = [i for i in range(l1, r1+1)]
    ans = list(set(ans) & set(l))
print(len(ans))

Fixed submission:
#!/usr/bin/env python
import numpy as np
N, M = map(int, input().split())
l_max = 1
r_min = 100000
for i in range(M):
    l, r = map(int, input().split())
    if l > l_max:
        l_max = l
    if r < r_min:
        r_min = r
    if r_min < l_max:
        print(0)
        exit()
print(r_min - l_max + 1)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
H, W = map(int, input().split())
A = [[s for s in input()] for i in range(H)]

l = []
for i in range(H):
        for j in range(W):
            if A[i][j] == '#':
                l.append([i,j])

c = 0
for k in range(H*W):
    c += 1
    for i in range(H):
        for j in range(W):
            if A[i][j] == '#':
                if i >= 1 and [i-1,j] not in l:
                    l.append([i-1,j])
                if j >= 1 and [i,j-1] not in l:
                    l.append([i,j-1])
                if i+1 < H and [i+1,j] not in l:
                    l.append([i+1, j])
                if j+1 < H and [i,j+1] not in l:
                    l.append([i, j+1])
    if len(l) == H*W:
        break
    for x in l:
        A[x[0]][x[1]] = '#'
print(c)         

Fixed submission:
#!/usr/bin/env python
H, W = map(int, input().split())

g = [[True] * (W+2)]
for _ in range(H):
    g.append([True] + [x == '#' for x in input()] + [True])
g.append([True] * (W+2))

l = []
for i in range(1, H+1):
    for j in range(1, W+1):
        if g[i][j]:
            l.append((i, j))

c = 0
while l:
    ll = []
    for i,j in l:
        if not g[i+1][j]:
            g[i+1][j] = True
            ll.append((i+1, j))
        if not g[i-1][j]:
            g[i-1][j] = True
            ll.append((i-1, j))
        if not g[i][j+1]:
            g[i][j+1] = True
            ll.append((i, j+1))
        if not g[i][j-1]:
            g[i][j-1] = True
            ll.append((i, j-1))
    c += 1
    l = ll

print(c-1)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
import math
N = int(input())
A = list(map(int, input().split()))
A.sort()

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort(reverse=True)
    return divisors

if N == 2:
    print(max(A))
else:
    min1 = min(A)
    div1 = make_divisors(min1)
    c = 0
    for i in div1:
        for a in A:
            if a % i == 0:
                c += 1
        if c == N-1 or c == N:
            g = i
            break
        c = 0

    m = 0
    for i in range(1, N-1):
        gcd = math.gcd(A[i], A[i+1])
        if gcd > m:
            m = gcd
    print(max(g, m))


Fixed submission:
#!/usr/bin/env python
from fractions import gcd
N = int(input())
A = list(map(int, input().split()))

L = []
R = []
k = 0
for i in range(N):
    k = gcd(k, A[i])
    L.append(k)
k = 0
for j in range(N)[::-1]:
    k = gcd(k, A[j])
    R.append(k)
R[:] = R[::-1]

ans = 0
for i in range(N):
    if i == 0:
        ans = R[1]
    elif i == N-1:
        ans = max(ans, L[N-2])
    else:
        ans = max(ans, gcd(L[i-1], R[i+1]))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
N = int(input())
S = input()

min = N
tmp = 0
if S.count('#') == N or S.count('.') == N:
    print(0)
else:
    for n in range(N):
        tmp = S[:n].count('#') + S[n:].count('.')
        if tmp < min:
            min = tmp
    print(min)

Fixed submission:
#!/usr/bin/env python
N = int(input())
S = input()

if S.count('#') == N or S.count('.') == N:
    print(0)
else:
    l = 0
    r = S.count('.')
    min = l+r
    for n in range(1,N+1):
        if S[n-1] == '#':
            l += 1
        else:
            r -= 1
        if l+r < min:
            min = l+r
    print(min)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
len_before = len(S)
for i in range(len_before):
  S_after = S.replace('01','').replace('10','')
  if '10' in S_after or '01' in S_after:
    S = S_after
  else:
    break
len_after = len(S_after)
print(len_before - len_after)

Fixed submission:
S = input()
zero = S.count('0')
one = len(S) - zero
num = zero if zero <= one else one
print(num * 2)