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

Fixed submission:
import bisect
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ans=0
for i in range(n):
  x=b[i]
  y=bisect.bisect_left(a,x)
  z=bisect.bisect_right(c,x)
  ans+=(n-z)*y
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ans=0
for i in range(n):
  x=b[i]
  y=bisect.bisect_left(a,x)
  z=bisect.bisect_right(c,x)
    ans+=(n-z)*y
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ans=0
for i in range(n):
  x=a[i]
  y=bisect.bisect_right(x)
  if y<n:
    z=bisect.bisect_right(b[y])
    ans+=n-z
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ans=0
for i in range(n):
  x=a[i]
  y=bisect.bisect_right(x)
  z=bisect.bisect_right(b[y])
  ans+=n-z
print(ans)
Problem p03561 (python)

Fixed submission:
k,n = map(int,input().split())
if k%2 == 0:
  ans = [k//2]+[k]*(n-1)
else:
  t = n//2
  ans = [k//2+1]*n
  for i in range(t):
    if ans[-1] == 1:
      ans.pop()
    else:
      ans[-1] -= 1
      while len(ans) < n:
        ans.append(k)
print(*ans)

Buggy submission (verdict: Runtime Error):
k,n = map(int,input().split())
if k%2 == 0:
  ans = [k//2]+[k//2+1]*(n-1)
else:
  print(2/0)
  t = n//2
  ans = [k//2+1]*n
  for i in range(t):
    if len(ans) < n:
      ans[-1] -= 1
      ans.append(k)
    elif ans[-1] > 1:
      ans[-1] -= 1
    else:
      ans.pop()
print(*ans)
Problem p03565 (python)

Fixed submission:
s=list(input())
t=list(input())
ls=len(s)
lt=len(t)
if ls<lt:
  print("UNRESTORABLE")
  exit()
for i in range(ls-lt,-1,-1):
  for j in range(i,i+lt):
    if s[j]==t[j-i] or s[j]=="?":
      continue
    else:
      break
  else:
    for k in range(i,i+lt):
      s[k]=t[k-i]
    for l in range(ls):
      if s[l]=="?":
        s[l]="a"
    print(*s,sep="")
    exit()
else:
  print("UNRESTORABLE")

Buggy submission (verdict: Runtime Error):
s=input()
t=input()
ls=len(s)
lt=len(t)
if ls<lt:
  print("UNRESTORABLE")
  exit()
for i in range(ls-lt,-1,-1):
  for j in range(i,i+lt):
    if s[j]==t[j] or s[j]=="?":
      continue
    else:
      break
  else:
    for k in range(i,i+lt):
      s[k]=t[k]
    for l in range(ls):
      if s[l]=="?":
        s[l]="a"
    print(s)
    exit()
else:
  print("UNRESTORABLE")
Problem p03569 (python)

Fixed submission:
s = input()
n = len(s)
t = s.replace("x","")
if not t:
  print(0)
  exit()
l = len(t)
if t != t[::-1]:
  print(-1)
  exit()
plc = [-1]
for i in range(n):
  if s[i] != "x":
    plc.append(i)
plc.append(n)
l = len(plc)
if l%2 == 0:
  fm = plc[:l//2]
  lt = plc[l//2:]
else:
  fm = plc[:l//2+1]
  lt = plc[l//2:]
lt = lt[::-1]
ans = 0
for i in range(1,len(fm)):
  ans += abs((fm[i]-fm[i-1])-(lt[i-1]-lt[i]))
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
n = len(s)
t = s.replace("x","")
if not t:
  print(0)
  exit()
l = len(t)
if t != t[::-1]:
  print(-1)
  exit()
plc = [-1]
for i in range(n):
  if s[i] != x:
    plc.append(i)
plc.append(n)
l = len(plc)
if l%2 == 0:
  fm = plc[:l//2]
  lt = plc[l//2:]
else:
  fm = plc[l//2+1]
  lt = plc[l//2:]
lt = lt[::-1]
ans = 0
for i in range(1,len(fm)):
  ans += abs((fm[i]-fm[i-1])-(lt[i]-lt[i-1]))
print(ans)
Problem p03583 (python)

Fixed submission:
N = int(input())

for x in range(1, 3501):
    for y in range(1, 3501):
        if 4*x*y-N*(x+y) != 0:
            if N*x*y/(4*x*y-N*(x+y)) == int(N*x*y/(4*x*y-N*(x+y))) and int(N*x*y/(4*x*y-N*(x+y))) > 0:
                print(x, y, int(N*x*y/(4*x*y-N*(x+y))))
                break
    else:
        continue
    break

Buggy submission (verdict: Runtime Error):
N = int(input())

for x in range(1, 3501):
    for y in range(1, 3501):
        if N*x*y/(4*x*y-N*(x+y)) == int(N*x*y/(4*x*y-N*(x+y))) and int(N*x*y/(4*x*y-N*(x+y))) > 0:
            print(x, y, int(N*x*y/(4*x*y-N*(x+y))))
            break
    else:
        continue
    break
Problem p03584 (python)

Fixed submission:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
a = [list(map(int,input().split())) for i in range(n)]
ans = [0]
l = max(k.bit_length(),1)
for i in range(l-1,-1,-1):
  if i == 0:
    x = k
  elif k & 1 << i:
    x = (k >> (i+1)) << i+1 | (1 << i)-1
  sm = 0
  for j in range(n):
    if a[j][0] | x == x:
      sm += a[j][1]
  ans.append(sm)
print(max(ans))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
a = [list(map(int,input().split())) for i in range(n)]
ans = []
l = k.bit_length()
for i in range(l-1,-1,-1):
  if i == 0:
    x = k
  elif k & 1 << i:
    x = (k >> (i+1)) << i+1 | (1 << i)-1
  sm = 0
  for j in range(n):
    if a[j][0] | x == x:
      sm += a[j][1]
  ans.append(sm)
print(max(ans))

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = [list(map(int,input().split())) for i in range(n)]
ans = []
l = k.bit_length()
for i in range(l-1,-1,-1):
  if i == 0:
    x = k
  elif k & 1 << i:
    x = (k >> (i+1)) << i+1 | (1 << i)-1
  sm = 0
  for j in range(n):
    if a[j][0] | x == x:
      sm += a[j][1]
  ans.append(sm)
print(max(ans))
Problem p03591 (python)

Fixed submission:
print("NYoe s"[input()[:4]=="YAKI"::2])

Buggy submission (verdict: Runtime Error):
print("NYoe s"[int(input())[:4]=="YAKI"::2])

Buggy submission (verdict: Runtime Error):
print("NYoe s"[int(input())[:4]::2])
Problem p03633 (python)

Fixed submission:
from functools import reduce, partial
from fractions import gcd
def lcm(a,b):
  return a*b//gcd(a,b)
n=int(input())
a=[int(input()) for i in range(n)]
lcmmulti = partial(reduce,lcm)
print(lcmmulti(a))

Buggy submission (verdict: Runtime Error):
import numpy as np
n=int(input())
a=np.ndarray([int(input()) for i in range(n)])
print(np.lcm.reduce(a))
Problem p03645 (python)

Fixed submission:
n,m=map(int,input().split())
tr=[[] for i in range(n+1)]
for i in range(m):
  a,b=map(int,input().split())
  if a!=1 and b!=n:
    continue
  tr[a].append(b)
  tr[b].append(a)
for i in range(2,n):
  if len(tr[i])==2:
    print("POSSIBLE")
    break
else:
  print("IMPOSSIBLE")

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
tr=[[] for i in range(n)]
for i in range(m):
  a,b=map(int,input().split())
  if a!=1 and b!=n:
    continue
  tr[a].append(b)
  tr[b].append(a)
for i in range(n):
  if len(tr[i])==2:
    print("POSSIBLE")
    break
else:
  print("IMPOSSIBLE")

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
tr=[[] for i in range(m)]
for i in range(m):
  a,b=map(int,input().split())
  if a!=1 and b!=n:
    continue
  tr[a].append(b)
  tr[b].append(a)
for i in range(m):
  if len(tr[m])==2:
    print("POSSIBLE")
    break
else:
  print("IMPOSSIBLE")
Problem p03654 (python)

Fixed submission:
import sys
input = sys.stdin.readline
from collections import defaultdict
n = int(input())
abd = [list(map(int,input().split())) for i in range(n-1)]
if n == 2:
  print(abd[0][2])
  exit()
graph = [[] for i in range(n+1)]
deg = [0 for i in range(n+1)]
for a,b,d in abd:
  graph[a].append((b,d))
  graph[b].append((a,d))
  deg[a] += 1
  deg[b] += 1
stack = []
root = 0
dp = [[] for i in range(n+1)]
sm = [1 for i in range(n+1)]
mx = [0 for i in range(n+1)]
for i in range(1,n+1):
  if deg[i] == 1:
    stack.append(i)
  elif root == 0:
    root = i
    deg[i] += 1
ans = 0
flg = 0
while stack:
  x = stack.pop()
  if dp[x]:
    sm[x] += sum(dp[x])
  if sm[x]*2 == n:
    flg = 1
  for y,d in graph[x]:
    if deg[y] > 1:
      dp[y].append(sm[x])
      if sm[x] == n-sm[x]:
        ans += (sm[x]*2-1)*d
      else:
        ans += min(sm[x],n-sm[x])*2*d
      deg[y] -= 1
      if deg[y] == 1:
        stack.append(y)
dmn = 10**18
if not flg:
  for a,b,d in abd:
    for v in (a,b):
      if not mx[v]:
        if dp[v]:
          mx[v] = max(max(dp[v]),n-1-sm[v])
        else:
          mx[v] = n-2
    if min(mx[a],mx[b])*2 < n:
      dmn = min(dmn,d)
  ans -= dmn
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n = int(input())
abd = [list(map(int,input().split())) for i in range(n-1)]
if n == 2:
  print(abd[0][2])
  exit()
graph = [[] for i in range(n+1)]
deg = [0 for i in range(n+1)]
for a,b,d in abd:
  graph[a].append((b,d))
  graph[b].append((a,d))
  deg[a] += 1
  deg[b] += 1
stack = []
root = 0
dp = [1 for i in range(n+1)]
for i in range(1,n+1):
  if deg[i] == 1:
    stack.append(i)
  elif root == 0:
    root = i
    deg[i] += 1
ans = 0
flg = 0
while stack:
  x = stack.pop()
  for y,d in graph[x]:
    if deg[y] > 1:
      dp[y] += dp[x]
      if dp[x] == n-dp[x]:
        flg = 1
        ans += (dp[x]*2-1)*d
      else:
        ans += min(dp[x],n-dp[x])*2*d
      deg[y] -= 1
      if deg[y] == 1:
        stack.append(y)
if not flg:
  print(2/0)
  dmn = min(list(zip(*abd))[2])
  ans -= dmn
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n = int(input())
abd = [list(map(int,input().split())) for i in range(n-1)]
if n == 2:
  print(abd[2])
  exit()
graph = [[] for i in range(n+1)]
deg = [0 for i in range(n+1)]
for a,b,d in abd:
  graph[a].append((b,d))
  graph[b].append((a,d))
  deg[a] += 1
  deg[b] += 1
stack = []
root = 0
dp = [1 for i in range(n+1)]
for i in range(1,n+1):
  if deg[i] == 1:
    stack.append(i)
  elif root == 0:
    root = i
    deg[i] += 1
ans = 0
flg = 0
while stack:
  x = stack.pop()
  for y,d in graph[x]:
    if deg[y] > 1:
      dp[y] += dp[x]
      if dp[x] == n-dp[x]:
        flg = 1
        ans += (dp[x]*2-1)*d
      else:
        ans += min(dp[x],n-dp[x])*2*d
      deg[y] -= 1
      if deg[y] == 1:
        stack.append(y)
if not flg:
  dmn = min(list(zip(*abd))[2])
  ans -= dmn
print(ans)
Problem p03657 (python)

Fixed submission:
a,b = map(int, input().split())
if a%3 and b%3 and (a+b)%3:
  print("Impossible")
else:
  print("Possible")

Buggy submission (verdict: Runtime Error):
a,b = map(int, input())
if a%3 and b%3 and (a+b)%3:
  print("Impossible")
else:
  print("Possible")
Problem p03674 (python)

Fixed submission:
from collections import Counter

mod = 10**9+7
rng = 100002
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
for i in range(1,rng):
  finv.append(pow(fctr[i],mod-2,mod))
def cmb(n,k):
  if n<=0 or k<0 or n<k:
    return 0
  else:
    return fctr[n]*finv[n-k]*finv[k]%mod

n = int(input())
a = list(map(int,input().split()))
same = []
c = Counter(a)
x = c.most_common()[0][0]
for i in range(n+1):
  if a[i] == x:
    same.append(i+1)
y = same[1]-same[0]+1
print(n)
for k in range(2,n+2):
  print((cmb(n+1,k)-cmb(n+1-y,k-1))%mod)

Buggy submission (verdict: Runtime Error):
from collections import Counter

mod = 10**9+7
rng = 100001
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
for i in range(1,rng):
  finv.append(pow(fctr[i],mod-2,mod))
def cmb(n,k):
  if n<=0 or k<0 or n<k:
    return 0
  else:
    return fctr[n]*finv[n-k]*finv[k]%mod

n = int(input())
a = list(map(int,input().split()))
same = []
c = Counter(a)
x = c.most_common()[0][0]
for i in range(n+1):
  if a[i] == x:
    same.append(i+1)
y = same[1]-same[0]+1
print(n)
for k in range(2,n+2):
  print((cmb(n+1,k)-cmb(n+1-y,k-1))%mod)
Problem p03696 (python)

Fixed submission:
from collections import deque
n=int(input())
s = input()
cnt = [0]
ans = deque(list(s))
for i in range(n):
  if s[i]=="(":
    cnt.append(cnt[-1]+1)
  else:
    cnt.append(cnt[-1]-1)
rt = min(cnt)
if rt<0:
  ans.extendleft("("*(-rt))
if cnt[-1]-rt>0:
  ans.extend(")"*(cnt[-1]-rt))
print(*ans,sep="")

Buggy submission (verdict: Runtime Error):
import queue
n=int(input())
s=input()
q=queue.Queue(s)
def br(a):
  for i in range(1,len(a)+1):
    if a[:i].count("(")<a[:i].count(")"):
      return ")"
  if a.count("(")==a.count(")"):
    return "0"
  else:
    return "("
for i in range(1,len(s)+1):
  if s[:i].count("(")<s[:i].count(")"):
    s.append("(")
Problem p03708 (python)

Fixed submission:
a=int(input())
b=int(input())
if a==b:
  print(1)
  exit()
abin=bin(a)[2:]
bbin=bin(b)[2:]
la=len(abin)
lb=len(bbin)
if la==lb:
  while abin[0]==bbin[0]:
    abin=abin[1:]
    bbin=bbin[1:]
  while len(abin)>1 and abin[0]=="0":
    abin=abin[1:]
cbin=bbin[1:]
while cbin and cbin[0]=="0":
  cbin=cbin[1:]
c=2**(len(cbin))-1
a=int(abin,2)
b=int(bbin,2)
lb=len(bbin)
lbnd=2**(lb-1)+c
hbnd=2**(lb-1)+a
if lbnd>=hbnd:
  print(2**lb-a)
else:
  print(2**(lb-1)-a+lbnd-a+1)

Buggy submission (verdict: Runtime Error):
a=int(input())
b=int(input())
if a==b:
  print(1)
  exit()
abin=bin(a)[2:]
bbin=bin(b)[2:]
la=len(abin)
lb=len(bbin)
if la==lb:
  while abin[0]==bbin[0]:
    abin=abin[1:]
    bbin=bbin[1:]
  while abin[0]=="0":
    abin=abin[1:]
cbin=bbin[1:]
while cbin[0]=="0":
  cbin=cbin[1:]
c=2**(len(cbin))-1
a=int(abin,2)
b=int(bbin,2)
lb=len(bbin)
lbnd=2**(lb-1)+c
hbnd=2**(lb-1)+a
if lbnd>=hbnd:
  print(2**lb-a)
else:
  print(2**(lb-1)-a+lbnd-a+1)

Buggy submission (verdict: Runtime Error):
a=int(input())
b=int(input())
if a==b:
  print(1)
  exit()
abin=bin(a)[2:]
bbin=bin(b)[2:]
la=len(abin)
lb=len(bbin)
if la==lb:
  while abin[0]==bbin[0]:
    abin=abin[1:]
    bbin=bbin[1:]
  while abin[0]=="0":
    abin=abin[1:]
cbin=bbin[1:]
while cbin[0]=="0":
  cbin=cbin[1:]
c=2**(len(cbin))
a=int(abin,2)
lbnd=2**(lb-1)+c
hbnd=2**(lb-1)+a
if lbnd>=hbnd:
  print(2**(lb-1)-a)
else:
  print(2**(lb-1)-a+lbnd-a)

Buggy submission (verdict: Runtime Error):
a=int(input())
b=int(input())
if a==b:
  print(1)
  exit()
abin=bin(a)[2:]
bbin=bin(b)[2:]
la=len(abin)
lb=len(bbin)
if la==lb:
  while abin[0]==bbin[0]:
    abin=abin[1:]
    bbin=bbin[1:]
  while abin[0]=="0":
    abin=abin[1:]
cbin=bbin[1:]
while cbin[0]=="0":
  cbin=cbin[1:]
c=2**(len(cbin))
a=int(abin,2)
lbnd=2**(lb-1)+c
hbnd=2**(lb-1)+a
if lbnd>=hbnd:
  print(lbnd-a+1)
else:
  print(2**(lb-1)-a+lbnd-a+1)

Buggy submission (verdict: Runtime Error):
a=int(input())
b=int(input())
if a==b:
  print(1)
  exit()
abin=bin(a)[2:]
bbin=bin(b)[2:]
la=len(abin)
lb=len(bbin)
if la==lb:
  while abin[0]==bbin[0]:
    del abin[0]
    del bbin[0]
  while abin[0]=="0":
    del abin[0]
cbin=bbin[1:]
while cbin[0]=="0":
  del cbin[0]
c=2**(len(cbin))
a=int(abin,2)
lbnd=2**(lb-1)+c
hbnd=2**(lb-1)+a
if lbnd>=hbnd:
  print(lbnd-a+1)
else:
  print(2**(lb-1)-a+lbnd-a+1)
Problem p03722 (python)

Fixed submission:
n,m = map(int,input().split())
vve = [list(map(int,input().split())) for i in range(m)]
graph = [[] for i in range(n+1)]
for a,b,e in vve:
  graph[a].append((b,-e))
score = [10**15 for i in range(n+1)]
score[1] = 0
for j in range(n+1):
  flg = 0
  for i in range(m):
    v1,v2,e = vve[i]
    e = -e
    if score[v1] != 10**15 and score[v2] > score[v1]+e:
      score[v2] = score[v1]+e
      flg = 1
      if j == n-1:
        p = -score[n]
      if j == n:
        q = -score[n]
  if flg == 0:
    break
if flg == 0:
  print(-score[n])
else:
  if p == q:
    print(p)
  else:
    print("inf")

Buggy submission (verdict: Runtime Error):
import scipy
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import bellman_ford

n,m = map(int,input().split())
evv = [list(map(int,input().split())) for i in range(m)]
for i in range(m):
  evv[i][2] = -evv[i][2]
  evv[i][0] -= 1
  evv[i][1] -= 1
v1,v2,edge = zip(*evv)
csr = csr_matrix((edge,(v1,v2)),shape = (n,n))
try:
  print(-int(bellman_ford(csr, indices = 0)[n-1]))
except scipy.sparse.csgraph._shortest_path.NegativeCycleError:
  print("inf")
Problem p03739 (python)

Fixed submission:
# coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
    sm = 0
    ans = 0
    ans += abs(ls[0]-x)
    k = x
    for i in range(1,len(ls)):
        sm += k
        if sm > 0:
            if abs(ls[i]) <= sm or sm < ls[i]:
                ans += abs(ls[i]+sm+1)
                k = -sm-1
            else:
                k = ls[i]
        if sm < 0:
            if abs(ls[i]) <= abs(sm) or sm > ls[i]:
                ans += abs(-sm+1-ls[i])
                k = -sm+1
            else:
                k = ls[i]
    return ans

anstot = []
if a[0] > 0:
    inv = -1
if a[0] < 0:
    inv = 1
if a[0] != 0:
    anstot.append(operate(a,a[0]))
    anstot.append(operate(a,inv))
if a[0] == 0:
    a[0] = 1
    anstot.append(1+operate(a,1))
    a[0] = -1
    anstot.append(1+operate(a,-1))    
print(min(anstot))

Buggy submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
    sm = 0
    ans = 0
    ans += abs(ls[0]-x)
    k = x
    for i in range(1,len(ls)):
        sm += k
        if sm > 0:
            if abs(ls[i]) <= sm or sm < ls[i]:
                ans += abs(ls[i]+sm+1)
                k = -sm-1
            else:
                k = ls[i]
        if sm < 0:
            if abs(ls[i]) <= abs(sm) or sm > ls[i]:
                ans += abs(-sm+1-ls[i])
                k = -sm+1
            else:
                k = ls[i]
    return ans

anstot = []
if a[0] > 0:
    inv = -1
if a[0] < 0:
    inv = 1
if a[0] != 0:
    anstot.append(operate(a,a[0]))
    anstot.append(operate(a,inv))
if a[0] == 0:
    for i in range(n):
        if a[i] == 0:
            a[i] = -1**(i%2)
        if a[i] != 0:
            r = i
            break
    anstot.append(operate(r+a,a[0]))
    for i in range(r):
        a[i] = -1**((1+i)%2)
    anstot.append(operate(r+a,a[0]))    
print(min(anstot))
Problem p03761 (python)

Fixed submission:
n=int(input())
s=[input() for i in range(n)]
l=list("abcdefghijklmnopqrstuvwxyz")
c_tmp=[]
for j in l:
  for i in s:
    c_tmp.append(i.count(j))
  print(j*min(c_tmp),end="")
  c_tmp.clear()

Buggy submission (verdict: Runtime Error):
n=int(input())
s=[list(input()) for in range(n)]
for i in s:
  i.sort()

Buggy submission (verdict: Runtime Error):
n=int(input())
s=[list(input()).sort() for in range(n)]
Problem p03770 (python)

Fixed submission:
from collections import defaultdict

mod = 10**9+7
rng = 200100
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
for i in range(1,rng):
  finv.append(pow(fctr[i],mod-2,mod))
def cmb(n,k):
  if n<0 or k<0:
    return 0
  else:
    return fctr[n]*finv[n-k]*finv[k]%mod

import sys
input = sys.stdin.readline
n,X,Y = map(int,input().split())
cw = [list(map(int,input().split())) for i in range(n)]
cw.sort()
mnls = []
graph = [[] for i in range(n)]
notmn = set()
for i in range(n):
  if i == 0 or cw[i-1][0] != cw[i][0]:
    mnls.append((i,cw[i][0],cw[i][1]))
  else:
    if cw[i][1]+mnls[-1][2] <= X:
      graph[i].append(mnls[-1][0])
      graph[mnls[-1][0]].append(i)
    else:
      notmn.add(i)
mnls.sort(key = lambda x:x[2])
for i,x in enumerate(mnls):
  if i == 0:
    continue
  if x[2]+mnls[0][2] <= Y:
    graph[x[0]].append(mnls[0][0])
    graph[mnls[0][0]].append(x[0])
for i in notmn:
  if mnls[0][1] != cw[i][0]:
    if mnls[0][2]+cw[i][1] <= Y:
      graph[i].append(mnls[0][0])
      graph[mnls[0][0]].append(i)
  else:
    if len(mnls) >= 2 and mnls[1][2]+cw[i][1] <= Y:
      graph[i].append(mnls[1][0])
      graph[mnls[1][0]].append(i)
vis = [0 for i in range(n)]
ans = 1
for i in range(n):
  if vis[i]:
    continue
  vis[i] = 1
  stack = [i]
  color = defaultdict(int)
  while stack:
    x = stack.pop()
    color[cw[x][0]] += 1
    for y in graph[x]:
      if vis[y] == 0:
        vis[y] = 1
        stack.append(y)
  sm = sum(color.values())
  for v in color.values():
    ans = ans*cmb(sm,v)%mod
    sm -= v
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

mod = 10**9+7
rng = 200100
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
for i in range(1,rng):
  finv.append(pow(fctr[i],mod-2,mod))
def cmb(n,k):
  if n<0 or k<0:
    return 0
  else:
    return fctr[n]*finv[n-k]*finv[k]%mod

import sys
input = sys.stdin.readline
n,X,Y = map(int,input().split())
cw = [list(map(int,input().split())) for i in range(n)]
cw.sort()
mnls = []
graph = [[] for i in range(n)]
notmn = set()
for i in range(n):
  if i == 0 or cw[i-1][0] != cw[i][0]:
    mnls.append((i,*cw[i]))
  else:
    if cw[i][1]+mnls[-1][2] <= X:
      graph[i].append(mnls[-1][0])
      graph[mnls[-1][0]].append(i)
    else:
      notmn.add(i)
mnls.sort(key = lambda x:x[2])
for i,x in enumerate(mnls):
  if i == 0:
    continue
  if x[2]+mnls[0][2] <= Y:
    graph[x[0]].append(mnls[0][0])
    graph[mnls[0][0]].append(x[0])
for i in notmn:
  if mnls[0][2]+cw[i][1] <= Y:
    graph[i].append(mnls[0][0])
    graph[mnls[0][0]].append(i)
vis = [0 for i in range(n)]
ans = 1
for i in range(n):
  if vis[i]:
    continue
  vis[i] = 1
  stack = [i]
  color = defaultdict(int)
  while stack:
    x = stack.pop()
    color[cw[x][0]] += 1
    for y in graph[x]:
      if vis[y] == 0:
        vis[y] = 1
        stack.append(y)
  sm = sum(color.values())
  for v in color.values():
    ans = ans*cmb(sm,v)%mod
    sm -= v
print(ans)
Problem p03792 (python)

Fixed submission:
n=int(input())
a=[list(input()) for i in range(n)]
if max([a[i].count("#") for i in range(n)]) ==0:
  print(-1)
  exit()
minus=0
b=list(zip(*a))
for i in range(n):
  if b[i].count("#")>=1:
    minus=max(minus,a[i].count("#"))
  else:
    minus=max(minus,a[i].count("#")-1)
for j in range(n):
  if b[j].count(".")==0:
    minus+=1
print(2*n-minus)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=[list(input()) for i in range(n)]
if max([a[i].count("#") for i in range(n)]) ==0:
  print(-1)
  exit()
minus=0
for i in range(n):
  if a[i].count(".")==0:
    minus=n
    break
  if a[i][i]=="#":
    minus=max(minus,a[i].count("#"))
b=zip(*a)
for j in range(n):
  if b[j].count(".")==0:
    minus+=1
print(2*n-minus)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=[list(input()) for i in range(n)]
if max(a[i].count("#") for i in range(n)) ==0:
  print(-1)
  exit()
minus=0
for i in range(n):
  if a[i].count(".")==0:
    minus=n
    break
  if a[i][i]=="#":
    minus=max(minus,a[i].count("#"))
b=zip(*a)
for j in range(n):
  if b[j].count(".")==0:
    minus+=1
print(2*n-minus)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
if max(a[i].count(".") for i in range(n)) ==0:
  print(-1)
  exit()
minus=0
for i in range(n):
  if a[i].count(".")==0:
    minus=n
    break
  elif a[i][i]=="#":
    minus=max(minus,a[i].count("#"))
b=zip(*a)
for j in range(n):
  if b[j].count(".")==0:
    minus+=1
print(2*n-minus)
Problem p03810 (python)

Fixed submission:
from fractions import gcd
n = int(input())
a = list(map(int,input().split()))
def gcds(ls):
  ret = 0
  for i in ls:
    ret = gcd(ret,i)
  return ret
def flg(ls):
  if (sum(ls)-len(ls))%2:
    ret = 1
  else:
    ret = 2
  return ret
if n%2 == 0:
  if sum(a)%2:
    print("First")
  else:
    print("Second")
else:
  cnt = 0
  while flg(a)%2 == 0:
    odd = 0
    oddidx = -1
    even = 0
    for i,x in enumerate(a):
      if x%2:
        odd += 1
        oddidx = i
      else:
        even += 1
    if odd == 1 and a[oddidx] > 1:
      a[oddidx] -= 1
      g = gcds(a)
      for i in range(n):
        a[i] //= g
      cnt += 1
    else:
      break
  if (flg(a)+cnt)%2 == 1:
    print("First")
  else:
    print("Second")

Buggy submission (verdict: Runtime Error):
from math import gcd
n = int(input())
a = list(map(int,input().split()))
def gcds(ls):
  ret = 0
  for i in ls:
    ret = gcd(ret,i)
  return ret
def flg(ls):
  if (sum(ls)-len(ls))%2:
    ret = 1
  else:
    ret = 2
  return ret
if n%2 == 0:
  if sum(a)%2:
    print("Second")
  else:
    print("First")
else:
  cnt = 0
  while flg(a)%2 == 0:
    odd = 0
    oddidx = -1
    even = 0
    for i,x in enumerate(a):
      if x%2:
        odd += 1
        oddidx = i
      else:
        even += 1
    if odd == 1 and a[oddidx] > 1:
      a[oddidx] -= 1
      g = gcds(a)
      for i in range(n):
        a[i] //= g
      cnt += 1
    else:
      break
  if (flg(a)+cnt)%2 == 1:
    print("First")
  else:
    print("Second")