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

Fixed submission:
def Warshall_Floyd(edges,N):
  for k in range(N):
    for i in range(N):
      for j in range(N):
        edges[i][j]=min(edges[i][j],edges[i][k]+edges[k][j])
  return edges

n=int(input())
arr=[list(map(int,input().split())) for _ in range(n)]
edge=[]
for i in range(n):
  for j in range(n):
    if i>=j:
      continue
    edge.append((arr[i][j],(i,j)))
edge=sorted(edge,key=lambda x:x[0])
g=[[float('inf')]*n for _ in range(n)]
for i in range(n):
  for j in range(n):
    g[i][j]=arr[i][j]
d=Warshall_Floyd(g,n)
ans=0
for w,(a,b) in edge:
  if d[a][b]<w:
    print(-1)
    break
  else:
    tmp=10**10
    for k in range(n):
      if k==a or k==b:
        continue
      tmp=min(tmp,d[a][k]+d[k][b])
    if w==tmp:
      continue
    else:
      ans+=w
else:
  print(ans)

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

class UnionFind():
  def __init__(self,n):
    self.n=n
    self.root=[-1]*(n+1)
    self.rank=[0]*(n+1)
  def FindRoot(self,x):
    if self.root[x]<0:
      return x
    else:
      self.root[x]=self.FindRoot(self.root[x])
      return self.root[x]
  def Unite(self,x,y):
    x=self.FindRoot(x)
    y=self.FindRoot(y)
    if x==y:
      return
    else:
      if self.rank[x]>self.rank[y]:
        self.root[x]+=self.root[y]
        self.root[y]=x
      elif self.rank[x]<=self.rank[y]:
        self.root[y]+=self.root[x]
        self.root[x]=y
        if self.rank[x]==self.rank[y]:
          self.rank[y]+=1
  def isSameGroup(self,x,y):
    return self.FindRoot(x)==self.FindRoot(y)
  def Count(self,x):
    return -self.root[self.FindRoot(x)]

n=int(input())
arr=[list(map(int,input().split())) for _ in range(n)]
edge=[]
for i in range(n):
  for j in range(n):
    if i>=j:
      continue
    edge.append((arr[i][j],(i,j)))
