Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
def permutations_count(n, r):
    if not n>=r>=0:
        return(0)
    else:
        return factorial(n) // factorial(n - r)
def combinations_count(n, r):
    if not n>=r>=0 or n<=0:
        return(0)
    elif r==0:
        return(1)
    else:
        return factorial(n) // (factorial(n - r) * factorial(r))
N=input()
K=int(input())
if length<K:
    print(0)
    exit()
length=len(str(N))
count=0
count3=0
flag=1
for i in range(length):
    if N[i]!="0":
        if i<length-1:
            if K-count3 >=0:count+=combinations_count(length-1-count3,K-count3)*(9**(K-count3))
            if K-count3-1 >=0:count+=combinations_count(length-1-count3,K-count3-1)*(9**(K-1-count3))*(int(N[i])-1)
            count3+=1
    if K-count3==0:
        count+=1
        #count+=int(N[i])
        break
    if i==length-1:
        count+=int(N[i])
print(count)


Fixed submission:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
def permutations_count(n, r):
    if not n>=r>=0:
        return(0)
    else:
        return factorial(n) // factorial(n - r)
def combinations_count(n, r):
    if not n>=r>=0 or n<0:
        return(0)
    elif r==0:
        return(1)
    else:
        return factorial(n) // (factorial(n - r) * factorial(r))
N=input()
K=int(input())
length=len(str(N))
count=0
count3=0
flag=1
for i in range(length):
    if N[i]!="0":
        if K-count3 >=1:count+=combinations_count(length-1-i,K-count3)*(9**(K-count3))
        if K-count3-1 >=0:count+=combinations_count(length-1-i,K-count3-1)*(9**(K-1-count3))*(int(N[i])-1)
        count3+=1
    if K-count3==0:
        count+=1
        #count+=int(N[i])
        break
print(count)
Buggy python submission (verdict: Runtime Error):
#from collections import *
from itertools import *
#from bisect import *
#from heapq import *
#import copy
#N=int(input())
#X,Y=map(int,input().split())
#S=list(map(int,input().split()))
#S=[list(map(int,input().split())) for i in range(N)]
#import math

