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

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

if N == 1:
  print(1)
  exit()

import math
def prime(x):
  p = {}
  cnt = 2
  Flag = True
  while Flag:
    last = math.ceil(x ** 0.5) 
    if cnt > last:
      break
    for i in range(cnt, last + 1):
      if x % i == 0:
        x //= i
        p[i] = 1
        cnt = i + 2
        while x % i == 0:
          x //= i
          p[i] += 1
        cnt = i + 1
        break
      if i == last:
        Flag = False
  if x != 1:
    p[x] = 1
  return p


class Inv:
  def __init__(s, mod):
    s.MOD = mod
  def modpow(s, a, n):
    res = 1
    while n > 0:
      if n & 1:
        res = res * a % s.MOD
      a = a * a % s.MOD
      n >>= 1
    return res
  def invx(s, a):
    return s.modpow(a, s.MOD - 2)
  def invL(s, a, n):
    ia = s.invx(a)
    L = [1] * (n + 1)
    for i in range(1, n + 1):
      L[i] = L[i - 1] * ia % s.MOD
    return L

p = prime(A[0])

for i in range(1, N):
  q = prime(A[i])
  for n in q:
    if n in p:
      p[n] = max(p[n], q[n])
    else:
      p[n] = q[n]

inv = Inv(MOD)
t = 1
for n in p:
  t = t * inv.modpow(n, p[n]) % MOD

ans = 0
for i in range(N):
  ans = (ans + t * inv.invx(A[i])) % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

def gcd(x, y):
  b = max(x, y)
  s = min(x, y)
  r = b % s
  while r != 0:
    b = s
    s = r
    r = b % s
  return s

def lcm(x, y):
  return (x * y) // gcd(x,y)

class Inv:
  def __init__(s, mod):
    s.MOD = mod
  def modpow(s, a, n):
    res = 1
    while n > 0:
      if n & 1:
        res = res * a % s.MOD
      a = a * a % s.MOD
      n >>= 1
    return res
  def invx(s, a):
    return s.modpow(a, s.MOD - 2)
  def invL(s, a, n):
    ia = s.invx(a)
    L = [1] * (n + 1)
    for i in range(1, n + 1):
      L[i] = L[i - 1] * ia % s.MOD
    return L

t = lcm(A[0], A[1])
inv = Inv(MOD)
for i in range(2, N):
  t = lcm(t, A[i]) % MOD

ans = 0
for i in range(N):
  ans = (ans + t * inv.invx(A[i])) % MOD
print(ans)
Problem p02851 (python)

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

if K == 1:
  print(0)
  exit()

L = [-1] * N
D = {}
D[A[0] % K] = 1
t = A[0] % K
L[0] = t
for i in range(1, min(K - 1, N)):
  t = (t + A[i] - 1) % K
  if t < 0:
    t += K
  if t in D:
    D[t] += 1
  else:
    D[t] = 1
  L[i] = t

n = 1
if n in D:
  ans = D[n]
else:
  ans = 0
for i in range(N):
  if L[i] != -1:
    D[L[i]] -= 1
  if i + K - 1 < N:
    t = (t + A[i + K - 1] - 1) % K
    if t < 0:
      t += K
    if t in D:
      D[t] += 1
    else:
      D[t] = 1
    L[i + K - 1] = t
  n = (n - 1 + A[i]) % K
  #n -= 1
  if n < 0:
    n += K
  if n in D:
    ans += D[n]
  
print(ans)

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

L = [-1] * N
D = [0] * K
D[A[0] % K] = 1
t = A[0] % K
L[0] = t
for i in range(1, min(K - 1, N)):
  t = (t + A[i] - 1) % K
  if t < 0:
    t += K
  D[t] += 1
  L[i] = t

n = 1
ans = D[n]
for i in range(N):
  if L[i] != -1:
    D[L[i]] -= 1
  if i + K - 1 < N:
    t = (t + A[i + K - 1] - 1) % K
    if t < 0:
      t += K
    D[t] += 1
    L[i + K - 1] = t
  n = (n - 1 + A[i]) % K
  #n -= 1
  if n < 0:
    n += K
  ans += D[n]
  
print(ans)

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

L = [-1] * N
D = [0] * K
D[A[0] % K] = 1
t = A[0] % K
L[0] = t
for i in range(1, min(K - 1, N)):
  t = (t + A[i] - 1) % K
  if t < 0:
    t += K
  D[t] += 1
  L[i] = t

