Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
def main():  
  N = int(input())
  VW = [list(map(int,input().split())) for _ in range(N)]
  Q = int(input())
  vL = [list(map(int,input().split())) for _ in range(Q)]
  M = 0
  import math
  from collections import deque
  for q in range(Q):
    x = vL[q][1]
    if x>M:
      M = x

  D = int(math.log2(N)) + 1
  K = D//2
  Ddp = deque([[0] * (M+1) for _ in range(2**K)])

  for q in range(Q):
    v,L = vL[q]
    if v<=2**K - 1:
      print(Ddp[v][L])
      continue
    
main()


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

for i in range(1,K+1):
  B = [0]*N
  for j in range(N):
    l = max(0, j-A[j])
    r = min(N-1, j+A[j])
    B[l] += 1
    if r+1<N:
      B[r+1] -= 1
    
  if B[0]==N:
    x = 1
  else:
    x = 0
  for i in range(1,N):
    B[i] += B[i-1]
    if B[i]==N:
      x += 1
  A = B.copy()
  if (x==N)or(i==K):
    break
    
print(' '.join(list(map(str,A))))
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))

if 0 in A:
  print(0)
  
else:
  ans = 1
  for a in A:
    ans *= 1
    if ans>10**18:
      ans = -1
      break
  print(ans)

Fixed submission:
A,B = map(int,input().split())
print(A*B)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))

ans = 1
for a in A:
  ans *= a

if ans>10**18:
  print(-1)
else:
  print(ans)

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

if 0 in A:
  print(0)
  
else:
  ans = 1
  for a in A:
    ans *= a
    if ans>10**18:
      ans = -1
      break
  print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = [i-1 for i in A]

doubling = [[0] * N for _ in range(61)]
for i in range(N):
  doubling[0][i] = A[i]
  
for i in range(1,61):
  for j in range(N):
    doubling[i][j] = doubling[i-1][doubling[i-1][j]]
    
now = 0
i = 0
while K!=0:
  if K&1==1:
    now = doubling[i][now]
    K = K>>1
    i += 1
    
  else:
    K = K>>1
    i += 1
    
print(now+1)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = [i-1 for i in A]

flag = [False]*N
from collections import deque

q = deque([])
q.append(0)
flag[0] = True
now = 0
while True:
  now = A[now]
  if flag[now]:
    break
  else:
    flag[now] = True
    q.append(now)
    
l = list(q)
if K<len(l):
  print(l[K]+1)

else:
  for i,x in enumerate(l):
    if x==now:
      l = l[i:]
      K -= i
      break
  
  print(l[K%len(l)]+1)

Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = [i-1 for i in A]

flag = [False]*N
from collections import deque

q = deque([])
q.append(0)
flag[0] = True
now = 0
while True:
  now = A[now]
  if flag[now]:
    break
  else:
    flag[now] = True
    q.append(now)
    
l = list(q)
if K<=len(l):
  print(l[K]+1)

else:
  for i,x in enumerate(l):
    if x==now:
      l = l[i:]
      K -= i
      break
  
  print(l[K%len(l)]+1)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A = [i-1 for i in A]

flag = [False]*N
from collections import deque

q = deque([])
q.append(0)
flag[0] = True
now = 0
while True:
  now = A[now]
  if flag[now]:
    break
  else:
    flag[now] = True
    q.append(now)
    
l = list(q)
if K<len(l):
  print(l[K]+1)

else:
  for i,x in enumerate(l):
    if x==now:
      l = l[i:]
      K -= i
      break
  
  print(l[K%len(l)]+1)
Buggy python submission (verdict: Runtime Error):
N = int(input())
from collections import deque
L = deque([])
R = deque([])

for i in range(N):
  S = input()
  l = [0]*len(S)
  if S[0]=='(':
    l[0]=1
  else:
    l[0]=-1
  for j in range(1,len(S)):
    if S[j]=='(':
      l[j] = l[j-1] + 1
    else:
      l[j] = l[j-1] - 1
      
  if l[-1] < 0:
    R.append([min(l)-l[-1],-l[-1]])
  else:
    L.append([min(l),l[-1]])
    
R = sorted(R, key = lambda x:x[0], reverse = True)
L = sorted(L, key = lambda x:x[0])

now = 0
judge = True
for r in R:
  if now+r[0]<0:
    judge = False
    break
  else:
    now += r[1]
    
for l in L:
  if now + l[0]<0:
    judge = False
    break
  else:
    now += r[1]
    