N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
Zlst=set([i for i in range(N)])
count=-1
for lst in combinations(range(N),N//2+N%2):
    odds=[[A[i],i] if i%2==0 else [B[i],i] for i in lst]
    even=[[B[i],i] if i%2==0 else [A[i],i] for i in list(Zlst-set(lst))]
    oddsst=sorted(odds,key=lambda  x:x[0])
    evenst=sorted(even,key=lambda  x:x[0])

    temp=oddsst[0][0]
    lst=[oddsst[0][1]]
    for i in range(1,N):
        if i%2==1:
            ntemp=evenst[i//2]
        else:
            ntemp=oddsst[i//2]
        if temp>ntemp[0]:
            flag=0
            break
        else:
            flag=1
            temp=ntemp[0]
            lst.append(ntemp[1])

    if flag:
        if count==-1:
            count=10**10
        tempcount=0
        while flag:
            flag=0
            for i in range(len(lst)-1):
                if lst[i]>lst[i+1]:
                    lst[i],lst[i+1]=lst[i+1],lst[i]
                    tempcount+=1
                    flag=1
        count=min(count,tempcount)


print(count)


Fixed submission:
#from collections import *
from itertools import *
#from bisect import *
#from heapq import *
#import copy
#N=int(input())
#X,Y=map(int,input().split())
#S=list(map(int,input().split()))
#S=[list(map(int,input().split())) for i in range(N)]
#import math

N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
Zlst=set([i for i in range(N)])
if N==1:
    print(0)
    exit()
count=-1
for lst in combinations(range(N),N//2+N%2):
    odds=[[A[i],i] if i%2==0 else [B[i],i] for i in lst]
    even=[[B[i],i] if i%2==0 else [A[i],i] for i in list(Zlst-set(lst))]
    oddsst=sorted(odds,key=lambda  x:x[0])
    evenst=sorted(even,key=lambda  x:x[0])

    temp=oddsst[0][0]
    lst=[oddsst[0][1]]
    for i in range(1,N):
        if i%2==1:
            ntemp=evenst[i//2]
        else:
            ntemp=oddsst[i//2]
        if temp>ntemp[0]:
            flag=0
            break
        else:
            flag=1
            temp=ntemp[0]
            lst.append(ntemp[1])

    if flag:
        if count==-1:
            count=10**10
        tempcount=0
        while flag:
            flag=0
            for i in range(len(lst)-1):
                if lst[i]>lst[i+1]:
                    lst[i],lst[i+1]=lst[i+1],lst[i]
                    tempcount+=1
                    flag=1
        count=min(count,tempcount)


print(count)
Buggy python submission (verdict: Time Limit Exceeded):
import collections

H,W=map(int,input().split())
S=[input() for i in range(H)]
cl=set()
for sy,sx in [[h,w] for h in range(H) for w in range(W)]:
    if S[sy][sx]!="#":
        for gy,gx in [[h,w] for h in range(H) for w in range(W)]:
            if S[gy][gx]!="#":
                stack=collections.deque([[sy,sx,0]])
                itta=set([sy*100+sx])
                #print(stack)
                while stack:
                    y,x,m=stack.popleft()
                    if y==gy and x==gx:
                        cl.add(m)
                        break
                    lst=[]

                    if y>0:lst.append([-1,0])
                    if y<H-1:lst.append([1,0])
                    if x>0:lst.append([0,-1])
                    if x<W-1:lst.append([0,1])

                    for a,b in lst:
                        Y=y+a
                        X=x+b
                        #print(Y,X)
                        if S[Y][X]=="." and not Y*100+X in itta:
                            stack.append([Y,X,m+1])
print(max(cl))


Fixed submission:
import collections

H,W=map(int,input().split())
S=[input() for i in range(H)]
cl=set()
for sy,sx in [[h,w] for h in range(H) for w in range(W)]:
    if S[sy][sx]!="#":
        stack=collections.deque([[sy,sx,0]])
        itta=set([sy*100+sx])
        #print(stack)
        while stack:
            y,x,m=stack.popleft()
            cl.add(m)
            lst=[]

            if y>0:lst.append([-1,0])
            if y<H-1:lst.append([1,0])
            if x>0:lst.append([0,-1])
            if x<W-1:lst.append([0,1])

            for a,b in lst:
                Y=y+a
                X=x+b
                #print(Y,X)
                if S[Y][X]=="." and not Y*100+X in itta:
                    stack.append([Y,X,m+1])
                    itta.add(Y*100+X)
print(max(cl))
Buggy python submission (verdict: Runtime Error):
from fractions import gcd
def yaku2(a):
    count=0
    while a%2:
        count+=a//2
    return(count)

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

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

yaku=yaku2(A[0])
for a in range(1,N):
    if yaku!=yaku2(A[i]):
        print(0)
        break


_lcm=A[0]
for i in range(1,N):
    A[0]=lcm(A[0],A[i])
#print(A[0])
print((M-(A[0]-A[0]//2-A[0]))//A[0])


Fixed submission:
from fractions import gcd
def yaku2(a):
    count=0
    while a%2==0:
        a=a//2
        count+=1
    return(count)

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

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

yaku=yaku2(A[0])
for i in range(1,N):
    if yaku!=yaku2(A[i]):
        print(0)
        exit()



_lcm=A[0]
for i in range(1,N):
    A[0]=lcm(A[0],A[i])
#print(A[0])
print((M-(A[0]-A[0]//2-A[0]))//A[0])
Buggy python submission (verdict: Runtime Error):
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math
N,M,V,P=map(int,input().split())
A=sorted(list(map(int,input().split())))
_SUM=sum(A[:N-(P-1)])



def main(s):
    tempSUM=_SUM-s
    s+=M
    a=(V-P)

    if s<A[-P]:
        return(0)
    else:
        if a>0:
            tempSUM+=a*M
            if s>tempSUM//(N-P)) or s==tempSUM//(N-P)) and tempSUM%(N-P)) == 0:
                return(1)
        else:
            return(1)


if main(A[0]):
    print(N)
    exit()
st=0
end=N-1

while 1:
    a=(st+end)//2
    if main(A[a]):
        end=a
    else:
        st=a

    if end-st<=1:
        break

if main(A[st]):
    print(N-st)
else:
    print(N-end)


Fixed submission:
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math
N,M,V,P=map(int,input().split())
A=sorted(list(map(int,input().split())))
_SUM=sum(A[:N-(P-1)])



def main(i):

    B=[A[m] for m in range(N-(P-1)) if m!=i]

    C=A[i]+M
    a=V-P

    if C<A[-P]:
        return(0)
    else:
        if a>0:
            Votenum=a*M
            for b in B:
                Votenum-=min(C-b,M)
                if Votenum<=0:
                    return(1)

            return(0)
        else:
            return(1)




if main(0):
    print(N)
    exit()
st=0
end=N-P+1

while 1:
    a=(st+end)//2
    if main(a):
        end=a
    else:
        st=a

    if end-st<=1:
        break

if main(st):
    print(N-st)
else:
    print(N-end)
Buggy python submission (verdict: Time Limit Exceeded):
mod=10**9 + 7
#print(bin(9))
N=int(input())
A=list(map(int,input().split()))
A.sort()
#print(A)
A=[bin(i)[2:] for i in A]
#print(A)
nowdata=[0 for i in range(60)]
data=[0 for i in range(60)]

#print(A)

for i in range(N-1):
    S=A[i]

    for a in range(len(S)):
        if S[-a-1]=="1":
            nowdata[a]+=1
    S=A[i+1]
    for a in range(len(S)):
        if S[-a-1]=="0":
            data[a]+=nowdata[a]
        else:
            data[a]+=i+1-nowdata[a]
    #print(nowdata)


num=0
#print(data)
for i in range(60):
    num+=data[i]* (2**i)
    num%=mod

print(num)


Fixed submission:
mod=10**9 + 7
#print(bin(9))
N=int(input())
A=list(map(int,input().split()))
A=[bin(i)[2:] for i in A]
A=[(60-len(i)) * "0" + i for i in A]
#print(A)
nowdata=[0 for i in range(60)]
data=[0 for i in range(60)]

#print(A)

for i in range(N-1):
    S=A[i]
    #print(nowdata)
    for a in range(len(S)):
        if S[a]=="1":
            nowdata[a]+=1
    S=A[i+1]
    for a in range(len(S)):
        if S[a]=="0":
            data[a]+=nowdata[a]
        else:
            data[a]+=i+1-nowdata[a]


num=0
#print(data)
for i in range(60):
    num+=data[-i-1]* (2**i)
    num%=mod

print(num)

Buggy python submission (verdict: Time Limit Exceeded):
mod=10**9 + 7
#print(bin(9))
N=int(input())
A=list(map(int,input().split()))
A.sort()
#print(A)
A=[bin(i)[2:] for i in A]
#print(A)
nowdata=[0 for i in range(60)]
data=[0 for i in range(60)]

#print(A)

for i in range(N-1):
    S=A[i]

    for a in range(len(S)):
        nowdata[a]+=int(S[-a-1])
    S=A[i+1]
    for a in range(len(S)):
        if S[-a-1]=="0":
            data[a]+=nowdata[a]
        else:
            data[a]+=i+1-nowdata[a]
    #print(nowdata)


num=0
#print(data)
for i in range(60):
    num+=data[i]* (2**i)
    num%=mod

print(num)


Fixed submission:
mod=10**9 + 7
#print(bin(9))
N=int(input())
A=list(map(int,input().split()))
A=[bin(i)[2:] for i in A]
A=[(60-len(i)) * "0" + i for i in A]
#print(A)
nowdata=[0 for i in range(60)]
data=[0 for i in range(60)]

#print(A)

for i in range(N-1):
    S=A[i]
    #print(nowdata)
    for a in range(len(S)):
        if S[a]=="1":
            nowdata[a]+=1
    S=A[i+1]
    for a in range(len(S)):
        if S[a]=="0":
            data[a]+=nowdata[a]
        else:
            data[a]+=i+1-nowdata[a]


num=0
#print(data)
for i in range(60):
    num+=data[-i-1]* (2**i)
    num%=mod

print(num)
Buggy python submission (verdict: Runtime Error):
H,W,K=map(int,input().split())
S=[input() for i in range(H)]
P=[[0 for i in range(W)] for m in range(H)]

for h in range(H):
    if "#" in S[h]:
        front=h
        break


m=0
x=0
for w in range(W):
    if S[front][w]=="#":
        m+=1
        for z in range(x,w+1):
            P[front][z]=m
        x=w+1
for z in range(x,W):
    P[front][z]=m

for h in range(front-1,-1,-1):
    for m in range(M):
        P[h][m]=S[h+1][m]


for h in range(front+1,H):
    if "#" in S[h]:
        x=0
        for w in range(W):
            if S[h][w]=="#":
                m+=1
                for z in range(x,w+1):
                    P[h][z]=m
                x=w+1
        for z in range(x,W):
            P[h][z]=m
    else:
        for w in range(W):
            P[h][w]=S[h-1][w]

for h in range(H):
    print(" ".join(list(map(str,P[h]))))


Fixed submission:
H,W,K=map(int,input().split())
S=[input() for i in range(H)]
P=[[0 for i in range(W)] for m in range(H)]

for h in range(H):
    if "#" in S[h]:
        front=h
        break


m=0
x=0
for w in range(W):
    if S[front][w]=="#":
        m+=1
        for z in range(x,w+1):
            P[front][z]=m
        x=w+1
for z in range(x,W):
    P[front][z]=m

for h in range(front-1,-1,-1):
    for w in range(W):
        P[h][w]=P[h+1][w]


for h in range(front+1,H):
    if "#" in S[h]:
        x=0
        for w in range(W):
            if S[h][w]=="#":
                m+=1
                for z in range(x,w+1):
                    P[h][z]=m
                x=w+1
        for z in range(x,W):
            P[h][z]=m
    else:
        for w in range(W):
            P[h][w]=P[h-1][w]

for h in range(H):
    print(" ".join(list(map(str,P[h]))))
Buggy python submission (verdict: Time Limit Exceeded):
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

N=int(input())
Flist=factorization(N)
F_list=[]
for i in Flist:
    F_list+=[i[0]]*i[1]
F_list.sort()
data=[[1,1] for i in range(2**len(F_list))]
for i in range(2**len(F_list)):
    for m,s in enumerate("0"*(len(F_list)-len(bin(i)[2:]))+bin(i)[2:]):
        if int(s):data[i][0]*=F_list[m]
        else     :data[i][1]*=F_list[m]
data2=data[[i[0]-i[1] for i in data].index(min([abs(i[0]-i[1]) for i in data]))]
print(data2[0]+data2[1]-2)


Fixed submission:
N=int(input())
print(min([sum([i-1,round(N/i)-1]) for i in range(1,int(N**0.5)+1) if N%i==0]))

Buggy python submission (verdict: Time Limit Exceeded):
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

N=int(input())
Flist=factorization(N)
F_list=[]
for i in Flist:
    F_list+=[i[0]]*i[1]
F_list.sort()
data=[[1,1] for i in range(2**len(F_list))]
for i in range(2**len(F_list)):
    for m,s in enumerate("0"*(len(F_list)-len(bin(i)[2:]))+bin(i)[2:]):
        if int(s):data[i][0]*=F_list[m]
        else     :data[i][1]*=F_list[m]
data2=data[[i[0]-i[1] for i in data].index(min([abs(i[0]-i[1]) for i in data]))]
print(data2[0]+data2[1]-2)


Fixed submission:
N=int(input())
print(min([sum([i-1,round(N/i)-1]) for i in range(1,int(N**0.5)+1) if N%i==0]))
Buggy python submission (verdict: Runtime Error):
import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        m=bisect.bisect_right(L[d1]+L[d2],l)
        if m>d2:count+=m-d2
print(count)


Fixed submission:
import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        m=bisect.bisect_left(L,L[d1]+L[d2])
        if m>d2+1:count+=m-(d2+1)
print(count)

Buggy python submission (verdict: Runtime Error):
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        m=bisect.bisect_right(L[d1]+L[d2],l)
        if:m>d2:count+=m-d2
print(count)


Fixed submission:
import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        m=bisect.bisect_left(L,L[d1]+L[d2])
        if m>d2+1:count+=m-(d2+1)
print(count)

Buggy python submission (verdict: Runtime Error):
N=int(input())
L=sorted(list(map(int,input().split())))
L=list(set(L))
count=0
d2breakFlag=False
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        Temp=L[d1]+L[d2]

        """for d3 in range(d2+1,N):
            if L[d1]+L[d2]>L[d3]:count+=1
            else:break
print(count)"""


Fixed submission:
import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        m=bisect.bisect_left(L,L[d1]+L[d2])
        if m>d2+1:count+=m-(d2+1)
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
d2breakFlag=False
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        for d3 in range(d2+1,N):
            if L[d1]+L[d2]>L[d3]:count+=1
            else:break
print(count)


Fixed submission:
import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        m=bisect.bisect_left(L,L[d1]+L[d2])
        if m>d2+1:count+=m-(d2+1)
print(count)
Buggy python submission (verdict: Runtime Error):
S=input()
K=int(input())

if list(S).count(S[0])==len(S):
    print(len(S)*K//2)

else:
    S_count=0
    i=0
    while i<len(S)-1:
        if S[i]==S[i+1]:
            S_count+=1
            i+=1
        i+=1

    if S[0]==S[-1]:
        a=0
        i=0
        while i<len(S)-1:
            if S[i]==S[i+1]:
                a+=1
                i+=1
            else:
                break
        b=0
        i=len(S)-1
        while i>0:
            if S[i]==S[i-1]:
                b+=1
                i+=-1
            else:
                break

        S_count*=K
        S_count-=(K-1)*(a//2 + b//2 - (a+b)//2)
        print(unko)
    else:
        S_count*=K
    print(S_count)


Fixed submission:
S=input()
K=int(input())

if list(S).count(S[0])==len(S):
    print(len(S)*K//2)

else:
    S_count=0
    i=0
    while i<len(S)-1:
        if S[i]==S[i+1]:
            S_count+=1
            i+=1
        i+=1

    if S[0]==S[-1]:
        a=1
        i=0
        while i<len(S)-1:
            if S[i]==S[i+1]:
                a+=1
                i+=1
            else:
                break
        b=1
        i=len(S)-1
        while i>0:
            if S[i]==S[i-1]:
                b+=1
                i+=-1
            else:
                break

        S_count*=K
        S_count-=(K-1)*(a//2 + b//2 - (a+b)//2)

    else:
        S_count*=K
    print(S_count)
Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=sorted(list(map(int,input().split())),reverse=True)
idx=1
for i in range(M):
    value=0

    for m in range(idx):
        if A[m]>value:
            value=A[m]
            nidx=m
    if nidx+1==idx:
        idx+=1
        idx=min(idx,N)
    A[nidx]/=2

A=list(map(int,A))
print(sum(A))


Fixed submission:
import heapq
N,M=map(int,input().split())
A=list(map(lambda x:x*-(1),list(map(int,input().split()))))
heapq.heapify(A)
for i in range(M):
    num=(heapq.heappop(A)*(-1)/2)
    heapq.heappush(A, num*-1)



A=list(map(int,A))
print(-sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=sorted(list(map(int,input().split())),reverse=True)
idx=1
for i in range(M):
    nidx=A[:idx].index(max(A[:idx]))
    if nidx+1==idx:
        idx+=1
    A[nidx]/=2

A=list(map(int,A))
print(sum(A))


Fixed submission:
import heapq
N,M=map(int,input().split())
A=list(map(lambda x:x*-(1),list(map(int,input().split()))))
heapq.heapify(A)
for i in range(M):
    num=(heapq.heappop(A)*(-1)/2)
    heapq.heappush(A, num*-1)



A=list(map(int,A))
print(-sum(A))
Buggy python submission (verdict: Time Limit Exceeded):
N,Q=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(N-1)]
PX=[list(map(int,input().split())) for i in range(Q)]

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

pt= [0 for i in range(N+1)]
for a,b in AB:
    eda[a].append(b)
    eda[b].append(a)
for p,x in PX:
    pt[p]+=x
eda=[sorted(i) for i in eda]

stack=[[1,eda[1]]]
v=set()
v.add(1)
while stack:
    while stack[-1][1]:
        a=stack[-1][1].pop()
        if not a in v:
            v.add(a)
            pt[a]+=pt[stack[-1][0]]
            stack.append([a,eda[a]])
    stack.pop()
print(" ".join(list(map(str,pt[1:]))))


Fixed submission:
N,Q=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(N-1)]
PX=[list(map(int,input().split())) for i in range(Q)]

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

pt= [0 for i in range(N+1)]
for a,b in AB:
    eda[a].append(b)
    eda[b].append(a)
for p,x in PX:
    pt[p]+=x


stack=[[1,eda[1]]]
v=set()
v.add(1)
while stack:
    while stack[-1][1]:
        a=stack[-1][1].pop()
        if not a in v:
            v.add(a)
            pt[a]+=pt[stack[-1][0]]
            stack.append([a,eda[a]])
    stack.pop()
print(" ".join(list(map(str,pt[1:]))))
Buggy python submission (verdict: Runtime Error):
from itertools import *
from collections import *
from bisect import *
s=list(input())
t=input()
sets=set(s)

a=len(s)*len(t)
maxcount=len(s)
for i in range(100):
    if maxcount<a
        maxcount**=10
    else:
        break


for T in t:
    if not T in sets:
        print(-1)
        exit()
N=len(s)
dic={}
for i in range(N):
    if not s[i] in dic:
        dic.update({s[i]:[i]})
    else:
        dic[s[i]].append(i)

count=1
prei=0
for i in range(len(t)):
    idx=bisect_left(dic[t[i]],prei)
    if idx<len(dic[t[i]]):
        prei=dic[t[i]][idx]
    else:
        prei=dic[t[i]][0]
        count+=N
    if i==len(t)-1:
        count+=prei



if count<=maxcount:
    print(count)
else:
    print(-1)


Fixed submission:
from itertools import *
from collections import *
from bisect import *
s=list(input())
t=input()
sets=set(s)
"""
a=len(s)*len(t)
maxcount=len(s)
for i in range(100):
    if maxcount<a:
        maxcount**=10
    else:
        break
"""

for T in t:
    if not T in sets:
        print(-1)
        exit()
N=len(s)
dic={}
for i in range(N):
    if not s[i] in dic:
        dic.update({s[i]:[i]})
    else:
        dic[s[i]].append(i)

count=0
prei=0
for i in range(len(t)):
    idx=bisect_left(dic[t[i]],prei)
    if idx<len(dic[t[i]]):
        prei=dic[t[i]][idx]+1
    else:
        prei=dic[t[i]][0]+1
        count+=N
    if i==len(t)-1:
        count+=prei



if count<=len(s)*(10**100):
    print(count)
else:
    print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
import collections
data=[collections.Counter(list(input())) for i in range(int(input()))]
count=0
for i in range(len(data)):
    for m in range(i+1,len(data)):
        count+=data[i]==data[m]
print(count)


Fixed submission:
import collections
N=int(input())
Slst=["".join(sorted(list(input()))) for i in range(N)]
dic={}
namelst=set()

for S in Slst:
    if S in namelst:
        dic[S]+=1
    else:
        namelst.add(S)
        dic.update({S:1})
#print(dic)

print(sum([dic[i]*(dic[i]-1)//2 for i in dic if dic[i]>=2]))