n = 1
ans = D[n]
for i in range(N):
  if L[i] != -1:
    D[L[i]] -= 1
  if i + K - 1 < N:
    t = (t + A[i + K - 1] - 1) % K
    if t < 0:
      t += K
    D[t] += 1
    L[i + K - 1] = t
  n = (n - 1 + A[i]) % K
  #n -= 1
  if n < 0:
    n += K
  ans += D[n]
  
print(ans)
Problem p02863 (python)

Fixed submission:
N, T = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]


AB.sort(reverse = True)
DP = [[0] * T for _ in range(N)]
DP[0][0] = AB[0][1]
for i in range(1, N):
  a, b = AB[i]
  DP[i][0] = max(DP[i - 1][0], b)
  for j in range(T):
    DP[i][j] = max(DP[i][j], DP[i - 1][j])
    if j + a < T:
      DP[i][j + a] = DP[i - 1][j] + b

print(max(DP[-1]))

Buggy submission (verdict: Runtime Error):
N, T = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]

ans = 0

DP = [0] * T

for i in range(N):
  a, b = AB[i]
  for j in range(T - 1, -1, -1):
    if j + a >= T:
      ans = max(ans, DP[j] + b)
    else:
      DP[j + a] = max(DP[j + a], DP[j] + b)

for i in range(N):
  ans = max(ans, DP[i])
print(ans)
Problem p02884 (python)

Fixed submission:
N, M = list(map(int, input().split()))
st = [list(map(int, input().split())) for _ in range(M)]

R = {}
for s, t in st:
  s -= 1
  t -= 1
  if s in R:
    R[s].append(t)
  else:
    R[s] = [t]

DP = [0] * N
ans = N
D = [0] * N
for i in range(N - 2, -1, -1):
  ma = 0
  su = 0
  le = len(R[i])
  for j in R[i]:
    su += DP[j]
    ma = max(ma, DP[j])

  DP[i] = su / le + 1 
  if le == 1:
    D[i] = 0
  else:
    D[i] = DP[i] - (((su - ma) / (le - 1)) + 1)

P = [0] * N 
P[0] = 1
ma = 0
man = -1
for i in range(N - 1):
  t = P[i] / len(R[i])
  for j in R[i]:
    P[j] += t 
  t = P[i] * D[i]
  R[i].sort()
  if ma < t:
    ma = t
    man = i
 
if ma == -1:
  print(DP[0])
else:
  print(DP[0] - ma)

Buggy submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
st = [list(map(int, input().split())) for _ in range(M)]

R = {}
for s, t in st:
  s -= 1
  t -= 1
  if s in R:
    R[s].append(t)
  else:
    R[s] = [t]

ans = N
for i in range(N - 1):
  DP = [0] * N
  for j in range(N - 2, -1, -1):
    su = 0
    amax = 0
    cnt = 0
    for x in R[j]:
      amax = max(amax, DP[x] + 1)
      su += A[-1]
    DP[j] = su / cnt
    if i == j and cnt != 1:
      DP[j] = (su - amax) / (cnt - 1)

  ans = min(ans, DP[0])
 
print(ans)
Problem p02914 (python)

Fixed submission:
import math

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

def honya(n, L):
  k = 0
  E = len(L)
  for i in range(n - 1, -1, -1):
    for j in range(k, E):
      if (L[j] >> i) & 1:
        L[k], L[j] = L[j], L[k]
        break
    else:
      continue
    k += 1
    for j in range(k, E):
      if (L[j] >> i) & 1:
        L[j] ^= L[k - 1]
  return L

def nasu(n):
  return math.ceil(math.log(n + 1, 2))

A.sort(reverse = True)
M = nasu(A[0])
P = [1]
for i in range(M):
  P.append(P[-1] * 2)

D = [0] * M
for i in range(N):
  t = A[i]
  for j in range(M):
    if t & 1:
      D[j] += 1
    t >>= 1

ans = 0
for i in range(M - 1, -1, -1):
  if D[i] % 2 == 1:
    ans += P[i]
    for j in range(N):
      A[j] &= ~(1 << i)

L = honya(M, A)
t = 0
for i in range(N):
  if L[i] == 0: break
  if (t & (1 << (nasu(L[i]) - 1))) == 0:
    t ^= L[i]

for i in  range(M - 1, -1, -1):
  if (t >> i) & 1:
    ans += P[i] * 2
    
