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

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
N=int(input())

def dfs(u,par):
  col=1
  for v in graph[u]:
    if col==par:
      col+=1
    color_dic[(u,v)]=col
    dfs(v,col)
    col+=1

elist=[]
graph=[[] for _ in range(N+1)]
for _ in range(N-1):
  a,b=map(int,input().split())
  graph[a].append(b)
  elist.append((a,b))
#print(graph)

color_dic={}
dfs(1,-1)
#print(color_dic)

K=len(set(color_dic.values()))
print(K)
for e in elist:
  print(color_dic[e])

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
N=int(input())

def dfs(u,par):
  col=1
  for v in graph[u]:
    if col==par:
      col+=1
    color_dic[(u,v)]=col
    dfs(v,col)
    col+=1

elist=[]
graph=[[] for _ in range(N+1)]
for _ in range(N-1):
  a,b=map(int,input().split())
  graph[a].append(b)
  elist.append((a,b))
#print(graph)

color_dic={}
dfs(1,-1)
#print(color_dic)

K=len(set(color_dic.values()))
print(K)
for e in elist:
  print(color_dic[e])
Problem p02852 (python)

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

seq=0
for s in slist:
  if s==0:
    seq=0
  else:
    seq+=1
    if seq==M:
      print(-1)
      sys.exit(0)

#####segfunc#####
def segfunc(x,y):
  return min(x,y)

#####ide_ele#####
ide_ele=float("inf")

class SegTree:
  """
  init(init_val, ide_ele): 配列init_valで初期化 O(N)
  update(k, x): k番目の値をxに更新 O(logN)
  query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
  """
  def __init__(self, init_val, segfunc, ide_ele):
    """
    init_val: 配列の初期値
    segfunc: 区間にしたい操作
    ide_ele: 単位元
    n: 要素数
    num: n以上の最小の2のべき乗
    tree: セグメント木(1-index)
    """
    n = len(init_val)
    self.segfunc = segfunc
    self.ide_ele = ide_ele
    self.num = 1 << (n - 1).bit_length()
    self.tree = [ide_ele] * 2 * self.num
    # 配列の値を葉にセット
    for i in range(n):
      self.tree[self.num + i] = init_val[i]
    # 構築していく
    for i in range(self.num - 1, 0, -1):
      self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

  def update(self, k, x):
    """
    k番目の値をxに更新
    k: index(0-index)
    x: update value
    """
    k += self.num
    self.tree[k] = x
    while k > 1:
      self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
      k >>= 1

  def query(self, l, r):
    """
    [l, r)のsegfuncしたものを得る
    l: index(0-index)
    r: index(0-index)
    """
    res = self.ide_ele

    l += self.num
    r += self.num
    while l < r:
      if l & 1:
        res = self.segfunc(res, self.tree[l])
        l += 1
      if r & 1:
        res = self.segfunc(res, self.tree[r - 1])
      l >>= 1
      r >>= 1
    return res
  
seg=SegTree([float("inf")]*(N+1), segfunc, ide_ele)
seg.update(N,0)

for i in reversed(range(N)):
  if slist[i]==0:
    left=i+1
    right=min(i+M,N)
    seg.update(i,seg.query(left,right+1)+1)

dist_list=[]
for i in range(N+1):
  dist_list.append(seg.query(i,i+1))
#print(dist_list)

answer_list=[]
i=0
while i<N:
  for j in range(1,M+1):
    if dist_list[i]>dist_list[i+j]:
      answer_list.append(j)
      i+=j
      break
    
print(*answer_list)

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

dp=[[] for _ in range(N+1)]
dp[0]=(0,[])
for i in range(1,N+1):
  if S[i]==0:
    cand=[]
    for j in range(1,M+1):
      if S[i-j]==1:
        continue
      elif i-j<0:
        break
      cand.append((dp[i-j][0]+1,dp[i-j][1]+[j]))
    cand.sort()
    #print(i,cand)
    if cand:
      dp[i]=cand[0]
    
#print(dp)
if dp[-1]:
  print(*dp[-1][1])
else:
  print(-1)
Problem p02885 (python)

Fixed submission:
A,B=map(int,input().split())
print(max(0,A-2*B))

Buggy submission (verdict: Runtime Error):
N=int(input())
L=list(map(int,input().split()))
L.sort()
#print(L)