edge=sorted(edge,key=lambda x:x[0])
g=[[float('inf')]*n for _ in range(n)]
ans=0
cnt=0
i=0
t=UnionFind(n)
checked=[0]*((n*(n-1))//2)
while cnt<n-1:
  w,(a,b)=edge[i]
  if t.isSameGroup(a,b)==False:
    g[a][b]=w
    g[b][a]=w
    checked[i]=1
    ans+=w
    t.Unite(a,b)
    cnt+=1
  i+=1
for i in range((n*(n-1))//2):
  if checked[i]==1:
    continue
  w,(a,b)=edge[i]
  cost=int(dijkstra(g,indices=a)[b])
  print(w,a,b,cost)
  if w==cost:
    continue
  elif w<cost:
    g[a][b]=w
    g[b][a]=w
    ans+=w
  elif w>cost:
    print(-1)
    break
else:
  print(ans)

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

n=int(input())
arr=[list(map(int,input().split())) for _ in range(n)]
edge=[]
for i in range(n):
  for j in range(n):
    if i>=j:
      continue
    edge.append((arr[i][j],(i,j)))
edge=sorted(edge,key=lambda x:x[0])
g=[[float('inf')]*n for _ in range(n)]
ans=0
for i in range(n-1):
  w,(a,b)=edge[i]
  g[a][b]=w
  g[b][a]=w
  ans+=w
for i in range(n-1,(n*(n-1))//2):
  w,(a,b)=edge[i]
  cost=int(dijkstra(g,indices=a)[b])
  if w==cost:
    continue
  elif w<cost:
    g[a][b]=w
    g[b][a]=w
    ans+=w
  elif w>cost:
    print(-1)
    break
else:
  print(ans)
Problem p03625 (python)

Fixed submission:
n=int(input())
arr=list(map(int,input().split()))+[0]
arr=sorted(arr, reverse=True)
ans1=0
ans2=0
ans3=0
count=1
for i in range(n):
  if arr[i]!=arr[i+1]:
    if count>=2:
      if ans1==0:
        ans1=arr[i]
      elif ans2==0:
        ans2=arr[i]      
      if count>=4:
        if ans3==0:
          ans3=arr[i]
    count=1
  else:
    count+=1
print(max(ans1*ans2,ans3**2))

Buggy submission (verdict: Runtime Error):
n=int(input())
arr=list(map(int,input().split()))+[0]
arr=sorted(arr, reverse=True
ans1=0
ans2=0
count=1
for i in range(n):
  if arr[i]!=arr[i+1]:
    if count>=2:
      if ans1==0:
        ans1=arr[i]
      else:
        ans2=arr[i]
    count=1
  else:
    count+=1
print(ans1*ans2)
Problem p03632 (python)

Fixed submission:
a,b,c,d=map(int,input().split())
if a<=c<=b:
  if b<d:
    print(b-c)
  else:
    print(d-c)
elif c<=a<=d:
  if b<d:
    print(b-a)
  else:
    print(d-a)
else:
  print(0)

Buggy submission (verdict: Runtime Error):
a,b,c,d=map(int,input())
if a<=c<=b:
  if b<d:
    print(b-c)
  else:
    print(d-c)
elif c<=a<=d:
  if b<d:
    print(b-a)
  else:
    print(d-a)
else:
  print(0)

Buggy submission (verdict: Runtime Error):
a,b,c,d=map(int,input())
if a<=c<=b:
  if b<d:
    print(b-c)
  else:
    print(d-c):
elif c<=a<=d:
  if b<d:
    print(b-a)
  else:
    print(d-a):  
else:
  print(0)
Problem p03636 (python)

Fixed submission:
s=input()
print(s[0]+str(len(s)-2)+s[-1])

Buggy submission (verdict: Runtime Error):
s=input()
print(s[0]+str(len(s)-2)+s[-1]
Problem p03644 (python)

Fixed submission:
n=int(input())
if n<2**1:
  print(1)
elif n<2**2:
  print(2)
elif n<2**3:
  print(4)
elif n<2**4:
  print(8)
elif n<2**5:
  print(16)
elif n<2**6:
  print(32)
elif n<2**7:
  print(64)

Buggy submission (verdict: Runtime Error):
n=int(input())
elif n<2**1:
  print(1)
elif n<2**2:
  print(2)
elif n<2**3:
  print(4)
elif n<2**4:
  print(8)
elif n<2**5:
  print(16)
elif n<2**6:
  print(32)
elif n<2**7:
  print(64)
Problem p03645 (python)

Fixed submission:
n,m=map(int,input().split())
g=[[] for _ in range(n+1)]
for i in range(m):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
flag=False
for i in g[1]:
  for j in g[i]:
    if j==n:
      flag=True
if flag==True:
  print('POSSIBLE')
else:
  print('IMPOSSIBLE')

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
g=[[] for _ in range(n+1)]
for i in range(m):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
flag=False
for i in g[1]:
  for j in i:
    if j==n:
      flag=True
if flag==True:
  print('POSSIBLE')
else:
  print('IMPOSSIBLE')
Problem p03673 (python)

Fixed submission:
import collections
n=int(input())
arr=list(map(int,input().split()))
q=collections.deque()
for i in range(n):
  if i%2==0:
    q.append(arr[i])
  else:
    q.appendleft(arr[i])
if n%2==1:
  q=list(q)[::-1]
print(' '.join(map(str,q)))

Buggy submission (verdict: Runtime Error):
import collections
n=int(input())
arr=list(map(int,input()))
q=collections.deque()
for i in range(n):
  if i%2==0:
    q.append(arr[i])
  else2
  q.appendleft(arr[i])
if i%2==1:
  q=list(q)[::-1]
print(' '.join(map(str,q)))
Problem p03681 (python)

Fixed submission:
n,m=map(int,input().split())
if abs(n-m)>=2:
  ans=0
elif abs(n-m)==1:
  ans=1
  for i in range(1,n+1):
    ans=(ans*i)%(10**9+7)
  for j in range(1,m+1):
    ans=(ans*j)%(10**9+7)
else:
  ans=1
  for i in range(1,n+1):
    ans=(ans*i)%(10**9+7)
  ans=(2*ans**2)%(10**9+7)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
if abs(n-m)>=2
  ans=0
elif abs(n-m)==1:
  ans=1
  for i in range(1,n+1):
    ans=(ans*i)%(10**9+7)
  for j in range(1,m+1):
    ans=(ans*j)%(10**9+7)
else:
  ans=1
  for i in range(1,n+1):
    ans=(ans*i)%(10**9+7)
  ans=(ans**2)%(10**9+7)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
if abs(n-m)>=2
  ans=0
elif abs(n-m)==1:
  ans=1
  for i in range(1,n+1):
    ans=(ans*i)%(10**9+7)
  for j in range(1,m+1):
    ans=(ans*i)%(10**9+7)
else:
  ans=1
  for i in range(1,n+1):
    ans=(ans*i)%(10**9+7)
  ans=(ans**2)%(10**9+7)
print(ans)
Problem p03687 (python)

Fixed submission:
check='abcdefghijklmnopqrstuvwxyz'
s=input()
l=len(s)
pos=[[0] for _ in range(26)]
for i in range(l):
  for j in range(26):
    if s[i]==check[j]:
      pos[j].append(i+1)
ans=10**9
for i in range(26):
  tmp=0
  for j in range(len(pos[i])-1):
    tmp=max(tmp,pos[i][j+1]-pos[i][j]-1)
  tmp=max(tmp,l-pos[i][-1])
  ans=min(ans,tmp)
print(ans)

Buggy submission (verdict: Runtime Error):
check='abcdefghijklmnopqrstuvwxyz'
s=input()
l=len(s)
pos=[0]*26
for i in range(l):
  for j in range(26):
    if s[i]==check[j]:
      pos[j].append(i)
ans=10**9:
  for i in range(26):
    tmp=0
    for j in range(len(pos[i])-1):
      tmp=max(tmp,pos[i][j+1]-pos[i][j])
    tmp=max(tmp,(l-1)-pos[i][-1])
    ans=min(ans,tmp)
print(ans)
Problem p03722 (python)

Fixed submission:
#https://qiita.com/chun1182/items/ddf2b6cba932b2bb0d4e
def BF(p,n,s):
    inf=float("inf")
    d=[inf for i in range(n)]
    d[s-1]=0
    for i in range(n+1):
        for e in p:
            if e[0]!=inf and d[e[1]-1]>d[e[0]-1]+e[2]:
                d[e[1]-1] = d[e[0]-1] + e[2]
        if i==n-1:t=d[-1]
        if i==n and t!=d[-1]:
            return [0,'inf']
    return list(map(lambda x:-x, d))

n,m=map(int,input().split())
g=[]
for _ in range(m):
  a,b,c=map(int,input().split())
  g.append([a,b,-c])
print(BF(g,n,1)[-1])

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

n,m=map(int,input().split())
g=[[float('inf')]*(n+1) for _ in range(n+1)]
for _ in range(m):
  a,b,c=map(int,input().split())
  g[a][b]=-c
try:
  score=int(bellman_ford(g)[1][n])
except:
  print('inf')
else:
  print(-score)
Problem p03731 (python)

Fixed submission:
n,t=map(int,input().split())
arr=list(map(int,input().split()))
start=arr[0]
ans=0
for i in range(1,n):
  tmp=arr[i]
  if tmp-start<t:
    ans+=tmp-start
  else:
    ans+=t
  start=tmp
print(ans+t)

Buggy submission (verdict: Runtime Error):
n,t=map(int,input().split())
arr=list(map(int,input()))
start=arr[0]
ans=0
for i in range(1,n):
  tmp=arr[i]
  if tmp-start<t:
    ans+=tmp-start
  else:
    ans+=t
  start=tmp
print(ans+t)

Buggy submission (verdict: Runtime Error):
n,t=map(int,input().split())
arr=list(map(int,input()))
s=arr[0]
ans=0
tmp=0
for i in range(1,n):
  if arr[i]-s<=t:
    tmp=arr[i]-s
  else:
    ans+=tmp+t
    tmp=0
ans+=tmp+t
print(ans)
Problem p03778 (python)

Fixed submission:
w,a,b=map(int,input().split())
if a<=b<=(a+w) or b<=(a+w)<=(b+w):
  print(0)
elif (a+w)<b:
  print(b-(a+w))
else:
  print(abs(a-(b+w)))

Buggy submission (verdict: Runtime Error):
w,a,b=map(int,input().split())
if a<=b<=(a+w) or b<=(a+w)<=(b+w):
  print(0)
elif (a+w)<b:
  print(b-(a+w))
else:
  print(abs(a-(b+w))
Problem p03808 (python)

Fixed submission:
n=int(input())
arr=list(map(int,input().split()))
if n==1:
  print('YES')
else:
  sum1=sum(arr)
  sum2=(n*(n+1))//2
  moves=sum1//sum2
  cnt=0
  if sum1%sum2!=0:
    print('NO')
  else:
    flag=True
    arr2=[]
    for i in range(n-1):
      tmp=arr[i]+moves
      if arr[i+1]==tmp:
        continue
      else:
        diff=tmp-arr[i+1]
        if diff%n!=0:
          flag=False
        else:
          arr2.append(n-i)
          cnt+=diff//n
          tmp=arr[i+1]-diff//n
          if tmp>n*(moves-diff//n):
            flag=False
    if flag==False:
      print('NO')
    else:
      if len(arr2)==moves:
        if arr[0]==sum(arr2):
          print('YES')
        else:
          print('NO')
      elif len(arr2)>moves:
        print('NO')
      else:
        if 1*(moves-len(arr2))<=arr[0]-sum(arr2)<=n*(moves-len(arr2)):
          print('YES')
        else:
          print('NO')

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

n=int(input())
arr=list(map(int,input().split()))
sum1=sum(arr)
sum2=(n*(n+1))//2
cnt=sum1//sum2
if sum1%sum2!=0:
  print('NO')
else:
  s=set()
  l=n//(gcd(n,cnt))
  for val in arr:
    s.add(val%n)
  if len(s)==l:
    print('YES')
  else:
    print('NO')
Problem p03827 (python)

Fixed submission:
n=int(input())
s=input()
res=0
ans=0
for i in range(n):
  if s[i]=='I':
    tmp=res+1
  else:
    tmp=res-1
  if tmp>ans:
    ans=tmp
  res=tmp
print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
ans=0
for i in range(n):
  if s[i]='I':
    tmp=ans+1
  else:
    tmp=ans-1
  if tmp>ans:
    ans=tmp
print(ans)
Problem p03836 (python)

Fixed submission:
sx,sy,tx,ty=map(int,input().split())
w=tx-sx
h=ty-sy
ans=''
ans+='U'*h
ans+='R'*w
ans+='D'*h
ans+='L'*w
ans+='L'
ans+='U'*(h+1)
ans+='R'*(w+1)
ans+='D'
ans+='R'
ans+='D'*(h+1)
ans+='L'*(w+1)
ans+='U'
print(ans)

Buggy submission (verdict: Runtime Error):
sx,sy,tx,ty=map(int,input().split())
w=tx-sx
h=ty-xy
ans=''
ans+='R'*w
ans+='U'*h
ans+='L'*w
ans+='D'*h
ans+='D'
ans+='R'*(h+1)
ans+='U'*(w+1)
ans+='L'
ans+='U'
ans+='L'*(h+1)
ans+='D'*(w+1)
ans+='R'
print(ans)
Problem p03844 (python)

Fixed submission:
a,b,c=map(str,input().split())
if b=='+':
  print(int(a)+int(c))
else:
  print(int(a)-int(c))

Buggy submission (verdict: Runtime Error):
a,b,c=map(str,input().split())
if b=='+':
  print(int(a)+int(b))
else:
  print(int(b)-int(b))
Problem p03846 (python)

Fixed submission:
n=int(input())
arr=list(map(int,input().split()))
if n%2==1:
  cnt=[0]*(n//2+1)
  for i in range(n):
    cnt[arr[i]//2]+=1
  if cnt[0]==1 and cnt.count(2)==n//2:
    print((2**(n//2))%(10**9+7))
  else:
    print(0)
else:
  cnt=[0]*(n//2)
  for i in range(n):
    cnt[(arr[i]-1)//2]+=1
  if cnt.count(2)==n//2:
    print((2**(n//2))%(10**9+7))
  else:
    print(0)

Buggy submission (verdict: Runtime Error):
n=int(input())
arr=list(map(int,input().split()))
if n%2==0:
  cnt=[0]*(n//2+1)
  for i in range(n):
    cnt[arr[i]//2]+=1
  if cnt[0]==1 and cnt.count(2)==n//2:
    print((2**(n//2))%(10**9+7))
  else:
    print(-1)
else:
  cnt=[0]*n//2
  for i in range(n):
    cnt[(arr[i]-1)//2]+=1
  if cnt.count(2)==n//2:
    print((2**(n//2))%(10**9+7))
  else:
    print(-1)
Problem p03862 (python)

Fixed submission:
n,k=map(int,input().split())
arr=list(map(int,input().split()))
ans=0
for i in range(1,n):
  if arr[i]+arr[i-1]<=k:
    continue
  elif arr[i-1]<=k<arr[i]+arr[i-1]:
    ans+=arr[i]+arr[i-1]-k
    arr[i]=k-arr[i-1]
  else:
    ans+=arr[i]
    arr[i]=0
    ans+=arr[i-1]-k
    arr[i-1]=k
print(ans)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input())
arr=list(map(int,input().split()))
ans=0
for i in range(1,n):
  if arr[i]+arr[i-1]<=k:
    continue
  elif arr[i-1]<=k<arr[i]+arr[i-1]:
    ans+=arr[i]+arr[i-1]-k
    arr[i]=k-arr[i-1]
  else:
    ans+=arr[i]
    arr[i]=0
    ans+=arr[i-1]-k
    arr[i-1]=k
print(ans)
Problem p03878 (python)

Fixed submission:
n=int(input())
arr=[(int(input()),i//n) for i in range(2*n)]
arr=sorted(arr,key=lambda x:x[0])
mod=10**9+7
ans=1
cnta=0
cntb=0
for pos,flag in arr:
  if flag==0:
    if cntb==0:
      cnta+=1
    else:
      ans*=cntb
      ans%=mod
      cntb-=1
  else:
    if cnta==0:
      cntb+=1
    else:
      ans*=cnta
      ans%=mod
      cnta-=1
print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
arr=[(int(input()),i//n) for i in range(2*n)]
arr=sorted(arr,key=lambda x:x[0])
mod=10**9+7
ans=1
cnta=0
cntb=0
for pos,flag in arr:
  if flag==0:
    if cntb==0:
      cnta+=1
    else:
      cntb-=1
  else:
    if cnta>=1:
      ans*=cnt
      ans%=mod
      cnta-=1
    else:
      cntb+=1
print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
arr=[(int(input()),i//n) for i in range(2*n)]
arr=sorted(arr,key=lambda x:x[0])
mod=10**9+7
ans=1
cnta=0
cntb=0
for pos,flag in arr:
  if flag==0:
    if cntb==0:
      cnta+=1
    else:
      cntb-=1
  else:
    if cnta>=1:
      ans*=cnt
      ans%=mod
      cnt-=1
    else:
      cntb+=1
print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
arr=[(int(input())+i//n) for i in range(2*n)]
arr=sorted(arr,key=lambda x:x[0])
mod=10**9+7
ans=1
cnta=0
cntb=0
for pos,flag in arr:
  if flag==0:
    if cntb==0:
      cnta+=1
    else:
      cntb-=1
  else:
    if cnta>=1:
      ans*=cnt
      ans%=mod
      cnt-=1
    else:
      cntb+=1
print(ans)
Problem p03909 (python)

Fixed submission:
h,w=map(int,input().split())
disp=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
for i in range(h):
  arr=list(map(str,input().split()))
  for j in range(w):
    if arr[j]=='snuke':
      print(disp[j]+str(i+1))

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
disp=['A','B','C','D','E','F','G','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
for i in range(h):
  arr=list(map(str,input.split()))
  for j in range(w):
    if arr[j]=='snuke':
      print(disp[j]+str(i))
Problem p03919 (python)

Fixed submission:
h,w=map(int,input().split())
board=[list(map(str,input().split())) for _ in range(h)]
pos="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(h):
  for j in range(w):
    if board[i][j]=="snuke":
      print(pos[j]+str(i+1))
      exit()

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
board=[list(map(int,input().split())) for _ in range(h)]
pos="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(h):
  for j in range(w):
    if board[i][j]=="snuke":
      print(pos[j]+str(i+1))
      exit()
Problem p03923 (python)

Fixed submission:
n,a=map(int,input().split())
ans=10**18
for k in range(41):
  l=0
  r=n
  while r-l!=1:
    m=(r+l)//2
    if m**(k+1)>=n:
      r=m
    else:
      l=m
  for m in range(k+2):
    if (l**m)*(r**((k+1)-m))>=n:
      ans=min(ans,k*a+l*m+r*((k+1)-m))
print(ans)

Buggy submission (verdict: Runtime Error):
n,a=map(int,input().split())
costs=[10**18]*(n+1)
costs[1]=0
times=[0]*(n+1)
for i in range(1,n+1):
  times[i]=costs[i]+(n+i-1)//i
  for j in range(2,n//i+1):
    costs[i*j]=min(costs[i*j],costs[i]+j+a)
print(min(times[1:]))
Problem p03962 (python)

Fixed submission:
a,b,c=map(int,input().split())
dic={}
dic[a]=1
dic[b]=1
dic[c]=1
print(len(list(dic)))

Buggy submission (verdict: Runtime Error):
a,b,c=map(int,input())
dic={}
dic[a]=1
dic[b]=1
dic[c]=1
print(len(list(dic)))
Problem p04013 (python)

Fixed submission:
n,a=map(int,input().split())
arr=list(map(int,input().split()))
l=n*max(a,max(arr))
dp=[[[0]*(l+1) for _ in range(n+1)] for _ in range(n+1)]
dp[0][0][0]=1
for i in range(1,n+1):
  xi=arr[i-1]
  for j in range(0,i+1):
    for k in range(0,l+1):
      if k<xi:
        dp[i][j][k]=dp[i-1][j][k]
      elif j>=1:
        dp[i][j][k]=dp[i-1][j][k]+dp[i-1][j-1][k-xi]
ans=0
for i in range(1,n+1):
  ans+=dp[n][i][i*a]
print(ans)

Buggy submission (verdict: Runtime Error):
n,a=map(int,input().split())
arr=list(map(int,input().split()))
l=sum(arr)
dp=[[[0]*(l+1) for _ in range(n+1)] for _ in range(n+1)]
dp[0][0][0]=1
for i in range(1,n+1):
  xi=arr[i-1]
  for j in range(0,i+1):
    for k in range(0,l+1):
      if k<xi:
        dp[i][j][k]=dp[i-1][j][k]
      elif j>=1:
        dp[i][j][k]=dp[i-1][j][k]+dp[i-1][j-1][k-xi]
ans=0
for i in range(1,n+1):
  ans+=dp[n][i][i*a]
print(ans)
Problem p04014 (python)

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

def f(b,n):
  if b>n:
    return n
  else:
    return f(b,n//b)+n%b

n=int(input())
s=int(input())
if s>n:
  print(-1)
elif s==n:
  print(n+1)
else:
  ans=10**18
  for i in range(2,int(n**0.5)+1):
    if f(i,n)==s:
      ans=i
      break
  if ans!=10**18:
    print(ans)
  else:
    for i in range(1,int(n**0.5)+1):
      tb=(n-s)//i+1
      if tb==1:
        continue
      if f(tb,n)==s:
        ans=min(ans,tb)
    if ans!=10**18:
      print(ans)
    else:
      print(-1)

Buggy submission (verdict: Runtime Error):
def f(b,n):
  if b>n:
    return n
  else:
    return f(b,n//b)+n%b

n=int(input())
s=int(input())
if s>n:
  print(-1)
elif s==n:
  print(n+1)
else:
  ans=0
  for i in range(2,int(n**0.5)+1):
    if f(i,n)==s:
      ans=i
      break
  if ans!=0:
    print(ans)
  else:
    for i in range(1,int(n**0.5)+1):
      tb=(n-s)//i+1
      if f(tb,n)==s:
        ans=tb
        break
    if ans!=0:
      print(ans)
    else:
      print(-1)

Buggy submission (verdict: Runtime Error):
def f(b,n):
  if b>n:
    return n
  else:
    return f(b,n//b)+n%b

n=int(input())
s=int(input())
if s==n:
  print(n+1)
else:
  ans=0
  for i in range(2,int(n**0.5)+1):
    if f(i,n)==s:
      ans=i
      break
  if ans!=0:
    print(ans)
  else:
    for i in range(1,int(n**0.5)+1):
      tb=(n-s)//i+1
      if f(tb,n)==s:
        ans=tb
        break
    if ans!=0:
      print(ans)
    else:
      print(-1)
Problem p04019 (python)

Fixed submission:
s=input()
ans=[0]*4
check='NWSE'
for i in range(len(s)):
  for j in range(len(check)):
    if s[i]==check[j]:
      ans[j]+=1
if ans[0]!=0 and ans[2]!=0 and ans[1]!=0 and ans[3]!=0:
  print('Yes')
elif ans[0]!=0 and ans[2]!=0 and ans[1]==0 and ans[3]==0:
  print('Yes')
elif ans[0]==0 and ans[2]==0 and ans[1]!=0 and ans[3]!=0:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
s=input()
ans=[0]*4
checK='NWSE'
for i in range(len(s)):
  for j in range(len(check)):
    if s[i]==check[j]:
      ans[j]+=1
if (ans[0]%2+ans[2]%2)%2==0 and (ans[1]%2+ans[3]%2)%2==0:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
s=input()
ans=[0]*4
checK='NWSE'
for i in range(len(s)):
  for j in range(len(check)):
    if s[i]==check[j]:
      ans[j]+=1
if (ans[0]+ans[2])%2==0 and (ans[1]+ans[3])%2==0:
  print('Yes')
else:
  print('No')
Problem p04025 (python)

Fixed submission:
n=int(input())
arr=list(map(int,input().split()))
l=min(arr)
r=max(arr)
ans=10**10
for i in range(l,r+1):
  tmp=0
  for j in range(n):
    tmp+=(arr[j]-i)**2
  if tmp<ans:
    ans=tmp
print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input().split())
arr=list(map(int,input().split()))
l=min(arr)
r=max(arr)
ans=10**10
for i in range(l,r+1):
  tmp=0
  for j in range(n):
    tmp+=(arr[j]-i)**2
  if tmp<ans:
    ans=tmp
print(ans)
Problem p04035 (python)

Fixed submission:
n,l=map(int,input().split())
arr=list(map(int,input().split()))
pos=0
tmp=0
for i in range(n-1):
  if (arr[i]+arr[i+1])>tmp:
    pos=i
    tmp=(arr[i]+arr[i+1])
if tmp<l:
  print('Impossible')
else:
  print('Possible')
  ans=[]
  for i in range(pos):
    ans.append(i+1)
  for i in range(n-1,pos+1,-1):
    ans.append(i)
  ans.append(pos+1)
  for i in range(n-1):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
n,l=map(int,input().split())
arr=list(map(int,input().split()))
pos=0
tmp=0
for i in range(n-1):
  if (arr[i]+arr[i+1])>tmp:
    pos=i
    tmp=(arr[i]+arr[i+1])
if tmp<l:
  print('Impossible')
else:
  print('Possible')
  ans=[]
  for i in range(pos):
    ans.append(i+1)
  for i in range(n,pos+1):
    ans.append(i)
  ans.append(pos+1)
  for i in range(n-1):
    print(ans[i])
Problem p02582 (python)

Fixed submission:
S = str(input())

if S == "RRR":
    print(3)
elif S == "RRS" or S == "SRR":
    print(2)
elif S == "SSS":
    print(0)
else:
    print(1)

Buggy submission (verdict: Runtime Error):
S = str(input())

if S == "RRR":
    print(3)
elif S == "RRS" or S == "SRR":
    print(2)
elif S == "SSS"
    print(0)
else:
    print(1)
Problem p02606 (python)

Fixed submission:
import math
L,R,d = map(int, input().split())

minumum = math.ceil(L/d)
maximum = R//d

if maximum - minumum >=0:
    print(maximum - minumum+1)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
L,R,d = map(int, input().split())

minumum = math.ceil(L/d)
maximum = R//d

if maximum - minumum >=0:
    print(maximum - minumum+1)
else:
    print(0)