print(ans)

Buggy submission (verdict: Runtime Error):
import math

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

X = []
Y = []

def nasu(n):
  return math.ceil(math.log(n, 2))

def honya(n, L, X, Y):
  if len(L) >= 2:
    X.append(L[0])
    Y.append(L[1:])
    return True
  cnt = 0
  for i in Y:
    if (i >> (n - 1)) & 1:
      cnt += 1
  if cnt % 2 == 1:
    X.append(L)
    return True


A.sort(reverse = True)

M = nasu(A[0])

D = [0] * M
for i in range(N):
  t = A[i]
  for j in range(M):
    if t & 1:
      D[j] += 1
    t >>= 1

cnt = 0
ans = 0
X = []
Y = []
for i in range(M, 0, -1):
  L = []
  while nasu(A[cnt]) == i:
    L.append(A[cnt])
    cnt += 1
    if cnt == N:
      break
  else:
    if D[i - 1] % 2 == 1:
      X.append(L)
      ans += pow(2, i - 1)
    else:
      if honya(i, L, X, Y):
        ans += pow(2, M - 1) * 2
    continue
  break
print(ans)
Problem p02998 (python)

Fixed submission:
N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]

class Union:
  def __init__(s, n):
    s.N = n
    s.L = [-1] * n

  def root(s, x):
    if s.L[x] < 0:
      return x, -s.L[x]
    return s.root(s.L[x])

  def merge(s, x, y):
    a, ar = s.root(x)
    b, br = s.root(y)
    if a == b:
      return True
    if ar < br:
      s.L[b] -= ar
      s.L[a] = b
    else:
      s.L[a] -= br
      s.L[b] = a
    return False

  def getList(s):
    D = []
    DN = [0] * N
    for i in range(N):
      if s.L[i] < 0:
        DN[i] = len(D)
        D.append([])
    for i in range(N):
      D[DN[s.root(i)[0]]].append(i)
    return D

M = 10**5 + 1
xL = [-1] * M
yL = [-1] * M
uni = Union(N)

for i in range(N):
  x = xy[i][0]
  y = xy[i][1]
  if xL[x] != -1:
    uni.merge(xL[x], i)
  if yL[y] != -1:
    uni.merge(yL[y], i)
  xL[x] = i
  yL[y] = i

L = uni.getList()
ans = 0
for t in L:
  xL = [0] * M
  yL = [0] * M
  xN = 0
  yN = 0
  for i in t:
    x, y = xy[i]
    if xL[x] == 0:
      xL[x] = 1
      xN += 1
    if yL[y] == 0:
      yL[y] = 1
      yN += 1
  ans += xN * yN - len(t)

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]

M = 10 * 5 + 1
D = [[0] * M for _ in range(M)]

X = {}
Y = {}
for x, y in xy:
  if x in X:
    X[x].append(y)
  else:
    X[x] = [y]
  if y in Y:
    Y[y].append(x)
  else:
    Y[y] = [x]
  D[x][y] = 1

ans = 0
for xx in X.keys():
  x = X[xx]
  L = len(x)
  if L == 1: continue
  for i in range(L - 1):
    if x[i] not in Y: continue
    y = Y[x[i]]
    for j in range(i + 1, L):
      for k in y:
        if D[k][x[j]] == 0:
          ans += 1

print(ans)
Problem p03013 (python)

Fixed submission:
N, M = list(map(int, input().split()))
a = [int(input()) for _ in range(M)]