if now!=0:
  judge = False
  
if judge:
  print('Yes')
else:
  print('No')

Fixed submission:
N = int(input())
from collections import deque
L = deque([])
R = deque([])

for i in range(N):
  S = input()
  l = [0]*len(S)
  if S[0]=='(':
    l[0]=1
  else:
    l[0]=-1
  for j in range(1,len(S)):
    if S[j]=='(':
      l[j] = l[j-1] + 1
    else:
      l[j] = l[j-1] - 1
  if l[-1] < 0:
    R.append([min(l)-l[-1], -l[-1], min(l), l[-1]])
  else:
    L.append([min(0,min(l)),l[-1]])
    
R = sorted(R, key = lambda x:x[0])
L = sorted(L, key = lambda x:x[0], reverse = True)
now = 0
judge = True
    
for l in L:
  if now + l[0]<0:
    judge = False
    break
  else:
    now += l[1]
    
for r in R:
  if now+r[2]<0:
    judge = False
    break
  else:
    now += r[3]
    
if now!=0:
  judge = False
  
if judge:
  print('Yes')
else:
  print('No')

Buggy python submission (verdict: Runtime Error):
N = int(input())
from collections import deque
L = deque([])
R = deque([])

for i in range(N):
  S = input()
  l = [0]*len(S)
  if S[0]=='(':
    l[0]=1
  else:
    l[0]=-1
  for j in range(1,len(S)):
    if S[j]=='(':
      l[j] = l[j-1] + 1
    else:
      l[j] = l[j-1] - 1
      
  if l[-1] < 0:
    R.append([min(l)-l[-1],-l[-1]])
  else:
    L.append([min(l),l[-1]])
    
R = sorted(R, key = lambda x:x[0], reverse = True)
L = sorted(L, key = lambda x:x[0])

now = 0
judge = True
for r in R:
  if now+r[0]<0:
    judge = False
    break
  else:
    now += r[1]
    
for l in L:
  if now + l[0]<0:
    judge = False
    break
  else:
    now += r[1]
    
if now!=0:
  judge = False
  
if judge:
  print('Yes')
else:
  print('No')

Fixed submission:
N = int(input())
from collections import deque
L = deque([])
R = deque([])

for i in range(N):
  S = input()
  l = [0]*len(S)
  if S[0]=='(':
    l[0]=1
  else:
    l[0]=-1
  for j in range(1,len(S)):
    if S[j]=='(':
      l[j] = l[j-1] + 1
    else:
      l[j] = l[j-1] - 1
  if l[-1] < 0:
    R.append([min(l)-l[-1], -l[-1], min(l), l[-1]])
  else:
    L.append([min(0,min(l)),l[-1]])
    
R = sorted(R, key = lambda x:x[0])
L = sorted(L, key = lambda x:x[0], reverse = True)
now = 0
judge = True
    
for l in L:
  if now + l[0]<0:
    judge = False
    break
  else:
    now += l[1]
    
for r in R:
  if now+r[2]<0:
    judge = False
    break
  else:
    now += r[3]
    
if now!=0:
  judge = False
  
if judge:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
X = int(input())
y = 1
z = X
for i in range(2,int((X**(1/2)))+1):
  if X%i==0:
    y = i
    z = x//i
    break
    
for a in range(10**5):
  b = a-y
  if a**4 + (a**3)*b + (a**2)*(b**2) + a*(b**3) + b**4==z:
    print(a,b)
    exit()

Fixed submission:
X = int(input())
for A in range(-1000,1001):
  for B in range(-1000,1001):
    if A**5-B**5==X:
      print(A,B)
      exit()
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
ans = 0
l = [0]*(2*10**5+10)
for i in range(1,len(A)):
  x = i-A[i]
  if x<0:
    continue
  else:
    l[x]+=1

ans+=l[A[0]]
for i in range(1,N):
  x = i+A[i]
  if x<0:
    continue
  if x>2*10**5:
    continue
  
  l[i-A[i]]-=1
  ans+=l[x]
  
print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
ans = 0
dic = {}
for i in range(1,N):
  x = i-A[i]
  if x<0:
    continue
  else:
    if x in dic:
      dic[x]+=1
    else:
      dic[x]=1
if A[0] in dic:
  ans+=dic[A[0]]
for i in range(1,N):
  x = i+A[i]
  
  if i-A[i] in dic:
    dic[i-A[i]]-=1
    
  if x in dic:
    ans+=dic[x]
  