answer=0
for i in range(N-2):
  index_c=i+2
  for j in range(i+1,N-1):
    for k in range(index_c,N):
      if L[i]+L[j]<=L[k]:
        #print(i,j,k-1,k-j-1)
        answer+=k-j-1
        index_c=k-1
        break
    else:
      #print(i,j,N-1,N-j-1)
      answer+=N-j-1
      index_c=N-1
        
print(answer)
Problem p02957 (python)

Fixed submission:
A,B=map(int,input().split())
if (A+B)%2==0:
  print((A+B)//2)
else:
  print("IMPOSSIBLE")

Buggy submission (verdict: Runtime Error):
K=int(input())
X,Y=map(int,input().split())

ispos_X,ispos_Y=True,True
if X<0:
  X=-X
  ispos_X=False
if Y<0:
  Y=-Y
  ispos_Y=False

if K%2==0 and (X+Y)%2==1:
  print(-1)
else:
  xx,yy=0,0
  vec_list=[]
  if K%2==0:
    while((X-xx)+(Y-yy)>2*K):
      if X-xx>K:
        xx+=K
        vec_list.append((K,0))
      elif Y-yy>K:
        yy+=K
        vec_list.append((0,K))
        
    #print(xx,yy,vec_list)
    diff_x=X-xx
    diff_y=Y-yy
    if diff_x+diff_y==K:
      vec_list.append((diff_x,diff_y))
    else:
      buf=2*K-diff_x-diff_y    
      vec_list.append((diff_x+buf//2,K-diff_x-buf//2))
      vec_list.append((-buf//2,K-buf//2))
      
  else:
    while((X-xx)+(Y-yy)>2*K):
      if X-xx>K:
        xx+=K
        vec_list.append((K,0))
      elif Y-yy>K:
        yy+=K
        vec_list.append((0,K))    
        
    #print(xx,yy,vec_list)
    diff_x=X-xx
    diff_y=Y-yy
    if diff_x+diff_y==K:
      vec_list.append((diff_x,diff_y))
    elif (diff_x+diff_y)%2==0:      
      buf=2*K-diff_x-diff_y
      if diff_x<K:
        vec_list.append((diff_x+buf//2,K-diff_x-buf//2))
        vec_list.append((-buf//2,K-buf//2))
      else:
        vec_list.append((K-diff_y-buf//2,diff_y+buf//2))
        vec_list.append((K-buf//2,-buf//2))
    elif diff_x+diff_y<K:
      xx+=diff_x
      yy+=-K+diff_x
      vec_list.append((diff_x,-K+diff_x))
      
      diff_x=X-xx
      diff_y=Y-yy      
      buf=2*K-diff_x-diff_y
      vec_list.append((diff_x+buf//2,K-diff_x-buf//2))
      vec_list.append((-buf//2,K-buf//2))
    else:
      if diff_x<K:
        xx+=diff_x
        yy+=K-diff_x
        vec_list.append((diff_x,K-diff_x))
      elif diff_y<K:
        xx+=K-diff_y
        yy+=diff_y        
        vec_list.append((K-diff_y,diff_y))

      diff_x=X-xx
      diff_y=Y-yy      
      buf=2*K-diff_x-diff_y
      vec_list.append((diff_x+buf//2,K-diff_x-buf//2))
      vec_list.append((-buf//2,K-buf//2))

  #print(vec_list)
  print(len(vec_list))
  pos_x,pos_y=0,0
  for x,y in vec_list:
    if ispos_X:
      pos_x+=x
    else:
      pos_x-=x
    if ispos_Y:
      pos_y+=y
    else:
      pos_y-=y
      
    print(pos_x,pos_y)
Problem p02962 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
s=list(input())
t=list(input())

tsr=list(t)+list(s)
while len(tsr)<2*len(t)+len(s):
  tsr+=s
#print(tsr)

#Z[i]: 文字列Sと各S[i:]の最長共通接頭辞の長さ
def z_alg(S):
  NN=len(S)
  Z=[NN]+[0]*(NN-1)
  i,j=1,0
  while i<NN:
    while i+j<NN and S[j]==S[i+j]:
      j+=1
    if not j:
      i+=1
      continue
    Z[i]=j
    k=1
    while NN-i>k and k<j-Z[k]:
      Z[i+k]=Z[k]
      k+=1
    i+=k
    j-=k
  return Z

zlist=z_alg(tsr)
#print(zlist)

match_list=[]
for i in range(len(s)):
  match_list.append(zlist[i+len(t)]>=len(t))
#print(match_list)

path=[-1]*len(s)
for i in range(len(s)):
  if match_list[i]:
    path[i]=(i+len(t))%len(s)
#print(path)

dist=[-1]*len(s)
for i in range(len(s)):
  if dist[i]!=-1:
    continue
    
  d=0
  offset=0
  stack=[i]
  ii=i
  while path[ii]!=-1:
    if d>len(s):
      print(-1)
      sys.exit(0)
    elif dist[ii]!=-1:
      offset=dist[ii]
      break    
    ii=path[ii]
    stack.append(ii)
    d+=1
  
  cnt=0
  while stack:
    p=stack.pop()
    dist[p]=offset+cnt
    cnt+=1
    
#print(dist)
print(max(dist))

Buggy submission (verdict: Runtime Error):
import sys
s=list(input())
t=list(input())

tsr=list(t)+list(s)
while len(tsr)<2*len(t)+len(s):
  tsr+=s
#print(tsr)

#Z[i]: 文字列Sと各S[i:]の最長共通接頭辞の長さ
def z_alg(S):
  NN=len(S)
  Z=[NN]+[0]*(NN-1)
  i,j=1,0
  while i<NN:
    while i+j<NN and S[j]==S[i+j]:
      j+=1
    if not j:
      i+=1
      continue
    Z[i]=j
    k=1
    while NN-i>k and k<j-Z[k]:
      Z[i+k]=Z[k]
      k+=1
    i+=k
    j-=k
  return Z

zlist=z_alg(tsr)
#print(zlist)

match_list=[]
for i in range(len(s)):
  match_list.append(zlist[i+len(t)]>=len(t))
#print(match_list)

graph=[[] for _ in range(len(s))]
for i in range(len(s)):
  if match_list[i]:
    graph[i].append((i+len(t))%len(s))
#print(graph)

def dfs(u,i):
  if dist[u]!=-1:
    return dist[u]
  elif not graph[u]:
    dist[u]=0
    return 0
  else:
    if i>len(s):
      print(-1)
      sys.exit(0)
    v=graph[u][0]
    res=dfs(v,i+1)
    dist[u]=1+res
    return 1+res

dist=[-1]*len(s)
for i in range(len(s)):
  if dist[i]==-1:
    dfs(i,0)
    
#print(dist)
print(max(dist))
Problem p02985 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

mod=10**9+7
N,K=map(int,input().split())

tree=[]
for i in range(N+1):
  tree.append([])

for i in range(N-1):
  a,b=map(int,input().split())
  tree[a].append(b)
  tree[b].append(a)
#print(tree)

answer=1
visited=[False]*(N+1)
def dfs(node,depth,cind):
  #print(node,depth,cind)
  global answer
  visited[node]=True
  
  if depth==0:
    answer*=K
    answer%=mod
  elif depth==1:
    answer*=K-1-cind
    answer%=mod
  else:
    answer*=K-2-cind
    answer%=mod
  
  next_nodes=tree[node]
  i=0
  for nn in next_nodes:
    if not visited[nn]:
      dfs(nn,depth+1,i)
      i+=1

node=N//2
dfs(node,0,0)
print(answer)

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

tree=[]
for i in range(N+1):
  tree.append([])

for i in range(N-1):
  a,b=map(int,input().split())
  tree[a].append(b)
  tree[b].append(a)
#print(tree)

answer=1
visited=[False]*(N+1)
def dfs(node,depth,cind):
  #print(node,depth,cind)
  global answer
  visited[node]=True
  
  if depth==0:
    answer*=K
    answer%=mod
  elif depth==1:
    answer*=K-1-cind
    answer%=mod
  else:
    answer*=K-2-cind
    answer%=mod
  
  next_nodes=tree[node]
  i=0
  for nn in next_nodes:
    if not visited[nn]:
      dfs(nn,depth+1,i)
      i+=1

node=1
dfs(node,0,0)
print(answer)
Problem p03062 (python)

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

neg_cnt = 0
abs_min = 999999999999999
for an in alist:
  if an < 0:
    neg_cnt += 1
  if abs_min > abs(an):
    abs_min = abs(an)
 
ans = 0
for i in range(N):
  ans += abs(alist[i])
  
if neg_cnt%2 == 0:
  print(ans)
else:
  print(ans-2*abs_min)

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

ans = 0
for i in range(N):
  ans += abs(alist[i])
  
print(ans)
Problem p03067 (python)

Fixed submission:
a,b,c=map(int,input().split())
if a <= c and c <= b:
  print("Yes")
elif b <= c and c <= a:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
a,b,c=map(int,input().split())
if a <= c and c <= b:
  print(Yes)
elif b <= c and c <= a:
  print(Yes)
else:
  print(No)
Problem p03148 (python)

Fixed submission:
from collections import defaultdict
import heapq
N,K=map(int,input().split())

hq=[]
tset_all=set()
for i in range(N):
  t,d=map(int,input().split())
  heapq.heappush(hq,(-d,t))
  tset_all.add(t)
#print(heapq)

hq_K=[]
dsum=0
tdic=defaultdict(int)
for i in range(K):
  md,t=heapq.heappop(hq)
  heapq.heappush(hq_K,((-md,t)))
  dsum-=md
  tdic[t]+=1  

t0=len(tdic)
max_answer=dsum+(t0**2)
#print(t0,max_answer)

for i in range(t0+1,min(K,len(tset_all))+1):
  loop_flg=True
  while(loop_flg):
    d,t=heapq.heappop(hq_K)
    if tdic[t]>1:
      tdic[t]-=1
      dsum-=d
      
      while(True):
        md2,t2=heapq.heappop(hq)
        if tdic[t2]==0:
          tdic[t2]=1
          dsum-=md2
          loop_flg=False
          break
          
  answer_i=dsum+(i**2)
  #print(i,answer_i)
  max_answer=max(max_answer,answer_i)
  
print(max_answer)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import heapq
N,K=map(int,input().split())

hq=[]
tset_all=set()
for i in range(N):
  t,d=map(int,input().split())
  heapq.heappush(hq,(-d,t))
  tset_all.add(t)
#print(heapq)

hq_K=[]
dsum=0
tdic=defaultdict(int)
for i in range(K):
  md,t=heapq.heappop(hq)
  heapq.heappush(hq_K,((-md,t)))
  dsum-=md
  tdic[t]+=1  

t0=len(tdic)
max_answer=dsum+(t0**2)
#print(t0,max_answer)

for i in range(t0+1,len(tset_all)+1):  
  loop_flg=True
  while(loop_flg):
    d,t=heapq.heappop(hq_K)
    if tdic[t]>1:
      tdic[t]-=1
      dsum-=d
      
      while(True):
        md2,t2=heapq.heappop(hq)
        if tdic[t2]==0:
          dsum-=md2
          loop_flg=False
          break
          
  answer_i=dsum+(i**2)
  #print(i,answer_i)
  max_answer=max(max_answer,answer_i)
  
print(max_answer)
Problem p03151 (python)

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

ablist=[]
for i in range(N):
  ablist.append(alist[i]-blist[i])
  
ablist.sort()
#print(ablist)

if ablist[0] >= 0:
  print("0")
elif sum(ablist) < 0:
  print("-1")
else:
  cnt = 0
  sum_ab = 0
  for ab in ablist:
    if ab<0:
      cnt += 1
      sum_ab += ab
    else:
      break
      
  for ab in reversed(ablist):
    cnt += 1
    sum_ab += ab
    if sum_ab >= 0:
      break
      
  print(cnt)

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

ablist=[]
for i in range(N):
  ablist.append(alist[i]-blist[i])
  
ablist.sort()
#print(ablist)

if ablist[0] >= 0:
  print("0")
elif sum(ablist) < 0:
  print("-1")
else:
  cnt = 0
  sum_ab = 0
  for ab in ablist:
    if ab<0:
      cnt += 1
      sum_ab += ab
    else:
      break
      
  for ab in reversed(ablist):
    cnt += 1
    sum_ab += ab
    if sum_ab >= 0:
      break
      
print(cnt)
Problem p03409 (python)

Fixed submission:
N=int(input())

red_list=[]
for i in range(N):
  a,b=map(int,input().split())
  red_list.append((a,b))
red_list.sort()
  
blue_list=[]
for i in range(N):
  c,d=map(int,input().split())
  blue_list.append((c,d))
blue_list.sort()

#print(red_list)
#print(blue_list)

answer=0
selected_set=set()
for i in range(N):
  c,d=blue_list[i]
  max_y=-1
  maxarg_y=-1
  for j in range(N):
    a,b=red_list[j]
    if c<=a:
      break
    
    if b<d and b>max_y and not j in selected_set:
      max_y=b
      maxarg_y=j
      
  if maxarg_y!=-1:
    answer+=1    
    selected_set.add(maxarg_y)
    #print(red_list[maxarg_y],blue_list[i])
    
print(answer)

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

max_answer=0
for k in range(101):
  answer=0
  k1=k
  k2=100-k

  red_list=[]
  for i in range(N):
    a,b=map(int,input().split())
    red_list.append((k1*a+k2*b,a,b))
  red_list.sort()

  blue_list=[]
  for i in range(N):
    c,d=map(int,input().split())
    blue_list.append((k1*c+k2*d,c,d))
  blue_list.sort()

  #print(red_list)
  #print(blue_list)

  answer=0
  selected_set=set()
  for i in range(N):
    rs,rx,ry=red_list[i]
    for j in range(N):
      if j in selected_set:
        continue
      bs,bx,by=blue_list[j]
      if rx<bx and ry<by:
        #print((rx,ry),(bx,by))
        selected_set.add(j)
        answer+=1
        break
      
  max_answer=max(max_answer,answer)
  
print(max_answer)
Problem p03612 (python)

Fixed submission:
N=int(input())
alist=[0]+list(map(int,input().split()))

answer=0
if alist[N]==N:
  alist[N-1],alist[N]=alist[N],alist[N-1]
  answer+=1

for i in range(1,N+1):
  if alist[i]==i:
    alist[i],alist[i+1]=alist[i+1],alist[i]
    answer+=1
    
print(answer)

Buggy submission (verdict: Runtime Error):
N=int(input())
alist=[0]+list(map(int,input().split()))

answer=0
if alist[N]=N:
  alist[N-1],alist[N]=alist[N],alist[N-1]
  answer+=1

for i in range(1,N+1):
  if alist[i]==i:
    alist[i],alist[i+1]=alist[i+1],alist[i]
    answer+=1
    
print(answer)

Buggy submission (verdict: Runtime Error):
N=int(input())
alist=[0]+list(map(int,input().split()))

if alist[N]=N:
  alist[N-1],alist[N]=alist[N],alist[N-1]
  answer+=1

answer=0
for i in range(1,N+1):
  if alist[i]==i:
    alist[i],alist[i+1]=alist[i+1],alist[i]
    answer+=1
    
print(answer)

Buggy submission (verdict: Runtime Error):
N=int(input())
alist=[0]+list(map(int,input().split()))

answer=0
for i in range(1,N+1):
  if alist[i]==i:
    alist[i],alist[i+1]=alist[i+1],alist[i]
    answer+=1
    
print(answer)
Problem p03660 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from collections import deque
N=int(input())

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

dist_f=[-1]*(N+1)
d=0
queue=[1]
while queue:  
  new_queue=set()
  for u in queue:
    dist_f[u]=d
    for v in graph[u]:
      if dist_f[v]==-1:
        new_queue.add(v)
        
  queue=list(new_queue)
  d+=1
#print(dist_f)

dist_s=[-1]*(N+1)
d=0
queue=[N]
while queue:  
  new_queue=set()
  for u in queue:
    dist_s[u]=d
    for v in graph[u]:
      if dist_s[v]==-1:
        new_queue.add(v)
        
  queue=list(new_queue)
  d+=1
#print(dist_s)

cnt_f,cnt_s=0,0
for i in range(1,N+1):
  if dist_f[i]<=dist_s[i]:
    cnt_f+=1
  else:
    cnt_s+=1

#print(cnt_f,cnt_s)
if cnt_f>cnt_s:
  print("Fennec")
else:
  print("Snuke")

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
from collections import deque
N=int(input())

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

def dfs_init(u,ls):
  global path
  visited[u]=True  
  if u==N:
    path=ls
    return
  
  for v in graph[u]:
    if not visited[v]:
      dfs_init(v,ls+[v])
  
path=[]  
visited=[False]*(N+1)
dfs_init(1,[1])
#print(path)

visited=[False]*(N+1)
set_f=set()
set_s=set()
turn=1
dq=deque(path)
while dq:
  #Fenec
  node_f=dq.popleft()
  visited[node_f]=True
  set_f.add(node_f)
  turn=-1
  
  if dq:
    #Snuke
    node_s=dq.pop()
    visited[node_s]=True
    set_s.add(node_s)
    turn=1
#print(set_f,set_s,visited,turn)

def dfs_fenec(u):
  global cnt_f
  if not visited[u]:
    visited[u]=True
    cnt_f+=1
  for v in graph[u]:
    if not visited[v]:
      dfs_fenec(v)

def dfs_snuke(u):
  global cnt_s
  if not visited[u]:
    visited[u]=True
    cnt_s+=1
  for v in graph[u]:
    if not visited[v]:
      dfs_snuke(v)
      
#fenec
cnt_f=0
for f in set_f:
  dfs_fenec(f)
  
#snuke
cnt_s=0
for s in set_s:
  dfs_snuke(s)
  
#print(cnt_f,cnt_s)
if turn==1:
  if cnt_f>cnt_s:
    print("Fennec")
  else:
    print("Snuke")
elif turn==-1:
  if cnt_f>=cnt_s:
    print("Fennec")
  else:
    print("Snuke")
Problem p03682 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
input=sys.stdin.readline

N=int(input())

xylist,yxlist=[],[]
par_dic={}
for i in range(N):
  x,y=map(int,input().split())
  xylist.append((x,y))
  yxlist.append((y,x))
  par_dic[(x,y)]=(x,y)
xylist.sort()
yxlist.sort()
#print(xylist)

elist=[]
for i in range(1,N):
  x1,y1=xylist[i-1]
  x2,y2=xylist[i]
  elist.append((min(abs(x2-x1),abs(y1-y2)),(x1,y1),(x2,y2)))
  y1,x1=yxlist[i-1]
  y2,x2=yxlist[i]
  elist.append((min(abs(x2-x1),abs(y1-y2)),(x1,y1),(x2,y2)))
#print(hq)
elist.sort(reverse=True)

def get_par(x):
  if x == par_dic[x]:
    return x
  else:
    par_dic[x] = get_par(par_dic[x])
    return par_dic[x]
def merge(x,y):
  par_x = get_par(x)
  par_y = get_par(y)
  if par_x != par_y:
    par_dic[par_y] = par_x
def is_same(x,y):
  return get_par(x) == get_par(y)

answer=0
while elist:
  d,p1,p2=elist.pop()
  #print(d,p1,p2)
  if not is_same(p1,p2):
    merge(p1,p2)
    answer+=d
  
print(answer)

Buggy submission (verdict: Runtime Error):
import heapq
import sys
sys.setrecursionlimit(10**6)

N=int(input())

xylist,yxlist=[],[]
par_list={}
for i in range(N):
  x,y=map(int,input().split())
  xylist.append((x,y))
  yxlist.append((y,x))
  par_list[(x,y)]=(x,y)
xylist.sort()
yxlist.sort()
#print(xylist)

hq=[]
for i in range(1,N):
  x1,y1=xylist[i-1]
  x2,y2=xylist[i]
  heapq.heappush(hq,(min(abs(x2-x1),abs(y1-y2)),(x1,y1),(x2,y2)))
for i in range(1,N):
  y1,x1=yxlist[i-1]
  y2,x2=yxlist[i]
  heapq.heappush(hq,(min(abs(x2-x1),abs(y1-y2)),(x1,y1),(x2,y2)))
#print(hq)

def get_par(x):
  if x == par_list[x]:
    return x
  else:
    par_list[x] = get_par(par_list[x])
    return par_list[x]
def merge(x,y):
  par_x = get_par(x)
  par_y = get_par(y)
  if par_x != par_y:
    par_list[par_y] = par_x
def is_same(x,y):
  return get_par(x) == get_par(y)

answer=0
cnt=0
while cnt<N-1:
  d,p1,p2=heapq.heappop(hq)
  #print(d,p1,p2)
  if not is_same(p1,p2):
    #print(p1,p2)
    merge(p1,p2)
    answer+=d
    cnt+=1
  
print(answer)
Problem p03798 (python)

Fixed submission:
N=int(input())
slist=[x=="o" for x in input()]
#print(slist)

answer_list=[0]*N

ok=False
for p1,p2 in [(1,1),(1,-1),(-1,1),(-1,-1)]:
  answer_list[0]=p1
  answer_list[1]=p2
  
  for i in range(2,N):
    if answer_list[i-1]==1:
      if slist[i-1]:
        answer_list[i]=answer_list[i-2]
      else:
        answer_list[i]=-answer_list[i-2]
    else:
      if slist[i-1]:
        answer_list[i]=-answer_list[i-2]
      else:
        answer_list[i]=answer_list[i-2]
        
  contra=False
  for i in range(N):
    l=(i-1)%N
    r=(i+1)%N
    if answer_list[i]==1:
      if slist[i]:
        if answer_list[l]!=answer_list[r]:
          contra=True
      else:
        if answer_list[l]==answer_list[r]:
          contra=True
    else:
      if slist[i]:
        if answer_list[l]==answer_list[r]:          
          contra=True
      else:
        if answer_list[l]!=answer_list[r]:
          contra=True

  if not contra:
    ok=True
    break
    
if ok:
  sw_list=[]
  for ans in answer_list:
    if ans==1:
      sw_list.append("S")
    else:
      sw_list.append("W")
  print("".join(sw_list))
else:
  print(-1)

Buggy submission (verdict: Runtime Error):
import sys
N=int(input())
slist=[x=="o" for x in input()]
#print(slist)

answer_list=[0]*(N+1)

if N==3:
  if slist.count(True)==3:
    print("SSS")
  elif slist.count(False)==3:
    print("WWW")
  else:
    print(-1)
  sys.exit(0)

ok=False
for p1,p2 in [(1,1),(1,-1),(-1,1),(-1,-1)]:
  answer_list[1]=p1
  answer_list[2]=p2
  if slist[1]:
    answer_list[3]=answer_list[1]
  else:
    answer_list[3]=-answer_list[1]
  for i in range(3,N+1):
    l=i-1
    r=i%N+1
    si=slist[i-1]

    if answer_list[i]==1:
      if si:
        if answer_list[l]==1:
          answer_list[r]=1
        else:
          answer_list[r]=-1
      else:
        if answer_list[l]==1:
          answer_list[r]=-1
        else:
          answer_list[r]=1
    elif answer_list[i]==-1:
      if si:
        if answer_list[l]==1:
          answer_list[r]=-1
        else:
          answer_list[r]=1
      else:
        if answer_list[l]==-1:
          answer_list[r]=1
        else:
          answer_list[r]=-1
    
  contra=False
  if slist[0]:
    if answer_list[1]==1 and answer_list[2]!=answer_list[N]:
      contra=True
    if answer_list[1]==-1 and answer_list[2]==answer_list[N]:
      contra=True
  elif not slist[0]:
    if anser_list[1]==1 and answer_list[2]==answer_list[N]:
      contra=True
    if anser_list[1]==-1 and answer_list[2]!=answer_list[N]:
      contra=True

  if not contra:
    ok=True
    break
      
if ok:
  #print(answer_list[1:])
  sw_list=[]
  for i in range(1,N+1):
    if answer_list[i]==1:
      sw_list.append("S")
    else:
      sw_list.append("W")
  print("".join(sw_list))  
else:
  print(-1)
Problem p04014 (python)

Fixed submission:
import sys
n=int(input())
s=int(input())

if n==s:
  print(n+1)
  sys.exit(0)

def f(b,n):
  ret=0
  while n:
    ret+=n%b
    n//=b
  return ret

for b in range(2,int(n**0.5)+1):
#for b in range(2,n+2):
  ss=f(b,n)
  if ss==s:
    print(b)
    sys.exit(0)

#n=pb+q
for p in range(int((n-1)**0.5),0,-1):
  q=s-p
  b=(n-q)//p
  if q<0 or b<=q or b<2 or (n-q)%p!=0:
    continue
    
  #print(b,p,q)
  ss=f(b,n)
  if ss==s:
    print(b)
    sys.exit(0)
    
print(-1)

Buggy submission (verdict: Runtime Error):
import sys
n=int(input())
s=int(input())

if n==s:
  print(n+1)
  sys.exit(0)

def f(b,n):
  ret=0
  while n:
    ret+=n%b
    n//=b
  return ret

for b in range(2,int(n**0.5)+1):
  ss=f(b,n)
  if ss==s:
    print(b)
    sys.exit(0)

#n=pb+q
for p in range(1,int(n**0.5)+1):
  q=s-p
  b=(n-q)//p
  if q<0 or (n-q)%p!=0:
    continue
    
  ss=f(b,n)
  if ss==s:
    print(b)
    sys.exit(0)
    
print(-1)