Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int,input().split())
xs = [0]*m
pre = [0]*m
for x in range(m):
    xs[x] = list(list(map(int,input().split())) + [x] + [x])
xs.sort()

for p in range(1,n+1):
    cnt = 1
    for x in xs:
        if x[0] == p:
            x[3] = cnt
            cnt += 1

for x in sorted(xs, key=lambda x:x[2]):
    print('{0:06d}'.format(x[0]) + '{0:06d}'.format(x[3]))

Fixed submission:
n, m = map(int,input().split())
xs = [0]*m
pre = [0]*m
for x in range(m):
    xs[x] = list(list(map(int,input().split())) + [x] + [x])
xs.sort()

skip = 0
for p in range(1,n+1):
    cnt = 1
    flag = 0
    for x in range(skip,len(xs)):
        if xs[x][0] <= p:
            xs[x][3] = cnt
            cnt += 1
            skip += 1
        else:
            flag = 1
        if flag:
            break


for x in sorted(xs, key=lambda x:x[2]):
    print('{0:06d}'.format(x[0]) + '{0:06d}'.format(x[3]))
Buggy python submission (verdict: Runtime Error):
s = input()
k = int(input())
cnt  = 0
ans  = False
for i in range(k):
    if s[i] != '1':
        print(s[i])
        ans = True
if ans:
    print(1)

Fixed submission:
s = input()
k = int(input())
ans  = True
for i in range(k):
    if s[i] != '1':
        print(s[i])
        ans = False
        break
if ans:
    print(1)

Buggy python submission (verdict: Runtime Error):
import sys

s = input()
k = int(input())
cnt  = 0
ans  = 0
while True:
    if cnt == len(s)-1:
        print("1")
        sys.exit(1)
    if s[cnt] != '1':
        print("{}".format(s[cnt]))
        sys.exit(1)
    cnt += 1

Fixed submission:
s = input()
k = int(input())
ans  = True
for i in range(k):
    if s[i] != '1':
        print(s[i])
        ans = False
        break
if ans:
    print(1)

Buggy python submission (verdict: Runtime Error):
import sys

s = input()
k = int(input())
cnt  = 0
ans  = 0
while True:
    if cnt == len(s):
        print("1")
        sys.exit(1)
    if s[cnt] != '1':
        print(s[cnt])
        sys.exit(1)
    cnt += 1

Fixed submission:
s = input()
k = int(input())
ans  = True
for i in range(k):
    if s[i] != '1':
        print(s[i])
        ans = False
        break
if ans:
    print(1)

Buggy python submission (verdict: Runtime Error):
s = input()
k = int(input())
cnt  = 0
ans  = 0
while True:
    if cnt == len(s)-1:
        print("1")
        exit(1)
    if s[cnt] != '1':
        print(s[cnt])
        exit(1)
    cnt += 1

Fixed submission:
s = input()
k = int(input())
ans  = True
for i in range(k):
    if s[i] != '1':
        print(s[i])
        ans = False
        break
if ans:
    print(1)

Buggy python submission (verdict: Runtime Error):
s = input()
k = int(input())
cnt  = 0
ans  = 0
while True:
    if cnt == len(s):
        print("1")
        exit(1)
    if s[cnt] != '1':
        print(s[cnt])
        exit(1)
    cnt += 1

Fixed submission:
s = input()
k = int(input())
ans  = True
for i in range(k):
    if s[i] != '1':
        print(s[i])
        ans = False
        break
if ans:
    print(1)
Buggy python submission (verdict: Runtime Error):
a = map(int,input().split())
Min = min(a)
Max = max(a)
Mid = sum(a) - Max - Min

ans = Mid-Min + Max-Mid
print(ans)

Fixed submission:
a = list(map(int,input().split()))
Min = min(a)
Max = max(a)
Mid = sum(a) - Max - Min
 
ans = Mid-Min + Max-Mid
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
a, b, k = map(int,input().split())
xs = [x for x in range(a,b+1)]
xs = sorted(list(set(xs[:k] + xs[-k:])))
for x in xs:
    print(x)

Fixed submission:
a, b, k = map(int, input().split())
if b-a+1 < 2*k:
    for i in range(a,b+1):
        print(i)
    exit(0)
for i in range(a,a+k):
    print(i)