print(ans)
Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
flag = [False]*(10**6)
plu_flag = [False]*(10**6)
plu = 1
for i in range(1,N+1):
  if M==0:
    break
  if (flag[i])or(flag[i+plu]):
    continue
  if (plu_flag[plu])or(plu_flag[N-plu]):
    plu+=1
    continue
    
  if i+plu>N:
    print(d)
  plu_flag[plu]=True
  plu_flag[N-plu]=True
  flag[i]=True
  flag[i+plu]=True
  print(i,i+plu)
  plu+=1
  M-=1

Fixed submission:
N,M = map(int,input().split())
start = 1
end = M
while True:
  if start>=end:
    break
  print(start,end)
  start+=1
  end-=1
  
start = M+1
end = 2*M+1
while True:
  if start>=end:
    break
  print(start,end)
  start = start+1
  end = end-1

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
flag = [False]*(10**6)
plu_flag = [False]*(10**6)
plu = 1
for i in range(1,N+1):
  if M==0:
    break
  if (flag[i])or(flag[i+plu]):
    continue
  if (plu_flag[plu])or(plu_flag[N-plu]):
    plu+=1
    continue
    
  if plu>N//2:
    print(d)
  if i+plu>N:
    print(d)
  plu_flag[plu]=True
  plu_flag[N-plu]=True
  flag[i]=True
  flag[i+plu]=True
  print(i,i+plu)
  plu+=1
  M-=1

Fixed submission:
N,M = map(int,input().split())
start = 1
end = M
while True:
  if start>=end:
    break
  print(start,end)
  start+=1
  end-=1
  
start = M+1
end = 2*M+1
while True:
  if start>=end:
    break
  print(start,end)
  start = start+1
  end = end-1

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
flag = [False]*(10**5+10)
plu = 1
for i in range(1,N+1):
  if M==0:
    break
  if flag[i] or flag[i+plu]:
    continue
  else:
    flag[i]=True
    flag[i+plu]=True
    print(i,i+plu)
    plu+=1
    M-=1

Fixed submission:
N,M = map(int,input().split())
start = 1
end = M
while True:
  if start>=end:
    break
  print(start,end)
  start+=1
  end-=1
  
start = M+1
end = 2*M+1
while True:
  if start>=end:
    break
  print(start,end)
  start = start+1
  end = end-1
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N = int(input())
A = list(map(int,input().split()))
A = [[a,i] for a,i in zip(A,range(1,N+1))]
A = sorted(A,reverse=True)
INF = float('inf')
dp = [[-INF] * (N+1) for _ in range(N+1)]
dp[0][0] = 0
for x_y in range(1,N+1):
  a = A[x_y-1]
  for x in range(x_y+1):
    y = x_y-x
    if x==0:
      dp[x][y] = dp[x][y-1] + a[0]*(N-y+1-a[1])
    elif y==0:
      dp[x][y] = dp[x-1][y] + a[0]*(a[1]-x)
    else:
      dp[x][y] = max(dp[x-1][y]+a[0]*(a[1]-x),dp[x][y-1] + a[0]*(N-y+1-a[1]))

print(int(np.max(dp)))

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
A = [[a,i] for a,i in zip(A,range(1,N+1))]
A = sorted(A,reverse=True)
INF = float('inf')
dp = [[-INF] * (N+1) for _ in range(N+1)]
dp[0][0] = 0
ans = 0
for x_y in range(1,N+1):
  a = A[x_y-1]
  for x in range(x_y+1):
    y = x_y-x
    if x==0:
      dp[x][y] = dp[x][y-1] + a[0]*(N-y+1-a[1])
    elif y==0:
      dp[x][y] = dp[x-1][y] + a[0]*(a[1]-x)
    else:
      dp[x][y] = max(dp[x-1][y]+a[0]*(a[1]-x),dp[x][y-1] + a[0]*(N-y+1-a[1]))
    
    ans = max(ans,dp[x][y])
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
dp = [[0]*3 for _ in range(N+1)]

dp[1][3] = A[0]
dp[2][2] = max(A[1],dp[1][3])