class comb():
  F = [1, 1]
  Fi = [1, 1]
  I = [0, 1]
  def __init__(self, num, mod):
    self.MOD = mod
    for i in range(2, num + 1):
      self.F.append((self.F[-1] * i) % mod)
      self.I.append(mod - self.I[mod % i] * (mod // i) % mod)
      self.Fi.append(self.Fi[-1] * self.I[i] % mod)
  def com(self, n, k):
    if n < k: return 0
    if n < 0 or k < 0: return 0
    return self.F[n] * (self.Fi[k] * self.Fi[n - k] % self.MOD) % self.MOD


MOD = 10 ** 9 + 7

for i in range(1, M):
  if a[i] - a[i - 1] == 1:
    print(0)
    exit()

com = comb(N, MOD)
ans = 1
x = 0
a = a + [N + 1]

for i in a:
  t = i - 1 - x
  at = 0
  for j in range(t // 2 + 1):
    k = t - j
    at = at + com.com(k, j) % MOD
  ans = ans * at % MOD
  x = i + 1
  

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
a = [int(input()) for _ in range(M)]

class comb():
  F = [1, 1]
  Fi = [1, 1]
  I = [0, 1]
  def __init__(self, num, mod):
    self.MOD = mod
    for i in range(2, num + 1):
      self.F.append((self.F[-1] * i) % mod)
      self.I.append(mod - self.I[mod % i] * (mod // i) % mod)
      self.Fi.append(self.Fi[-1] * self.I[i] % mod)
  def com(self, n, k):
    if n < k: return 0
    if n < 0 or k < 0: return 0
    return self.F[n] * (self.Fi[k] * self.Fi[n - k] % self.MOD) % self.MOD


MOD = 10 ** 9 + 7

for i in range(1, M):
  if a[i] - a[i - 1] == 1:
    print(0)
    exit()

com = comb(N, MOD)
ans = 1
x = 0
a = a + [N + 1]

for i in a:
  t = i - 1 - x
  at = 0
  for j in range(t // 2 + 1):
    k = t - j
    at = at + com.com(k, j) % MOD
  ans = ans * at % MODs
  x = i + 1
  

print(ans)
Problem p03016 (python)

Fixed submission:
import math
import sys
sys.setrecursionlimit(10000)

def nasu(A, B, L, D, M):
  if L == 1:
    return A
  ans = nasuA(A, L, D, M)
  ans = (ans + nasuB(1, L - 1, D, M) * B * D) % M
  return ans % M

def nasuB(B, L, D, M):
  if L == 1:
    return B
  ans = powB(1, L, D, M) % M
  return ans % M

def powB(B, L, D, M):
  if L == 1:
    return B
  k = 0
  t = D
  while T[k + 1] <= L:
    k += 1
    t = t * t % M
    if k + 1 == len(T):
      break
  ans = powB2(B, T[k], D, M) % M
  if L != T[k]:
    ans += nasu(T[k] + 1, 1, L - T[k], D, M) * t % M 
    #ans += powB(1, L - T[k], D, M) * t % M
    #ans += powA(-T[k], L - T[k], D, M) * t % M
  return ans % M
 
def powB2(B, L, D, M):
  if L == 1:
    return 1
  ans = powB2(B * 2, L // 2, D * D % M, M) * (D + 1) % M
  ans = ans + nasuA(1, L // 2, D * D % M, M) * B * D % M
  return ans

def nasuA(A, L, D, M):
  if L == 1:
    return A
  ans = powA(A, L, D, M) % M
  return ans % M

def powA(A, L, D, M):
  if L == 1:
    return A
  k = 0
  t = D
  while T[k + 1] <= L:
    k += 1
    t = t * t % M
    if k + 1 == len(T):
      break
  ans = powA2(A, T[k], D, M)
  if L != T[k]:
    ans +=  powA(A, L - T[k], D, M) * t % M
  return ans % M
 
def powA2(A, L, D, M):
  if L == 1:
    return A
  return powA2(A, L // 2, D * D % M, M) * (D + 1) % M

def powmod(a, n, M):
  ans = 1
  while n:
    if n & 1:
      ans = (ans * a) % M
    a = a * a % M
    n >>= 1
  return ans

L, A, B, M = list(map(int, input().split()))

N = math.ceil(math.log(A + 1, 10))
k = pow(10, N)
D = [[0, 0] for _ in range(20)]
while L > 0:
  n = min(L, (k - 1 - A) // B + 1)
  D[N - 1][0] = A
  D[N - 1][1] = n
  L -= n
  N += 1
  k *= 10
  A += n * B
T = [1]
while T[-1] < 10 ** 19:
  T.append(T[-1] * 2)
BI = (((B // M) + 1) * M - B) % M
ans = 0
for i in range(20):
  l = D[i][1]
  a = D[i][0] + (l - 1) * B
  if l == 0:
    continue
  ans = ans * powmod(pow(10, i + 1), l, M) % M
  ans = (ans + nasu(a, BI, l, pow(10, i + 1), M)) % M
print(ans)

Buggy submission (verdict: Runtime Error):
#memo
exit()

import math

def nasu(A, B, L, D, M, T):
  ans = nasuA(A, B, L, D, M, T)
  ans += nasuB(B, L, D, M, T)
  return ans % M

def nasuA(A, B, L, D, M, T):
  ans = A % M
  ans += powA(A, B, L, D, M, T) % M
  return ans % M

def powA(A, B, L, D, M, T):
  k = 0
  while T[k + 1] <= L:
    k += 1
    if k + 1 == len(L):
      break
  ans = powA2(A, T[k], D, M)
  if L != T[k]:
    ans +=  powA(A, B, L - T[k], D, M) * k % M
  return ans % M
 
def powA2(A, L, D, M):



L, A, B, M = list(map(int, input().split()))

N = math.ceil(math.log(A + 1, 10))
k = pow(10, N)
D = [[0, 0] for _ in range(20)]
while L > 0:
  n = min(L, (k - 1 - A) // B + 1)
  print(n)
  D[N - 1][0] = A
  D[N - 1][1] = n
  L -= n
  N += 1
  k *= 10
  A += n * B

T = [1]
while T[-1] < 10 ** 18:
  T.append(T[-1] * 2)

print(D)
ans = 0
for i in range(20):
  a = D[i][0]
  l = D[i][1]
  if l == 0:
    continue
  ans = (ans + nasu(a, B, l, pow(10, i), M, T)) % M
  
print(ans)
Problem p03034 (python)

Fixed submission:
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

N = int(input())
s = list(map(int, input().split()))

D = [[]]
for i in range(1, N):
  DD = []
  T = [0]
  for j in range(i, N, i):
    T.append(T[-1] + s[j])
  DD.append(T)
  T = [0]
  start = (N - 1) % i
  if start == 0: start += i
  for j in range(start, N, i):
    T.append(T[-1] + s[j])
  DD.append(T)
  D.append(DD)

ans = s[-1]
cnt = 0
for n in range(2, N - 1):
  end = N - 1 - n
  for m in make_divisors(end):
    if n <= m: break
    if n % m == 0 and end >= n: continue
    k = end // m
    a = D[m][0][k]
    l = len(D[m][1])
    b = D[m][1][-1] - D[m][1][l - k - 2]
    ans = max(ans, a + b)

print(ans)

Buggy submission (verdict: Runtime Error):
import sympy

N = int(input())
s = list(map(int, input().split()))

D = [[]]
for i in range(1, N):
  DD = []
  T = [0]
  for j in range(i, N, i):
    T.append(T[-1] + s[j])
  DD.append(T)
  T = [0]
  start = (N - 1) % i
  if start == 0: start += i
  for j in range(start, N, i):
    T.append(T[-1] + s[j])
  DD.append(T)
  D.append(DD)

ans = s[-1]
cnt = 0
for n in range(2, N - 1):
  end = N - 1 - n
  for m in sympy.divisors(end):
    if n <= m: break
    if n % m == 0 and end >= n: continue
    k = end // m
    a = D[m][0][k]
    l = len(D[m][1])
    b = D[m][1][-1] - D[m][1][l - k - 2]
    ans = max(ans, a + b)

print(ans)
Problem p03038 (python)

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

A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key = lambda x: x[1], reverse = True)
cnt = 0
for b, c in BC:
  for i in range(b):
    if A[cnt] < c:
      A[cnt] = c
      cnt += 1
      if cnt >= N:
        break
    else:
      break
  if cnt >= N:
    break

print(sum(A))

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

A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key = lambda x: x[1], reverse = True)
cnt = 0
for b, c in BC:
  for i in range(b):
    if A[cnt] < c:
      A[cnt] = c
      cnt += 1
    else:
      break

print(sum(A))
Problem p03053 (python)

Fixed submission:
H, W = list(map(int, input().split()))
A = [input() for _ in range(H)]
b = "#"
w = "."

wc = 0
L = []
wb = [[0] * W for _ in range(H)]
for i in range(H):
  for j in range(W):
    if A[i][j] == w:
      wc += 1
    else:
      L.append([i, j])
      wb[i][j] = 1

Ans = 0
while wc > 0:
  L2 = []
  Ans += 1
  for ki, kj in L:
    if ki - 1 >= 0:
      if wb[ki - 1][kj] == 0:
        wb[ki - 1][kj] = 1
        wc -= 1
        L2.append([ki - 1 , kj])
    if ki + 1 < H:
      if wb[ki + 1][kj] == 0:
        wb[ki + 1][kj] = 1
        wc -= 1
        L2.append([ki + 1 , kj])
    if kj - 1 >= 0:
      if wb[ki][kj - 1] == 0:
        wb[ki][kj - 1] = 1
        wc -= 1
        L2.append([ki, kj - 1])
    if kj + 1 < W:
      if wb[ki][kj + 1] == 0:
        wb[ki][kj + 1] = 1
        wc -= 1
        L2.append([ki, kj + 1])
  L = L2
print(Ans)

Buggy submission (verdict: Runtime Error):
H, W = list(map(int, input().split()))
A = [input() for _ in range(H)]
b = "#"
w = "."

wc = 0
L = []
wb = [[0] * W for _ in range(H)]
for i in range(H):
  for j in range(W):
    if A[i][j] == w:
      wc += 1
    else:
      L.append([i, j])
      wb[i][j] = 1

Ans = 0
while wc > 0:
  L2 = []
  Ans += 1
  for ki, kj in L:
    if ki - 1 >= 0:
      if wb[ki - 1][kj] == 0:
        wb[ki - 1][kj] = 1
        wc -= 1
        L2.append([ki - 1 , kj])
    if ki + 1 < W:
      if wb[ki + 1][kj] == 0:
        wb[ki + 1][kj] = 1
        wc -= 1
        L2.append([ki + 1 , kj])
    if kj - 1 >= 0:
      if wb[ki][kj - 1] == 0:
        wb[ki][kj - 1] = 1
        wc -= 1
        L2.append([ki, kj - 1])
    if kj + 1 < H:
      if wb[ki][kj + 1] == 0:
        wb[ki][kj + 1] = 1
        wc -= 1
        L2.append([ki, kj + 1])
  L = L2
print(Ans)

Buggy submission (verdict: Runtime Error):
H, W = list(map(int, input().split()))
A = [input() for _ in range(H)]

b = "#"
w = "."

def out(i, j):
  if i < 0:
    return True
  if i >= W:
    return True
  if j < 0:
    return True
  if j >= H:
    return True
  return False

def check(i, j):
  if A[i][j] == b:
    return 0
  k = 1
  while True:
    F = False
    for ii in range(i - k, i):
      jj = j - (k - (i - ii))
      if out(ii, jj):
        continue
      if A[ii][jj] == b:
        return k
    for ii in range(i - k, i):
      jj = j + (k - (i - ii))
      if out(ii, jj):
        continue
      if A[ii][jj] == b:
        return k
    for ii in range(i, i + k + 1):
      jj = j - (k + (i - ii))
      if out(ii, jj):
        continue
      if A[ii][jj] == b:
        return k
    for ii in range(i, i + k + 1):
      jj = j + (k + (i - ii))
      if out(ii, jj):
        continue
      if A[ii][jj] == b:
        return k
    k += 1

Ans = 0
for i in range(H):
  for j in range(W):
    k = check(i, j)
    Ans = max(Ans, k)

print(Ans)
Problem p03055 (python)

Fixed submission:
N = int(input())
if N == 1:
  print("First")
  exit()
ab = [list(map(int, input().split())) for _ in range(N - 1)]

R = {}
for a, b in ab:
  if a - 1 in R:
    R[a - 1].append(b - 1)
  else:
    R[a - 1] = [b - 1]
  if b - 1 in R:
    R[b - 1].append(a - 1)
  else:
    R[b - 1] = [a - 1]

L = [0] * N
L[0] = 1
F = []
RL = {}
RL[1] = [0]
RL[2] = []
for i in R[0]:
  F.append(i)
  L[i] = 2
  RL[2].append(i)

k = 2
while F != []:
  k += 1
  Ft = []
  RL[k] = []
  for i in F:
    for j in R[i]:
      if L[j] == 0:
        L[j] = k
        Ft.append(j)
        RL[k].append(j)
  F = Ft

d = 0
D = [0] * N
for i in range(k - 1, 0, -1):
  for j in RL[i]:
    for k in R[j]:
      if L[k] == i - 1:
        if D[k] == 0:
          D[k] = D[j] + 1
          d = max(d, D[j] + 1)
        else:
          d = max(d, D[j] + D[k] + 1)
          D[k] = max(D[k], D[j] + 1)

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

Buggy submission (verdict: Runtime Error):
N = int(input())
ab = [list(map(int, input().split())) for _ in range(N - 1)]

T = [[0] * N for _ in range(N)]
R = {}
for a, b in ab:
  T[a - 1][b - 1] = 1
  T[b - 1][a - 1] = 1
  if a - 1 in R:
    R[a - 1].append(b - 1)
  else:
    R[a - 1] = [b - 1]
  if b - 1 in R:
    R[b - 1].append(a - 1)
  else:
    R[b - 1] = [a - 1]

L = [0] * N
L[0] = 1
F = []
RL = {}
RL[1] = [0]
RL[2] = []
for i in R[0]:
  F.append(i)
  L[i] = 2
  RL[2].append(i)

k = 2
while F != []:
  k += 1
  Ft = []
  RL[k] = []
  for i in F:
    for j in R[i]:
      if L[j] == 0:
        L[j] = k
        Ft.append(j)
        RL[k].append(j)
  F = Ft

d = 0
D = [0] * N
for i in range(k - 1, 0, -1):
  for j in RL[i]:
    for k in R[j]:
      if L[k] == i - 1:
        if D[k] == 0:
          D[k] = D[j] + 1
          d = max(d, D[j] + 1)
        else:
          d = max(d, D[j] + D[k] + 1)
          D[k] = max(D[k], D[j] + 1)

if d % 3 == 1:
  print("Second")
else:
  print("First")
Problem p03108 (python)

Fixed submission:
def rootT(x):
  if x == T[x]:
    return x
  x = T[x]
  return rootT(x)

def mergeT(x, y):
  a = rootT(x)
  b = rootT(y)
  if a != b:
    if NumTable[a] > NumTable[b]:
      T[b] = a
      NumTable[a] += NumTable[b]
    else:
      T[a] = b
      NumTable[b] += NumTable[a]

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(M)]

AB.reverse()

T = [i for i in range(N + 1)]
NumTable = [1 for i in range(N + 1)]

L = []
Ans = N * (N - 1) // 2
for i in AB:
  L.append(Ans)
  a = rootT(i[0])
  b = rootT(i[1])
  if a != b:
    ta = NumTable[a]
    tb = NumTable[b]
    mergeT(a, b)
    Ans -= (ta + tb) * (ta + tb - 1) // 2  - ta * (ta - 1) // 2 - tb * (tb - 1) // 2
    mergeT(a, b)

for i in L[::-1]:
  print(i)

Buggy submission (verdict: Runtime Error):
def rootT(x):
  if x == T[x]:
    return x
  x = T[x]
  return rootT(x)

def mergeT(x, y):
  a = rootT(x)
  b = rootT(y)
  if a != b:
    if unionTable[a] > unionTable[b]:
      T[b] = a
      NumTable[a] += NumTable[b]
    else:
      T[a] = b
      NumTable[b] += NumTable[a]

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(M)]

AB.reverse()

T = [i for i in range(N + 1)]
unionTable = [-1 for i in range(N + 1)]
NumTable = [1 for i in range(N + 1)]

L = []
Ans = N * (N - 1) // 2
for i in AB:
  L.append(Ans)
  a = rootT(i[0])
  b = rootT(i[1])
  if a != b:
    ta = NumTable[a]
    tb = NumTable[b]
    mergeT(a, b)
    Ans -= (ta + tb) * (ta + tb - 1) // 2  - ta * (ta - 1) // 2 - tb * (tb - 1) // 2
    mergeT(a, b)

for i in L[::-1]:
  print(i)
Problem p03232 (python)

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

mod = 10**9 + 7

F = 1
for i in range(2, N + 1):
	F = F * i % mod

D = [0]
for i in range(2, N + 1):
	D.append(D[-1] + pow(i, mod - 2, mod))

L = [0] * N
for i in range(N // 2 + N % 2):
  t = round(F * (D[-1 - i] + D[i] + 1)) % mod
  L[i] = t
  L[-1 - i] = t

Ans = 0
for i in range(N):
  Ans += L[i] * A[i] % mod

print(Ans % mod)

Buggy submission (verdict: Runtime Error):
import math

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

mod = 10**9 + 7

F = 1
for i in range(2, N + 1):
	F *= i

D = [1]
for i in range(2, N + 1):
	D.append(D[-1] + 1 / i)

L = [0] * N
for i in range(N // 2 + N % 2):
  L[i] = F * (D[-1 - i] + D[i] - 1) % mod
  L[-1 - i] = L[i]

Ans = 0
for i in range(N):
  Ans += math.ceil(L[i] * A[i]) % mod
print(Ans % mod)