for i in range(b-k+1,b+1):
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
a, b, k = map(int,input().split())
xs = [x for x in range(a,b+1) if x < a+k or x > b-k]
for x in xs:
    print(x)

Fixed submission:
a, b, k = map(int, input().split())
if b-a+1 < 2*k:
    for i in range(a,b+1):
        print(i)
    exit(0)
for i in range(a,a+k):
    print(i)
for i in range(b-k+1,b+1):
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
a, b, k = map(int,input().split())
xs = [x for x in range(a,b+1) if x < a+k or x > b-k]
for x in range(len(xs)):
    print(xs[x])

Fixed submission:
a, b, k = map(int, input().split())
if b-a+1 < 2*k:
    for i in range(a,b+1):
        print(i)
    exit(0)
for i in range(a,a+k):
    print(i)
for i in range(b-k+1,b+1):
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
a, b, k = map(int,input().split())
for x in range(a,b+1):
    if x < a+k or x > b-k:
        print(x)

Fixed submission:
a, b, k = map(int, input().split())
if b-a+1 < 2*k:
    for i in range(a,b+1):
        print(i)
    exit(0)
for i in range(a,a+k):
    print(i)
for i in range(b-k+1,b+1):
    print(i)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
A.append(0)
for i in range(N):
    ans = 0
    tmp = 0
    for index, a in enumerate(A):
        if i != index:
            ans += abs(tmp-a)
            tmp = a
    print(ans)

Fixed submission:
N = int(input())
A = [0]
A = A + list(map(int, input().split()))
A.append(0)
sumOfRoute = 0
tmp = 0
for a in A:
    sumOfRoute += abs(tmp-a)
    tmp = a

prev = 0
for i in range(1, N+1):
    nexti = A[i+1]
    print(sumOfRoute + abs(prev - nexti) - abs(A[i] - nexti) - abs(prev - A[i]))
    prev = A[i]
Buggy python submission (verdict: Runtime Error):
n = input()
print(len(set(input().split()))

Fixed submission:
n = input()
if len(set(input().split())) == 3:
	print("Three")
else:
	print("Four")
Buggy python submission (verdict: Runtime Error):
def check(arr, x, y):
    d = ((0, -1), (0, 1), (1, -1), (1, 0), (1, 1), (-1, -1), (-1, 0), (-1, 1))

    if arr[x][y] == '#':
        return -1
    else:
        cnt = 0
        for dx, dy in d:
            try:
                if (x+dx) >=0 and (y+dy) >= 0:
                    if arr[x + dx][y + dy] == '#':
                        cnt += 1
            except:
                pass
    return cnt    

h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(input())
ans = []
for x in range(h):
    for y in range(w):
        val = check(s, x, y)
        if val == -1:
            ans.append('#')
        else:
            ans.append(val)

for i in range(h*w):
    print(ans[i], end="")
    if (i+1)%w == 0:
        print()

Fixed submission:
n = int(input())
k = int(input())

ans = 1
for i in range(n):
    if ans*2 > ans + k:
        ans += k
    else:
        ans *= 2
print(ans)
Buggy python submission (verdict: Runtime Error):
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)

def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

def lcm_core(a,b):
    g = gcd_core(a,b)
    return (a*b)//g

def lcm(arr):
    l = lcm_core(arr[0],arr[1])
    for i in range(2,len(arr)):
        l = lcm_core(l,arr[i])
    return l

N = int(input())
T = [int(input()) for _ in range(N)]

print(lcm(T))

Fixed submission:
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)

def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

def lcm_core(a,b):
    g = gcd_core(a,b)
    return (a*b)//g

def lcm(arr):
    l = lcm_core(arr[0],arr[1])
    for i in range(2,len(arr)):
        l = lcm_core(l,arr[i])
    return l

N = int(input())
T = [int(input()) for _ in range(N)]
if N==1:
    print(T[0])
else:
    print(lcm(T))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

cnt = 0
t = 1
for i in a:
    t = a[t-1]
    cnt += 1
    if t == 2:
        print(cnt)
        exit(1)
print("-1")

Fixed submission:
import sys

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

cnt = 0
t = 1
for i in a:
    t = a[t-1]
    cnt += 1
    if t == 2:
        print(cnt)
        sys.exit()
print("-1")

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

cnt = 0
t = 1
for i in a:
    t = a[t-1]
    cnt += 1
    if t == 2:
        print(cnt)
        exit(1)