for i in range(2,N):
  a = A[i]
  mid = (i+1)//2
  ma = (i+2)//2
  mi = i//2
  dp[i+1][2+ma-mid] = max(dp[i][2+ma-i//2],dp[i-1][1+ma-(i-1)//2]+a)
  dp[i+1][2+mi-mid] = max(dp[i][2+mi-i//2],dp[i-1][1+mi-(i-1)//2]+a)

print(dp[-1][2])

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
dp = [{} for _ in range(N+1)]
INF = -float('inf')
for i in range(1,N+1):
    mid = i//2
    dp[i][mid-2],dp[i][mid-1],dp[i][mid],dp[i][mid+1],dp[i][mid+2]=INF,INF,INF,INF,INF
dp[1][1] = A[0]
dp[2][1] = max(A[1],dp[1][1])
dp[1][0],dp[2][0]=0,0

for i in range(2,N):
  a = A[i]
  ma = (i+2)//2
  mi = i//2
  dp[i+1][ma] = max(dp[i][ma],dp[i-1][ma-1]+a)
  dp[i+1][mi] = max(dp[i][mi],dp[i-1][mi-1]+a)

print(dp[-1][N//2])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
dp = [[0]*(N+1) for _ in range(N+1)]
dp[1][1] = A[0]
dp[2][1] = max(A[1],dp[1][1])

for i in range(2,N):
  a = A[i]
  ma = (i+2)//2
  mi = i//2
  dp[i+1][ma] = max(dp[i][ma],dp[i-1][ma-1]+a)
  dp[i+1][mi] = max(dp[i][mi],dp[i-1][mi-1]+a)

print(dp[-1][N//2])

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
dp = [{} for _ in range(N+1)]
INF = -float('inf')
for i in range(1,N+1):
    mid = i//2
    dp[i][mid-2],dp[i][mid-1],dp[i][mid],dp[i][mid+1],dp[i][mid+2]=INF,INF,INF,INF,INF
dp[1][1] = A[0]
dp[2][1] = max(A[1],dp[1][1])
dp[1][0],dp[2][0]=0,0

for i in range(2,N):
  a = A[i]
  ma = (i+2)//2
  mi = i//2
  dp[i+1][ma] = max(dp[i][ma],dp[i-1][ma-1]+a)
  dp[i+1][mi] = max(dp[i][mi],dp[i-1][mi-1]+a)

print(dp[-1][N//2])
Buggy python submission (verdict: Time Limit Exceeded):
N,X,Y = map(int,input().split())
ans = [0]*N
for i in range(1,N+1):
  for j in range(i,N+1):
    y = min(abs(i-X)+abs(j-Y)+1,abs(i-Y)+abs(j-X)+1)
    x = min(j-i,y)
    ans[x]+=1

for k in range(1,N):
  print(ans[k])

Fixed submission:
N,X,Y = map(int,input().split())
ans = [0]*N
for i in range(1,N+1):
  for j in range(i,N+1):
    y = min(abs(i-X)+abs(j-Y)+1,abs(i-Y)+abs(j-X)+1)
    x = min(j-i,y)
    ans[x]+=1

for k in range(1,N):
  print(ans[k])
Buggy python submission (verdict: Runtime Error):
def main():
  H,W,K = map(int,input().split())
  import numpy as np
  from collections import deque
  ans = 10000
  S = np.array([list(map(int,list(input()))) for _ in range(H)])
  cum = [[0]*W for _ in range(H+1)]
  for j in range(W):
    for i in range(1,H+1):
      cum[i][j] = cum[i-1][j]+S[i-1][j]

  cum = np.array(cum)
  for i in range(2**(H-1)):
    q = deque([0])
    for h in range(H):
      if (i>>h)&1:
        q.append(h+1)
    q.append(H)
    p = deque()
    judge = False
    for a in range(len(q)-1):
      x = cum[q[a+1]]-cum[q[a]]
      if (x>K).any():
        judge = True
        break
      else:
        p.append(x)

    if judge:
      continue

    t_a = len(q)-2
    l = [0]*len(p)
    for w in range(W):
      for x,a in enumerate(p):
        l[x]+=a[w]
        if l[x]>K:
          t_a+=1
          l = [0]*len(p)
          for z,b in enumerate(p):
            l[z] = b[w]
          break
    ans = min(ans,t_a)

  print(ans)
  
main()

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
A_set = list(set(A))
l = [0]*(N+2)
ans = [0]*(N+2)
for a in A:
  l[a]+=1
comb = [0]*(N+2)
for i in range(2,N+1):
  comb[i] = i*(i-1)//2
  
for i in range(2,N+1):
  ans[i]=comb[i]-comb[i-1]
  
total = 0
for a in A_set:
  total += comb[l[a]]

for a in A:
  print(int(total-ans[l[a]]))
Buggy python submission (verdict: Runtime Error):
def main():
  H,N = map(int,input().split())
  AB = [list(map(int,input().split())) for _ in range(N)]
  ab = min(AB,key = lambda x:x[1])
  INF = float('inf')
  dp = [INF]*(H+1)
  mi_a = ab[0]
  mi_b = ab[1]
  dp[0] = 0
  for i in range(1,H+1):
    if i<=mi_a:
      dp[i]=mi_b
    else:
      mi = float('inf')
      for a,b in AB:
        mi = min(mi,dp[max(0,i-a)]+b)
      if dp[i]>mi:
        dp[i] = mi

  print(dp[H])
  
main()

Fixed submission:
H,A = map(int,input().split())
if H%A==0:
  print(H//A)
else:
  print((H//A)+1)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
from fractions import gcd
mod = 10**9+7
g = A[0]
for a in A[1::]:
  g = g*a//gcd(g,a)

ans = 0
b = g%mod
for a in A:
  ans += b*pow(a,mod-2,mod)
  ans %= mod
  
print(ans%mod)

Fixed submission:
N = int(input())
ans = 0
l=[[0]*10 for _ in range(10)]
for i in range(1,N+1):
  s = str(i)
  l[int(s[0])][int(s[-1])]+=1
  
for i in range(10):
  for j in range(10):
    ans+=l[i][j]*l[j][i]
    
print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
from fractions import gcd
mod = 10**9+7
g = A[0]
for a in A[1::]:
  g = g*a//gcd(g,a)

ans = 0
b = g%mod
for a in A:
  ans += b*pow(a,mod-2,mod)
  ans %= mod
  
print(ans%mod)

Fixed submission:
N = int(input())
ans = 0
l=[[0]*10 for _ in range(10)]
for i in range(1,N+1):
  s = str(i)
  l[int(s[0])][int(s[-1])]+=1
  
for i in range(10):
  for j in range(10):
    ans+=l[i][j]*l[j][i]
    
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
INF = N**3
max_ab = max(max(A),max(B))+1
dp = [[INF] * max_ab for _ in range(2**N)]
dp[0][0]=0
for i in range(2**N):
  done = 0
  for j in range(N):
    if (i>>j)&1:
      done += 1
  
  for right_end in range(max_ab):
    if dp[i][right_end]==INF:
      continue
    for add_last in range(N):
      if (i>>add_last)&1:
        continue
      if abs(add_last-done)%2==0:
        add_last_surface = A[add_last]
      else:
        add_last_surface = B[add_last]
        
      if right_end>add_last_surface:
        continue
        
      num = 0
      for k in range(add_last):
        if (i>>k)&1==0:
          num+=1
          
      dp[i|1<<add_last][add_last_surface]=min(dp[i|1<<add_last][add_last_surface],
                                              dp[i][right_end]+num)
      
ans = min(dp[-1])
if ans==INF:
  print(-1)
else:
  print(ans)

Fixed submission:
H = int(input())
W = int(input())
N = int(input())
import math
x = max(H,W)
print(math.ceil(N/x))
Buggy python submission (verdict: Runtime Error):
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
from collections import deque
ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j]=='#':
      continue
    inf = 10**5
    A = [[inf]*W for _ in range(H)]
    A[i][j]=0
    judge = [[False]*W for _ in range(H)]
    judge[i][j]=True
    q = deque([[i,j]])
    while q:
      a = q.popleft()
      x=a[0]
      y=a[1]
      b = A[x][y]
      for m in [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]:
        h = m[0]
        w = m[1]
        if (h<0)or(h>H-1)or(w<0)or(w>W-1):
          continue
        if S[h][w]=='#':
          continue
        c = b+1
        if judge[h][w]:
          continue
        A[h][w]=c
        judge[h][w]=True
        q.append([h,w])
        
    A = [a for b in A for a in b if a!=inf]
    ans = max(ans,max(A))
    
print(ans)

Fixed submission:
C = input()
print(chr(ord(C)+1))
Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
from collections import deque
ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j]=='#':
      continue
    inf = 10**5
    A = [[inf]*W for _ in range(H)]
    A[i][j]=0
    q = deque([[i,j]])
    while q:
      a = q.pop()
      x=a[0]
      y=a[1]
      b = A[x][y]
      for m in [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]:
        h = m[0]
        w = m[1]
        if (h<0)or(h>H-1)or(w<0)or(w>W-1):
          continue
        if S[h][w]=='#':
          continue
        c = b+1
        if c>=A[h][w]:
          continue
        A[h][w]=c
        q.append([h,w])
        
    A = [a for b in A for a in b if a!=inf]
    ans = max(ans,max(A))
    
print(ans)

Fixed submission:
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
from collections import deque
import numpy as np
ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j]=='#':
      continue
    inf = 10**5
    A = [[inf]*W for _ in range(H)]
    A[i][j]=0
    q = deque([[i,j]])
    while q:
      a = q.popleft()
      x=a[0]
      y=a[1]
      b = A[x][y]
      for m in [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]:
        h = m[0]
        w = m[1]
        if (h<0)or(h>H-1)or(w<0)or(w>W-1):
          continue
        if S[h][w]=='#':
          continue
        c = b+1
        if c>=A[h][w]:
          continue
        A[h][w]=c
        q.append([h,w])
      
    A = np.array(A)
    ans = max(ans,max(A[A<10**5]))
    
print(ans)

Buggy python submission (verdict: Runtime Error):
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
from collections import deque
ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j]=='#':
      continue
    inf = 10**5
    A = [[inf]*W for _ in range(H)]
    A[i][j]=0
    q = deque([[i,j]])
    while q:
      a = q.popleft()
      x=a[0]
      y=a[1]
      b = A[x][y]
      for m in [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]:
        h = m[0]
        w = m[1]
        if (h<0)or(h>H-1)or(w<0)or(w>W-1):
          continue
        if S[h][w]=='#':
          A[h][w]=0
          continue
        c = b+1
        if c>=A[h][w]:
          continue
        A[h][w]=c
        q.append([h,w])
        
    A = [a for a in b for b in A if a!=inf]
    ans = max(ans,max(max(A)))
    
print(ans)

Fixed submission:
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
from collections import deque
import numpy as np
ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j]=='#':
      continue
    inf = 10**5
    A = [[inf]*W for _ in range(H)]
    A[i][j]=0
    q = deque([[i,j]])
    while q:
      a = q.popleft()
      x=a[0]
      y=a[1]
      b = A[x][y]
      for m in [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]:
        h = m[0]
        w = m[1]
        if (h<0)or(h>H-1)or(w<0)or(w>W-1):
          continue
        if S[h][w]=='#':
          continue
        c = b+1
        if c>=A[h][w]:
          continue
        A[h][w]=c
        q.append([h,w])
      
    A = np.array(A)
    ans = max(ans,max(A[A<10**5]))
    
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
A = list(map(int,input().split()))
mod = 10**9+7
import itertools
ans = 0
for a in itertools.combinations(A,K):
  ans += (max(a)-min(a))%mod
  ans %= mod
  
print(ans)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
fact_l=[1]*(N+10)
mod = 10**9+7
for i in range(2,N+1):
  fact_l[i]=(fact_l[i-1]*i)%mod
  
fact_inv_l = [1]*(N+10)
for i in range(2,N+1):
  fact_inv_l[i]=pow(fact_l[i],mod-2,mod)
ans = 0
for i in range(1,N-K+2):
  b = N-i
  c = K-1
  ans -= A[i-1]*fact_l[b]%mod*fact_inv_l[b-c]%mod*fact_inv_l[c]%mod
  ans += A[-i]*fact_l[b]%mod*fact_inv_l[b-c]%mod*fact_inv_l[c]%mod
    
print(ans%mod)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
A = list(map(int,input().split()))
mod = 10**9+7
import itertools
ans = 0
for a in itertools.combinations(A,K):
  ans += (max(a)-min(a))%mod
  ans %= mod
  
print(ans)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
fact_l=[1]*(N+10)
mod = 10**9+7
for i in range(2,N+1):
  fact_l[i]=(fact_l[i-1]*i)%mod
  
fact_inv_l = [1]*(N+10)
for i in range(2,N+1):
  fact_inv_l[i]=pow(fact_l[i],mod-2,mod)
ans = 0
for i in range(1,N-K+2):
  b = N-i
  c = K-1
  ans -= A[i-1]*fact_l[b]%mod*fact_inv_l[b-c]%mod*fact_inv_l[c]%mod
  ans += A[-i]*fact_l[b]%mod*fact_inv_l[b-c]%mod*fact_inv_l[c]%mod
    
print(ans%mod)