Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
A,B,C,D =map(int,input().split())
if A+B>C+D:
  print("Left")
else A+B=C+D
  print("Balanced")
else:
  print("Right")

Fixed submission:
A,B,C,D =map(int,input().split())
if A+B>C+D:
  print("Left")
elif A+B==C+D:
  print("Balanced")
else:
  print("Right")

Buggy python submission (verdict: Runtime Error):
n,a,b=map(int,input().split())
ans=0
for i in range(n+1):
    if a<=sum(list(map(int, list(str(i)))))<=b:
        ans += i
print(ans)

Fixed submission:
A,B,C,D =map(int,input().split())
if A+B>C+D:
  print("Left")
elif A+B==C+D:
  print("Balanced")
else:
  print("Right")

Buggy python submission (verdict: Runtime Error):
n,a,b=map(int,input().split())
ans=0
for i in range(n+1):
    if a<=sum(list(map(int, sum(str(i)))))<=b:
        ans += i
print(ans)

Fixed submission:
A,B,C,D =map(int,input().split())
if A+B>C+D:
  print("Left")
elif A+B==C+D:
  print("Balanced")
else:
  print("Right")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int,input().split()))
A = list(set(a))
c = 0
ac = 0
for i in range(len(A)):
    ac = a.count(A[i])
    if ac >= A[i]:
        c += ac - A[i]
    elif ac > 0:
        c += ac
print(c)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))
if N == 1:
    if a[0] != 1:
        print(1)
        exit()
    else:
        print(0)
        exit()
a.sort()
A = 0
c = 0
while True:
    try:
        #print(a)
        A = a[0]
        if len(a)>1:
            for i in range(1, len(a)):
                if a[i] != A:
                    # print(i + 1)
                    # c += i
                    del a[:i]
                    if i >= A:
                        c += i - A
                    else:
                        c += i
                    # print(a[i], c)
                    break
                elif i == len(a) - 1:
                    if i + 1 >= A:
                        c += i + 1 - A
                    else:
                        c += i + 1
                    print(c)
                    exit()
        else:
            print(c + 1)
            exit()
    except IndexError:
        break

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int,input().split()))
A = list(set(a))
c = 0
for i in range(len(A)):
    if a.count(A[i]) >= A[i]:
        c += a.count(A[i]) - A[i]
    elif a.count(A[i]) > 0:
        c += a.count(A[i])
print(c)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))
if N == 1:
    if a[0] != 1:
        print(1)
        exit()
    else:
        print(0)
        exit()
a.sort()
A = 0
c = 0
while True:
    try:
        #print(a)
        A = a[0]
        if len(a)>1:
            for i in range(1, len(a)):
                if a[i] != A:
                    # print(i + 1)
                    # c += i
                    del a[:i]
                    if i >= A:
                        c += i - A
                    else:
                        c += i
                    # print(a[i], c)
                    break
                elif i == len(a) - 1:
                    if i + 1 >= A:
                        c += i + 1 - A
                    else:
                        c += i + 1
                    print(c)
                    exit()
        else:
            print(c + 1)
            exit()
    except IndexError:
        break
Buggy python submission (verdict: Time Limit Exceeded):
import collections
N, K = map(int, input().split())
A = list(map(int, input().split()))
a = collections.Counter(A)
c = len(A)
for i in range(K):
    try:
        c -= a.most_common()[i][1]
    except IndexError:
        print(c)
        exit()
print(c)


Fixed submission:
import collections
N, K = map(int, input().split())
A = list(map(int, input().split()))
a = collections.Counter(A)
c = len(A)
#print(a.most_common(K))
S = a.most_common(K)
for i in range(K):
    try:
        c -= S[i][1]
    except IndexError:
        print(c)
        exit()
print(c)
Buggy python submission (verdict: Runtime Error):
S = list(input())
T = list(input())
LS = len(S)
LT = len(T)
a = ""
for i in range(LS - LT + 1):
    for j in range(LT):
        if S[i + j] != T[j] and S[i + j] != "?":
            break
        if j == LT - 1:
            for k in range(LT):
                S[i + k] = T[k]
            #print("".join(S))
            s = str("".join(S)).replace("?", "a")
            print(s)
            exit()
# S = S.replace("?", "a")
print("UNRESTORABLE")a