print(-1)

Fixed submission:
import sys

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

cnt = 0
t = 1
for i in a:
    t = a[t-1]
    cnt += 1
    if t == 2:
        print(cnt)
        sys.exit()
print("-1")
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

colors = [0]*9
for a in A:
    colors[a//400] += 1
ansmin = 0
for c in colors[:-1]:
    if c:
        ansmin += 1
print("{} {}".format(ansmin, ansmin+colors[-1]))

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

colors = [0]*9
for a in A:
    colors[min(8, a//400)] += 1
ansmin = 0
for c in colors[:-1]:
    if c:
        ansmin += 1
x = 0
if ansmin < colors[-1]:
    x = 1
print("{} {}".format(max(ansmin, x), ansmin+colors[-1]))

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

hist = [0]*9
for i in A:
    hist[i//400] += 1

ansmin = 0
for i in hist[:-1]:
    if i > 0:
        ansmin += 1
print("{} {}".format(ansmin, ansmin+hist[-1]))

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

colors = [0]*9
for a in A:
    colors[min(8, a//400)] += 1
ansmin = 0
for c in colors[:-1]:
    if c:
        ansmin += 1
x = 0
if ansmin < colors[-1]:
    x = 1
print("{} {}".format(max(ansmin, x), ansmin+colors[-1]))
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
H = list(map(int, input().split()))

ans = 0
for i in range(N):
    if H[i] >= K:
        ans += 1
print(ans)

Fixed submission:
N = int(input())
if N%2:
    print(((N+1)//2)/N)
else:
    print(0.5)
Buggy python submission (verdict: Runtime Error):
#include <iostream>
using namespace std;

int main(){
    int M, D;
    cin >> M >> D;
    int ans = 0;
    for(int m = 1;m<=M;m++){
        for(int d=1;d<=D;d++){
            if(d % 10 >= 2 && d/10 >= 2 && (d%10)*(d/10) == m){
                ans += 1;
            }
        }
    }
    cout << ans << endl;
}

Fixed submission:
from math import sqrt
M, D = map(int, input().split())
ans = 0
for i in range(1, M+1):
    for j in range(1, D+1):
        if j//10 >= 2 and j%10 >= 2:
            if (j//10)*(j%10) == i:
                ans += 1

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())

es = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    es[a-1].append(b-1)
    es[b-1].append(a-1)

xs = [0]*N
for _ in range(Q):
    p, x = map(int, input().split())
    xs[p-1] += x

ans = [0]
ans[0] = xs[0]
visited = [0]*N
stack = [0]
while stack:
    v = stack.pop()
    visited[v] = 1
    for to in es[v]:
        if visited[to]:
            continue
        stack.append(to)
        ans[to] = ans[v] + xs[to]

print(*ans)

Fixed submission:
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())

es = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    es[a-1].append(b-1)
    es[b-1].append(a-1)

xs = [0]*N
for _ in range(Q):
    p, x = map(int, input().split())
    xs[p-1] += x

ans = [0]*N
ans[0] = xs[0]
visited = [0]*N
stack = [0]
while stack:
    v = stack.pop()
    visited[v] = 1
    for to in es[v]:
        if visited[to]:
            continue
        stack.append(to)
        ans[to] = ans[v] + xs[to]

print(*ans)

Buggy python submission (verdict: Runtime Error):
 
def dfs(a, b):  

    visited[a] = 1 
    b += 1
    start[a] = b  
    dfs_order.append(a)  
      
    for it in adj[a]:  
        if not visited[it]:  
            b = dfs(it, b)  
       
    endd[a] = b 
    return b 
  
n, q  = map(int, input().split())
start = [None] * (n+1) 
endd = [None] * (n+1)
dfs_order = []  
adj = [[] for i in range(n+1)]  
visited = [False] * (n+1)
for i in range(n-1):
    a, b = map(int, input().split())
    adj[a-1].append(b-1)

dfs(0, 0)  

subtree = [[] for _ in range(n+1)]
for i in range(n):  
    subtree[i].append(i)  
    if start[i] != endd[i]:   
        for j in range(start[i]+1, endd[i]+1):  
            subtree[i].append(dfs_order[j-1])   

ans = [0]*(n)

for i in range(q):
    p, x = map(int, input().split())
    for j in subtree[p-1]:
        ans[j] += x

print(*ans)



Fixed submission:
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())

es = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    es[a-1].append(b-1)
    es[b-1].append(a-1)

xs = [0]*N
for _ in range(Q):
    p, x = map(int, input().split())
    xs[p-1] += x

ans = [0]*N
ans[0] = xs[0]
visited = [0]*N
stack = [0]
while stack:
    v = stack.pop()
    visited[v] = 1
    for to in es[v]:
        if visited[to]:
            continue
        stack.append(to)
        ans[to] = ans[v] + xs[to]

print(*ans)
Buggy python submission (verdict: Runtime Error):
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;
typedef long long ll;

int main(){
    int N;
    map<string, ll> m;
    cin >> N;
    string S;
    for(int i=0;i<N;i++){
        cin >> S;
        vector<char> v;
        //範囲ベースforループ
        for(auto x:S)v.push_back(x);
        sort(v.begin(), v.end());
        string T = "";
        for(auto x:v)T += x;
        m[T]++;
    }
    ll ans = 0;
    for(auto x:m) ans += (x.second-1)*x.second/2;
    cout << ans << endl;
}

Fixed submission:
from collections import defaultdict
N = int(input())
D = defaultdict(int)
for i in range(N):
    S = ''.join(sorted(input()))
    D[S] += 1

ans = 0
for key in D:
    M = D[key]
    if M >= 2:
        ans += M*(M-1)//2
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
from collections import defaultdict
N = int(input())
D = defaultdict(int)
for i in range(N):
    S = tuple(sorted(Counter(input()).most_common()))
    D[S] += 1

ans = 0
for key in D:
    M = D[key]
    if M >= 2:
        ans += M*(M-1)//2
    
print(ans)

Fixed submission:
from collections import defaultdict
N = int(input())
D = defaultdict(int)
for i in range(N):
    S = ''.join(sorted(input()))
    D[S] += 1

ans = 0
for key in D:
    M = D[key]
    if M >= 2:
        ans += M*(M-1)//2
    
print(ans)
Buggy python submission (verdict: Runtime Error):
#include <iostream>
#include <algorithm>

using namespace std;

int N;
int A[200010], B[200010];
pair<int, int> C[200010];

int main(){
    cin >> N;
    for(int i=1;i<=N;i++){
        cin >> A[i] >> B[i];
        //<b, i>のpairを作りAにアクセスできるようにする
        C[i] = {B[i], i};
    }
    sort(C+1, C+N+1);
    int time = 0;
    for(int i=1;i<=N;i++){
        int id = C[i].second;
        if(time + A[id] <= B[id]) time += A[id];
        else{
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

Fixed submission:
N = int(input())
T = sorted([tuple(map(int, input().split())) for _ in range(N)], key=lambda x:x[1])
now = 0
for a, b in T:
    if now + a <= b:
        now += a
    else:
        print("No")
        exit()
print("Yes")
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
from copy import copy
input = sys.stdin.readline
mod = 10**9+7
N, M = map(int, input().split())
S = "".join(input().split())
T = "".join(input().split())
dS = defaultdict(int)
dT = defaultdict(int)
dS[""] += 1
dT[""] += 1
for i in range(N):
    tmp = copy(dS)
    for key in tmp:
        dS[key+S[i]] += tmp[key]

for i in range(M):
    tmp = copy(dT)
    for key in tmp:
        dT[key+T[i]] += tmp[key]
    
ans = 0
for key in dS:
    ans += dS[key]*dT[key]%mod

print(ans%mod)


Fixed submission:
mod = 10**9 + 7
N, M = map(int, input().split())
S = [None] + list(map(int, input().split()))
T = [None] + list(map(int, input().split()))
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1, N+1):
    for j in range(1, M+1):
        if S[i] == T[j]:
            dp[i][j] = (dp[i][j-1] + dp[i-1][j] + 1)%mod
        else:
            dp[i][j] = (dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1])%mod
print((dp[N][M]+1)%mod)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
mod = 10**9+7
N, M = map(int, input().split())
S = "".join(input().split())
T = "".join(input().split())
dS = defaultdict(int)
dT = defaultdict(int)
dS[""] += 1
dT[""] += 1
for i in range(N):
    newkey = []
    for key in dS:
        newkey.append((key+S[i], dS[key]))
    
    for key, c in newkey:
        dS[key] += c

for i in range(M):
    newkey = []
    for key in dT:
        newkey.append((key+T[i], dT[key]))

    for key, c in newkey:
        dT[key] += c

    

ans = 0
for key in dS:
    ans += dS[key]*dT[key]%mod

print(ans%mod)


Fixed submission:
mod = 10**9 + 7
N, M = map(int, input().split())
S = [None] + list(map(int, input().split()))
T = [None] + list(map(int, input().split()))
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1, N+1):
    for j in range(1, M+1):
        if S[i] == T[j]:
            dp[i][j] = (dp[i][j-1] + dp[i-1][j] + 1)%mod
        else:
            dp[i][j] = (dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1])%mod
print((dp[N][M]+1)%mod)
Buggy python submission (verdict: Runtime Error):
from collections import deque
from collections import defaultdict
N = int(input())
XY = list(sorted([tuple(map(int, input().split())) for _ in range(N)]))
PQ = []
for i in range(N-1):
    for j in range(i, N):
        PQ.append((XY[j][0]-XY[i][0], XY[j][1]-XY[i][1]))
PQ = list(set(PQ))
ans = N
for p, q in PQ:
    tmp1 = 1
    check = defaultdict(int)
    T = deque(XY.copy())
    for x, y in T:
        check[(x, y)] += 1
    x, y = T.popleft()
    cnt = 1
    while cnt < N:
        tmp2 = 1
        for s, t in T:
            if check[(s, t)] == 0:
                continue
            else:
                if s - p == x and t - q == y:
                    check[(s, t)] -= 1
                    tmp2 = 0
                    break
        if tmp2:
            x, y = T.popleft()
        else:
            x, y = s, t
        tmp1 += tmp2
        cnt += 1      
    ans = min(ans, tmp1)
print(ans)


        


    


Fixed submission:
N = int(input())
XY = sorted([tuple(map(int, input().split())) for _ in range(N)])
PQ = []
for i in range(N-1):
    for j in range(i, N):
        PQ.append((XY[j][0]-XY[i][0], XY[j][1]-XY[i][1]))
PQ = list(set(PQ))
ans = N
for p, q in PQ:
    tmp1 = 1
    check=[0]*N
    for i in range(N-1):
        if check[i]:
            continue
        else:
            check[i] = 1
            tmp2 = 1
        for j in range(i, N):
            if check[j]:
                continue
            else:
                if XY[j][0] - p == XY[i][0] and XY[j][1] - q == XY[i][1]:
                    check[j] == 1
                    tmp2 = 0
                    break
        tmp1 += tmp2        
    ans = min(ans, tmp1)
print(ans)
Buggy python submission (verdict: Runtime Error):
from collections import deque
H, W = map(int ,input().split())
A = [input() for _ in range(H)]
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
inf = float("inf")

dp = [[inf]*W for _ in range(H)]
q = deque()
for y, row in enumerate(A):
    for x, c in enumerate(row):
        if c == "#":
            dp[y][x] = 0
            q.append((x, y))

while q:
    x, y = q.popleft()
    d = dp[x][y]
    for dx, dy in dxy:
        nx, ny = x+dx, y+dy
        if (not 0 <= nx < W) or (not 0 <= ny < H):
            continue
        if dp[ny][nx] <= d+1:
            continue
        dp[ny][nx] = d+1
        q.append((nx, ny))

print(max(max(row) for row in dp))



Fixed submission:
from collections import deque
H, W = map(int ,input().split())
A = [input() for _ in range(H)]
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
inf = float("inf")

dp = [[inf]*W for _ in range(H)]
q = deque()
for y, row in enumerate(A):
    for x, c in enumerate(row):
        if c == "#":
            dp[y][x] = 0
            q.append((x, y))

while q:
    x, y = q.popleft()
    d = dp[y][x]
    for dx, dy in dxy:
        nx, ny = x+dx, y+dy
        if (not 0 <= nx < W) or (not 0 <= ny < H):
            continue
        if dp[ny][nx] <= d+1:
            continue
        dp[ny][nx] = d+1
        q.append((nx, ny))

print(max(max(row) for row in dp))

Buggy python submission (verdict: Runtime Error):
from collections import deque
H, W = map(int ,input().split())
A = [input() for _ in range(H)]
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
inf = float("inf")

dp = [[inf]*W for _ in range(H)]
q = deque()
for y, row in enumerate(A):
    for x, c in enumerate(row):
        if c == "#":
            dp[y][x] = 0
            q.append((x, y))

while q:
    x, y = q.popleft()
    d = dp[x][y]
    for dx, dy in dxy:
        nx, ny = x+dx, y+dy
        if (not 0 <= nx < W) or (not 0 <= ny < H):
            continue
        if dp[ny][nx] <= d+1:
            continue
        dp[ny][nx] = d+1
        q.append((nx, ny))

print(max(max(row) for row in dp))



Fixed submission:
from collections import deque
H, W = map(int ,input().split())
A = [input() for _ in range(H)]
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
inf = float("inf")

dp = [[inf]*W for _ in range(H)]
q = deque()
for y, row in enumerate(A):
    for x, c in enumerate(row):
        if c == "#":
            dp[y][x] = 0
            q.append((x, y))

while q:
    x, y = q.popleft()
    d = dp[y][x]
    for dx, dy in dxy:
        nx, ny = x+dx, y+dy
        if (not 0 <= nx < W) or (not 0 <= ny < H):
            continue
        if dp[ny][nx] <= d+1:
            continue
        dp[ny][nx] = d+1
        q.append((nx, ny))

print(max(max(row) for row in dp))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
mod = 10**9+7
n = int(input())
c = [0]
d = defaultdict(list)
r = defaultdict(list)

idx = 1
for i in range(n):
    tmp = int(input())
    if c[-1] != tmp:
        r[idx] += d[tmp]
        d[tmp].append(idx)
        c.append(tmp)
        idx += 1

n = len(c)
dp = [0]*(n)
dp[0] = 1
for i in range(1, n):
    dp[i] = dp[i-1]
    for j in r[i]:
        dp[i] += (dp[j-1]%mod)
    dp[i] %= mod

print(dp[-1])


Fixed submission:
from collections import defaultdict
mod = 10**9+7
n = int(input())
c = [0]
d = defaultdict(list)
r = defaultdict(int)

idx = 1
for i in range(n):
    tmp = int(input())
    if c[-1] != tmp:
        if d[tmp]:
            r[idx] = d[tmp][-1]
        d[tmp].append(idx)
        c.append(tmp)
        idx += 1

n = len(c)
dp = [0]*(n)
dp[0] = 1
for i in range(1, n):
    dp[i] = dp[i-1]
    if r[i]:
        dp[i] += dp[r[i]]
    dp[i] %= mod
print(dp[-1])
Buggy python submission (verdict: Runtime Error):
from math import log2
a, b = map(int, input().split())
p, q = a, b
a -= 1
if a > 0:
    n = int(log2(a))
else:
    n = -1
m = int(log2(b))
a_cnt = [0]*(m+1)
b_cnt = [0]*(m+1)
for i in range(m, 0, -1):
    if i <= n:
        if a >= 2**i:
            a_cnt[i] = a-2**i+1
            a -= 2**i
    if b >= 2**i:
        b_cnt[i] = b-2**i+1
        b -= 2**i

ans = [0]*(m+1)
for i in range(m+1):
    ans[i] = (b_cnt[i]-a_cnt[i])%2

tmp = 0
for i in range(1, m+1):
    if ans[i]:
        tmp += 2**i

tmp += ((q+1)//2-p//2)%2
print(tmp)


Fixed submission:
from math import log2
a, b = map(int, input().split())
p, q = a, b
a -= 1
if a > 0:
    n = int(log2(a))
else:
    n = -1
if b > 0:
    m = int(log2(b))
else:
    print(0)
    exit()
a_cnt = [0]*(m+1)
b_cnt = [0]*(m+1)
for i in range(m, 0, -1):
    if i <= n:
        if a >= 2**i:
            a_cnt[i] = a-2**i+1
            a -= 2**i
    if b >= 2**i:
        b_cnt[i] = b-2**i+1
        b -= 2**i

ans = [0]*(m+1)
for i in range(m+1):
    ans[i] = (b_cnt[i]-a_cnt[i])%2

tmp = 0
for i in range(1, m+1):
    if ans[i]:
        tmp += 2**i

tmp += ((q+1)//2-p//2)%2
print(tmp)