Fixed submission:
S = list(input())
T = list(input())
LS = len(S)
LT = len(T)
a = ""
for i in reversed(range(LS - LT + 1)):
    for j in range(LT):
        if S[i + j] != T[j] and S[i + j] != "?":
            break
        if j == LT - 1:
            for k in range(LT):
                S[i + k] = T[k]
            #print("".join(S))
            s = str("".join(S)).replace("?", "a")
            print(s)
            exit()
# S = S.replace("?", "a")
print("UNRESTORABLE")
Buggy python submission (verdict: Runtime Error):
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def lcm(a, b):
    return int(a * b / gcd(max(a, b), min(a, b)))


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

A = T[0]
for i in range(N):
    A = lcm(A, T[i])
print(A)


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


def lcm(a, b):
    return a * b // gcd(max(a, b), min(a, b))


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

A = T[0]
for i in range(N):
    A = lcm(A, T[i])
print(A)

Buggy python submission (verdict: Runtime Error):
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def lcm(a, b):
    return int(a * b / gcd(max(a, b), min(a, b)))


N = int(input())
T = [int(input()) for i in range(N)]
if N == 1:
    print(T[0])
    exit()
A = T[0]
for i in range(1, N):
    A = lcm(A, T[i])
print(A)


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


def lcm(a, b):
    return a * b // gcd(max(a, b), min(a, b))


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

A = T[0]
for i in range(N):
    A = lcm(A, T[i])
print(A)

Buggy python submission (verdict: Runtime Error):
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def lcm(a, b):
    return int(a * b / gcd(max(a, b), min(a, b)))


N = int(input())
T = [int(input()) for i in range(N)]
A = T[0]
for i in range(1, N):
    A = lcm(A, T[i])
print(A)


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


def lcm(a, b):
    return a * b // gcd(max(a, b), min(a, b))


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

A = T[0]
for i in range(N):
    A = lcm(A, T[i])
print(A)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, (input().split()))
L = [[0 for i in range(N)] for j in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    L[a - 1][b - 1] = 1
    L[b - 1][a - 1] = 1
#print(L)
for i in range(N):
    if L[0][i] == 1:
        if L[i][N - 1] == 1:
            print("POSSIBLE")
            exit()
print("IMPOSSIBLE")


Fixed submission:
N, M = map(int, (input().split()))
L1 = [0 for i in range(N)]
L2 = [0 for j in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    if a == 1:
        L1[b - 1] = 1
    elif b == N:
        L2[a - 1] = 1
for i in range(N):
    if L1[i] == L2[i] == 1:
        print("POSSIBLE")
        exit()
print("IMPOSSIBLE")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = [int(input()) for _ in range(N)]
seen_list = []
n = 1
ans = 0
while True:
    if n in seen_list:
        print(-1)
        exit()
    else:
        if n == 2:
            print(ans)
            exit()
        else:
            seen_list.append(n)
            n = a[n - 1]
            ans += 1


Fixed submission:
from collections import defaultdict

N = int(input())
a = [int(input()) for _ in range(N)]
seen_list = defaultdict(int)
n = 1
ans = 0
while True:
    if seen_list[n] == 1:
        print(-1)
        exit()
    else:
        if n == 2:
            print(ans)
            exit()
        else:
            seen_list[n] = 1
            n = a[n - 1]
            ans += 1
Buggy python submission (verdict: Runtime Error):
def an(a, b):  # a is time b is place
    if a > b:
        return float('INF')
    elif a == b:
        return a
    else:
        return min(an(a + 1, b), an(a + 1, b - a))


X = int(input())
print(an(1, X))


Fixed submission:
X = int(input())
c = 0
for i in range(1, 1000000000):
    c += i
    if c > X:
        print(i)
        exit()
    elif c == X:
        print(i)
        exit()
Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

l = {}
r = {}

for i in range(n):
    x = i + an[i]
    if x not in l:
        l[x] = [i]
    else:
        l[x].append(i)

    x = i - an[i]
    if x not in r:
        r[x] = [i]
    else:
        r[x].append(i)

sum = 0
for k in l:
    if k in r:
        for i in range(len(l[k])):
            for j in range(len(r[k])):
                if l[k][i] != r[k][j]:
                    sum += 1
print(sum)


Fixed submission:
from sys import stdin

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

l = {}
r = {}

for i in range(n):
    x = i + an[i]
    if x not in l:
        l[x] = [i]
    else:
        l[x].append(i)

    x = i - an[i]
    if x not in r:
        r[x] = [i]
    else:
        r[x].append(i)

sum = 0
for k in l:
    if k in r:
        sum += len(l[k])*len(r[k])

print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

sum = 0
for i in range(n-1):
    for j in range(i+an[i], n):
        if j-i == (an[i]+an[j]):
            sum += 1

print(sum)


Fixed submission:
from sys import stdin

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

l = {}
r = {}

for i in range(n):
    x = i + an[i]
    if x not in l:
        l[x] = [i]
    else:
        l[x].append(i)

    x = i - an[i]
    if x not in r:
        r[x] = [i]
    else:
        r[x].append(i)

sum = 0
for k in l:
    if k in r:
        sum += len(l[k])*len(r[k])

print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

sum = 0
for i in range(n-1):
    for j in range(i+1, n):
        if j-i == (an[i]+an[j]):
            sum += 1

print(sum)


Fixed submission:
from sys import stdin

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

l = {}
r = {}

for i in range(n):
    x = i + an[i]
    if x not in l:
        l[x] = [i]
    else:
        l[x].append(i)

    x = i - an[i]
    if x not in r:
        r[x] = [i]
    else:
        r[x].append(i)

sum = 0
for k in l:
    if k in r:
        sum += len(l[k])*len(r[k])

print(sum)
Buggy python submission (verdict: Runtime Error):
from sys import stdin


def dprint(*args):
    #print(*args)
    pass


n, a, b, c = [int(x) for x in stdin.readline().rstrip().split()]
ans = "Yes"

card = []
sentaku = []

for i in range(n):
    s = stdin.readline().rstrip()
    card.append(s)

if a+b+c == 0:
    ans = "No"

nums = {"A": a, "B": b, "C": c}


def judge(i, j, k):
    global ans
    dprint(i, j, k, ans, sentaku)
    if nums[j] == 0 and nums[k] == 0:
        ans = "No"
        return

    if nums[j] == 0:
        nums[j] += 1
        nums[k] -= 1
        sentaku.append(j)
        return
    if nums[k] == 0:
        nums[k] += 1
        nums[j] -= 1
        sentaku.append(k)
        return

    total = sum(nums.values())
    if total > 2 or i < n-1:
        nums[j] += 1
        nums[k] -= 1
        sentaku.append(j)
        return

    ns = card[i+1]
    if ns[0] == j or ns[1] == j:
        nums[j] += 1
        nums[k] -= 1
        sentaku.append(j)
        return
    else:
        nums[k] += 1
        nums[j] -= 1
        sentaku.append(k)
        return
    pass


for i in range(n):
    s = card[i]
    judge(i, s[0], s[1])


print(ans)

if ans == "Yes":
    for i in range(len(sentaku)):
        print(sentaku[i])


Fixed submission:
from sys import stdin

import time
def dprint(*args):
    #print(*args)
    pass


n, a, b, c = [int(x) for x in stdin.readline().rstrip().split()]
ans = "Yes"

card = []
sentaku = []

for i in range(n):
    s = stdin.readline().rstrip()
    card.append(s)

if a+b+c == 0:
    ans = "No"

nums = {"A": a, "B": b, "C": c}


def judge(i, j, k):
    global ans
    dprint(n, i, j, k, ans, nums, sentaku)
    if nums[j] == 0 and nums[k] == 0:
        ans = "No"
        return

    if nums[j] == 0:
        nums[j] += 1
        nums[k] -= 1
        sentaku.append(j)
        return
    if nums[k] == 0:
        nums[k] += 1
        nums[j] -= 1
        sentaku.append(k)
        return

    total = sum(nums.values())
    if total > 2 or i == n-1:
        if nums[k] > 1:
            nums[j] += 1
            nums[k] -= 1
            sentaku.append(j)
        else:
            nums[k] += 1
            nums[j] -= 1
            sentaku.append(k)
        dprint("tekitou")
        return

    ns = card[i+1]
    if ns[0] == j or ns[1] == j:
        nums[j] += 1
        nums[k] -= 1
        sentaku.append(j)
        return
    else:
        nums[k] += 1
        nums[j] -= 1
        sentaku.append(k)
        return
    pass


for i in range(n):
    s = card[i]
    judge(i, s[0], s[1])

print(ans)

if ans == "Yes":
    for i in range(len(sentaku)):
        print(sentaku[i])
Buggy python submission (verdict: Runtime Error):
from sys import stdin

n = int(stdin.readline().rstrip())

sum = 0
for i in range(n):
    if i%5==0:
        continue
    if i%3==0:
        continue
    sum +=15 i

print(sum)


Fixed submission:
from sys import stdin

n = int(stdin.readline().rstrip())

sum = 0
for i in range(1,n+1):
    if i%5==0:
        continue
    if i%3==0:
        continue
    sum += i

print(sum)
Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
import sys
sys.setrecursionlimit(10000)
from functools import lru_cache

@lru_cache(maxsize=100000)
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

@lru_cache(maxsize=100000)
def gcd3(a,b,c):
   return gcd(gcd(a,b),c)

n = int(stdin.readline().rstrip())

sum = 0

for i in range(n):
    for j in range(n):
        for k in range(n):
            a,b,c = sorted((i+1,j+1,k+1))
            sum += gcd3(a,b,c)

print(sum)


Fixed submission:
from sys import stdin
import sys
sys.setrecursionlimit(10000)
import functools
from functools import lru_cache

#from math import gcd

@lru_cache(maxsize=1000000)
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

def gcdn(nums):
    return functools.reduce(gcd, nums)

@lru_cache(maxsize=1000000)
def gcd3(a,b,c):
   return gcd(gcd(a,b),c)

n = int(stdin.readline().rstrip())

sum = 0
for i in range(n):
    sum += (i+1)
    for j in range(i+1,n):
        sum += 6*gcd(i+1,j+1)
        for k in range(j+1,n):
            #a,b,c = sorted((i+1,j+1,k+1))
            a,b,c = (i+1,j+1,k+1)
            sum += 6*gcdn((a,b,c))

print(sum)
Buggy python submission (verdict: Runtime Error):
from sys import stdin

ntree = []
ans = []


def culceach(n, sum):
    ans[n-1] += sum
    for i in range(len(ntree[n-1])):
        culceach(ntree[n-1][i], ans[n-1])

def addnum(p, x):
    ans[p-1] += x

#    for i in range(len(ntree[p-1])):
#        addnum(ntree[p-1][i], x)

n, q = [int(x) for x in stdin.readline().rstrip().split()]

for i in range(n):
    ntree.append([])
    ans.append(0)

for i in range(n-1):
  a, b = [int(x) for x in stdin.readline().rstrip().split()]
  ntree[a-1].append(b)

for i in range(q):
  p, x = [int(x) for x in stdin.readline().rstrip().split()]
  addnum(p,x)

culceach(1, 0)

print(' '.join([str(n) for n in ans]))


Fixed submission:
from sys import stdin

ntree = []
ans = []


n, q = [int(x) for x in stdin.readline().rstrip().split()]

for i in range(n):
    ntree.append([])
    ans.append(0)

for i in range(n-1):
  a, b = [int(x) for x in stdin.readline().rstrip().split()]
  ntree[a-1].append(b)

for i in range(q):
  p, x = [int(x) for x in stdin.readline().rstrip().split()]
  ans[p-1] += x

for i in range(n):
    for j in range(len(ntree[i])):
       ans[ntree[i][j]-1] += ans[i]

print(' '.join([str(n) for n in ans]))

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

ntree = []
ans = []


def addnum(p, x):
    ans[p-1] += x
    for i in range(len(ntree[p-1])):
        addnum(ntree[p-1][i], x)

n, q = [int(x) for x in stdin.readline().rstrip().split()]

for i in range(n):
    ntree.append([])
    ans.append(0)

for i in range(n-1):
  a, b = [int(x) for x in stdin.readline().rstrip().split()]
  ntree[a-1].append(b)


for i in range(q):
  p, x = [int(x) for x in stdin.readline().rstrip().split()]
  addnum(p,x)

print(' '.join([str(n) for n in ans]))


Fixed submission:
from sys import stdin

ntree = []
ans = []


n, q = [int(x) for x in stdin.readline().rstrip().split()]

for i in range(n):
    ntree.append([])
    ans.append(0)

for i in range(n-1):
  a, b = [int(x) for x in stdin.readline().rstrip().split()]
  ntree[a-1].append(b)

for i in range(q):
  p, x = [int(x) for x in stdin.readline().rstrip().split()]
  ans[p-1] += x

for i in range(n):
    for j in range(len(ntree[i])):
       ans[ntree[i][j]-1] += ans[i]

print(' '.join([str(n) for n in ans]))
Buggy python submission (verdict: Runtime Error):
from sys import stdin
n = int(stdin.readline().rstrip())
ans=0
ba=0
bx=0
xa=0
for i in range(n):
  s = stdin.readline().rstrip()
  for j in range(len(s)-1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans +=1
  if( s[0] == 'B' and s[-1] == 'A' ):
    ba +=1
  elif s[0] == 'B':
    bx +=1
  elif s[-1] == 'A':
    xa +=1

ans += min(xa,bx)
if ba>0:
  ans += (ba-1)
  if and xa!=0 and xa!=bx:
    ans += 1

print(ans)

Fixed submission:
from sys import stdin
n = int(stdin.readline().rstrip())
ans=0
ba=0
bx=0
xa=0
for i in range(n):
  s = stdin.readline().rstrip()
  for j in range(len(s)-1):
    if s[j] == 'A' and s[j+1] == 'B':
      ans +=1
  if( s[0] == 'B' and s[-1] == 'A' ):
    ba +=1
  elif s[0] == 'B':
    bx +=1
  elif s[-1] == 'A':
    xa +=1

ans += min(xa,bx)
if ba>0:
  ans += (ba-1)
  if (xa+bx)!=0:
    ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def f():
     h,w = map(int,input().split())
     field = []
     for i in range(h):
          field.append(list(input()))
          
     que = []
     for i in range(h):
          for j in range(w):
               if field[i][j] == "#":
                    que.append([i,j])
     if que == []:
          print(0)
          return
     cnt = 0
     while que != []:
          que1 = []
          for p in que:
               for v in [[1,0],[0,1],[-1,0],[0,-1]]:
                    newx,newy = p[1]+v[1],p[0]+v[0]
                    if (0<=newx) and (newx<w) and (0<=newy) and (newy<h):
                         if field[newy][newx] == ".":
                              field[newy][newx] = "#"
                              que1.append([newy,newx])
          que = que1
          cnt += 1
     print(cnt-1)
     return
f()


Fixed submission:
def f():
     h,w = map(int,input().split())
     field = []
     for i in range(h):
          field.append(list(input()))
          
     que = []
     for i in range(h):
          for j in range(w):
               if field[i][j] == "#":
                    que.append([i,j])
     if que == []:
          print(0)
          return
     cnt = 0
     while que != []:
          que1 = []
          for p in que:
               for v in [[1,0],[0,1],[-1,0],[0,-1]]:
                    newx,newy = p[1]+v[1],p[0]+v[0]
                    if (0<=newx) and (newx<w) and (0<=newy) and (newy<h):
                         if field[newy][newx] == ".":
                              field[newy][newx] = "#"
                              que1.append([newy,newx])
          que = que1
          cnt += 1
     print(cnt-1)
     return
f()
Buggy python submission (verdict: Runtime Error):
from sys import stdin

def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

left = []
right = []

for i in range(n):
  if(i==0):
    before = an[i]
  else:
    before = gcd(before, an[i])
  left.append(before)

for i in range(n):
  if(i==0):
    before = an[n-1-i]
  else:
    before = gcd(before, an[n-1-i])
  right.append(before)

max = 0
for i in range(n):
  if(i==0):
    before = right[n-2];
  elif(i==(n-1)):
    before = left[n-2];
  else:
    before = gcd(left[i-1],right[n-2-i])
  max = max(before,max)

print(max)

Fixed submission:
from sys import stdin

def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

left = []
right = []

for i in range(n):
  if(i==0):
    before = an[i]
  else:
    before = gcd(before, an[i])
  left.append(before)

for i in range(n):
  if(i==0):
    before = an[n-1-i]
  else:
    before = gcd(before, an[n-1-i])
  right.append(before)

max = 0
for i in range(n):
  if(i==0):
    before = right[n-2];
  elif(i==(n-1)):
    before = left[n-2];
  else:
    before = gcd(left[i-1],right[n-2-i])
  if(max < before ):
    max = before

print(max)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))


max = 0
for i in range(n):
  before = -1
  for j in range(n):
    if( j==i ):
      continue
    if( before == -1 ):
      before = an[j]
    else:
      before = gcd(before, an[j])
  if(max < before ):
    max = before
print(max)


Fixed submission:
from sys import stdin

def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

left = []
right = []

for i in range(n):
  if(i==0):
    before = an[i]
  else:
    before = gcd(before, an[i])
  left.append(before)

for i in range(n):
  if(i==0):
    before = an[n-1-i]
  else:
    before = gcd(before, an[n-1-i])
  right.append(before)

max = 0
for i in range(n):
  if(i==0):
    before = right[n-2];
  elif(i==(n-1)):
    before = left[n-2];
  else:
    before = gcd(left[i-1],right[n-2-i])
  if(max < before ):
    max = before

print(max)