Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
"""
k=int(input())
a=list(map(int,input().split()))
if k==1:
    print(0)
else:
    dp = [0] * k
    dp[1] = abs(a[0]-a[1])
    for i in range(2,k):
        dp[i]=min(
            dp[i-1]+abs(a[i]-a[i-1]),
            dp[i-2]+abs(a[i]-a[i-2])
        )
    print(dp[-1])
"""
from collections import deque
n=int(input())
cost=list(map(int,input().split()))
if n==1:
    print(0)
else:
    # if we reach early step faster then terminate which makes more sense
    ways = [1, 2]
    ways.sort(reverse=True)
    q = deque([(0, 0)])  # path,cost
    min_cost = float('inf')
    explore_more = 1
    while q:
        # print(q)
        for _ in range(len(q)):
            path, cost_ = q.popleft()
            if path == n - 1:
                min_cost = min(min_cost, cost_)
                explore_more = 0
            if explore_more:
                for way in ways:
                    if path + way < n:
                        q.append((path + way, cost_ + abs(cost[path] - cost[path + way])))

    print(min_cost)




Fixed submission:
k=int(input())
a=list(map(int,input().split()))
if k==1:
    print(0)
else:
    dp = [0] * k
    dp[1] = abs(a[0]-a[1])
    for i in range(2,k):
        dp[i]=min(
            dp[i-1]+abs(a[i]-a[i-1]),
            dp[i-2]+abs(a[i]-a[i-2])
        )
    print(dp[-1])

Buggy python submission (verdict: Runtime Error):
# url: https://atcoder.jp/contests/dp/tasks
from functools import lru_cache
k=int(input())
a=list(map(int,input().split()))

@lru_cache(maxsize=None)
def f(idx,cost):
    if idx==k-1:
        return cost
    return min([
       f(idx+i,cost+abs(a[idx]-a[idx+i])) for i in [1,2]
        if idx+i<=k-1
    ])
print(f(0,0))

Fixed submission:
k=int(input())
a=list(map(int,input().split()))
if k==1:
    print(0)
else:
    dp = [0] * k
    dp[1] = abs(a[0]-a[1])
    for i in range(2,k):
        dp[i]=min(
            dp[i-1]+abs(a[i]-a[i-1]),
            dp[i-2]+abs(a[i]-a[i-2])
        )
    print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

def make_reverse(S):

    if len(S) == 1:
        return S

    reversedS = ""
    for i in range(len(S)-1, -1, -1):
        reversedS += S[i]
    else: return reversedS


def main(S, Q):
    
    for i in range(Q):

        query = input().split()
        if int(query[0]) == 1:
            S = make_reverse(S)
        else:
            if int(query[1]) == 1:
                S = query[2] + S
            else:
                S = S + query[2]
    
    else: return S


if __name__ == "__main__":

    answer = main(S, Q)
    print(answer)
    

Fixed submission:
S = input()
Q = int(input())
lst_query = [input().split() for i in range(Q)]

def make_reverse(S):

    if len(S) == 1:
        return S

    reversedS = ""
    for i in range(len(S)-1, -1, -1):
        reversedS += S[i]
    else: return reversedS

def main(S, Q, lst_query):

    cnt_reverse = 0
    leftS  = ""
    rightS = ""

    for i in range(Q-1, -1, -1):
        
        query = lst_query[i]

        if int(query[0]) == 1:
            cnt_reverse += 1
        else:
            if cnt_reverse % 2 == 0:
                if int(query[1]) == 1: leftS  = leftS + query[2]
                else                 : rightS = query[2] + rightS
            else:
                if int(query[1]) == 1: rightS = query[2] + rightS
                else                 : leftS  = leftS + query[2]

    if cnt_reverse % 2 == 1: S = make_reverse(S)
    return leftS + S + rightS


if __name__ == "__main__":

    answer = main(S, Q, lst_query)
    print(answer)
Buggy python submission (verdict: Runtime Error):
[N, M] = list(map(int, input().split()))
lst_sc = [list(map(int, input().split())) for i in range(M)]

def main(N, M, lst_sc):
    
    numbers = [''] * N
    for sc in lst_sc:

        [s, c] = sc

        if s > N: return -1

        if numbers[s-1] == '':
            numbers[s-1] = str(c)
        elif numbers[s-1] == str(c):
            pass
        else:
            return -1

    if N > 1 and numbers[0] == '0': return -1
    if numbers[1] == '': numbers[1] = '0'
    if numbers[2] == '': numbers[2] = '0'
    
    answer = ''
    for i in range(N):
        answer += numbers[i]
    return int(answer)
    

if __name__ == "__main__":

    answer = main(N, M, lst_sc)
    print(answer)


Fixed submission:
[N, M] = list(map(int, input().split()))
lst_sc = [list(map(int, input().split())) for i in range(M)]

def main(N, M, lst_sc):
    
    numbers = [''] * N
    for sc in lst_sc:

        [s, c] = sc

        if s > N: return -1

        if numbers[s-1] == '':
            numbers[s-1] = str(c)
        elif not numbers[s-1] == str(c):
            return -1
            
    if N > 1 and numbers[0] == '0': return -1
    if N == 1 and numbers[0] == '': return 0
    
    answer = ''
    for i in range(N):
        if numbers[i] == '':
            if i == 0: numbers[i] = '1'
            else     : numbers[i] = '0'
        answer += numbers[i]
    return int(answer)
    

if __name__ == "__main__":

    answer = main(N, M, lst_sc)
    print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
def calc_E(prob):

    E = sum([i for i in range(1, prob+1)]) / prob
    return E

def main(N, K, list_p):
    
    list_E = list(map(calc_E, list_p))
    max_E  = sum(list_E[0:K])
    prev_E = sum(list_E[0:K])

    for i in range(1, N-K+1):
        tmpE   = prev_E - list_E[i-1] + list_E[i-1+K]
        max_E  = max(max_E, tmpE)
        prev_E = tmpE

    return max_E
        


if __name__ == "__main__":

    [N, K] = list(map(int, input().split(' ')))
    list_p = list(map(int, input().split(' ')))

    answer = main(N, K, list_p)
    print(answer)

Fixed submission:
def calc_E(prob):

    E = (prob + 1) / 2
    return E

def main(N, K, list_p):
    
    list_E = list(map(calc_E, list_p))
    max_E  = sum(list_E[0:K])
    prev_E = sum(list_E[0:K])

    for i in range(1, N-K+1):
        tmpE   = prev_E - list_E[i-1] + list_E[i-1+K]
        max_E  = max(max_E, tmpE)
        prev_E = tmpE

    return max_E
        


if __name__ == "__main__":

    [N, K] = list(map(int, input().split(' ')))
    list_p = list(map(int, input().split(' ')))

    answer = main(N, K, list_p)
    print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
def calc_E(prob):

    E = sum([i for i in range(1, prob+1)]) / prob
    return E

def main(N, K, list_p):
    
    max_E = 0

    for i in range(N-K+1):
        tmpE = 0
        for j in range(K):  tmpE += calc_E(list_p[i+j])
        if tmpE >= max_E :  max_E = tmpE
    
    return max_E
        


if __name__ == "__main__":

    [N, K] = list(map(int, input().split(' ')))
    list_p = list(map(int, input().split(' ')))

    answer = main(N, K, list_p)
    print(answer)

Fixed submission:
def calc_E(prob):

    E = (prob + 1) / 2
    return E

def main(N, K, list_p):
    
    list_E = list(map(calc_E, list_p))
    max_E  = sum(list_E[0:K])
    prev_E = sum(list_E[0:K])

    for i in range(1, N-K+1):
        tmpE   = prev_E - list_E[i-1] + list_E[i-1+K]
        max_E  = max(max_E, tmpE)
        prev_E = tmpE

    return max_E
        


if __name__ == "__main__":

    [N, K] = list(map(int, input().split(' ')))
    list_p = list(map(int, input().split(' ')))

    answer = main(N, K, list_p)
    print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
iipt=lambda:int(input())
miipt=lambda:list(map(int,input().split(" ")))
n, m=miipt()
a=[]
done=[0]*m
for i in range(m):
    a.append(miipt())
a=list(enumerate(a))
b=sorted(a, key=lambda x:x[1])
a.sort(key=lambda x:x[1][1])
c=0
while not all(done):
    j, (_, h) = a.pop(0)
    if(done[j]): continue
    d = filter(lambda x: x[1][0]<h and not done[x[0]], b)
    if d:
        c+=1
    for (i, l) in d:
        done[i]=1
print(c)

Fixed submission:
import bisect
iipt=lambda:int(input())
miipt=lambda:list(map(int,input().split(" ")))
n, m=miipt()
a=[]
done=[0]*m
for i in range(m):
    a.append(miipt())
b=sorted(a, key=lambda x:x[1])
c=0
d=-1
for p, q in b:
    if not p<=d<q:
        c+=1
        d=q-1
print(c)
Buggy python submission (verdict: Runtime Error):
i=lambda:int(input())
print((i()+i()+1)//2)

Fixed submission:
a,b=map(int,input().split(" "))
print((a+b+1)//2)
Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
print((n-1)(m-1))

Fixed submission:
n,m=map(int,input().split())
print((n-1)*(m-1))
Buggy python submission (verdict: Runtime Error):
print((int(input())+int(input()))%24)

Fixed submission:
a,b=map(int,input().split())
print((a+b)%24)
Buggy python submission (verdict: Runtime Error):
n,k = maP(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for i in arr:
  if i >= k:
    ans += 1
print(ans)

Fixed submission:
n,k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for i in arr:
  if i >= k:
    ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
arr = [0 for i in range(n)]
cnt = list(map(int, input().split()))
for i in range(n):
  arr[cnt[i]] = i+1
print(*arr)

Fixed submission:
n = int(input())
arr = [0 for i in range(n)]
cnt = list(map(int, input().split()))
for i in range(n):
  arr[cnt[i]-1] = i+1
print(*arr)
Buggy python submission (verdict: Runtime Error):
n = int(input())
arr = list(map(int, input().split()))
rev = sorted(arr, reverse = True)
for i in range(n):
  if arr[i] == rev[-1]:
    print(rev[-2])
  else:
	print(rev[-1])

Fixed submission:
n = int(input())
arr = []
for i in range(n):
    arr.append(int(input()))
rev = sorted(arr, reverse = True)
for i in range(n):
    if arr[i] == rev[0]:
        print(rev[1])
    else:
        print(rev[0])
Buggy python submission (verdict: Runtime Error):
from math import inf
n = int(input())
arr = list(map(int, input().split()))
cnt = [[0,0] for x in range(n-1)]
for i in range(1,n):
    if i == 1:
        cnt[i-1][0] = abs(arr[i-1]-arr[i])
        cnt[i-1][1] = inf
    else:
        cnt[i-1][0] = abs(arr[i-1]-arr[i])
        cnt[i-1][1] = abs(arr[i-2]-arr[i])
for i in range(1,n-1):
    cnt[i][0] += min(cnt[i-1])
    if i != 1:
        cnt[i][1] += min(cnt[i-2])
print(min(cnt[-1]))


Fixed submission:
n = int(input())
arr = list(map(int, input().split()))
cnt = [[0,0] for x in range(n-1)]
for i in range(1,n):
	if i == 1:
		cnt[i-1][0] = abs(arr[i-1]-arr[i])
		cnt[i-1][1] = 100000
	else:
		cnt[i-1][0] = abs(arr[i-1]-arr[i])
		cnt[i-1][1] = abs(arr[i-2]-arr[i])
for i in range(1,n-1):
	cnt[i][0] += min(cnt[i-1])
	if i != 1:
		cnt[i][1] += min(cnt[i-2])
print(min(cnt[-1]))

Buggy python submission (verdict: Runtime Error):
from math import inf
n = int(input())
arr = list(map(int, input().split()))
cnt = [[0,0] for x in range(n-1)]
for i in range(1,n):
    if i == 1:
        cnt[i-1][0] = abs(arr[i-1]-arr[i])
        cnt[i-1][1] = inf
    else:
        cnt[i-1][0] = abs(arr[i-1]-arr[i])
        cnt[i-1][1] = abs(arr[i-2]-arr[i])
for i in range(1,n-1):
    cnt[i][0] += min(cnt[i-1])
    if i != 1:
        cnt[i][1] += min(cnt[i-2])
print(min(cnt[-1]))


Fixed submission:
n = int(input())
arr = list(map(int, input().split()))
cnt = [[0,0] for x in range(n-1)]
for i in range(1,n):
	if i == 1:
		cnt[i-1][0] = abs(arr[i-1]-arr[i])
		cnt[i-1][1] = 100000
	else:
		cnt[i-1][0] = abs(arr[i-1]-arr[i])
		cnt[i-1][1] = abs(arr[i-2]-arr[i])
for i in range(1,n-1):
	cnt[i][0] += min(cnt[i-1])
	if i != 1:
		cnt[i][1] += min(cnt[i-2])
print(min(cnt[-1]))
Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())                            # 1行2列
List=[list(map(int,input().split())) for i in range(M)] # N行C列・・・Cは任意


if N == 1:
	answer = ' '
elif N == 2:
	answer = '  '
elif N == 3:
	answer = '   '

ok = 0
for search in List:
	# search[0]:桁数
	# search[1]:値
	if answer[search[0]-1] == str(search[1]) or answer[search[0]-1] == ' ':
		if N != 1 and search[0] == 1 and search[1] == 0:
			ok = -1
			break
		else:
			anslist = list(answer)
			anslist[search[0]-1] = str(search[1])
			answer = ''.join(anslist)
	else:
		ok = -1
		break

if ok == -1:
	print(-1)
else:
	for i in range(N):
		if answer[i] == ' ':
			anslist = list(answer)
			
			if i == 0 and N != 1:
				anslist[i] = '1'
			else:
				anslist[i] = '0'
			
			answer = ''.join(anslist)
	print(answer)


Fixed submission:
N=int(input())                                          # 1行1列


if N %2 == 0:
	print(int(N/2))
else:
	print(int((N+1)/2))
Buggy python submission (verdict: Runtime Error):
def main():
	all,rolls=map(int,input().split())
	List=list(map(int,input().split()))

	sum = 0
	sumList =[]

	for i in range(all - rolls + 1):
		sum = 0
		for j in range(rolls):
			sum = sum + (List[i + j] + 1) / 2
		sumList.append(sum)

	sumList.sort(reverse=True)
	print(sumList[0])

if __name__ == '__main__':
	main()

Fixed submission:
a,b=input().split()
c,d=map(int,input().split())
e=input()

if a==e:
	c = c - 1
else:
	d = d - 1

print(c,d)

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

all,rolls=map(int,input().split())
List=list(map(int,input().split()))

sum = 0
sumList =[]

for i in range(all - rolls + 1):
	sum = 0
	for j in range(rolls):
		sum = sum + (List[i + j] + 1) / 2
	sumList.append(sum)

sumList.sort(reverse=True)
print(sumList[0])

Fixed submission:
a,b=input().split()
c,d=map(int,input().split())
e=input()

if a==e:
	c = c - 1
else:
	d = d - 1

print(c,d)

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

all,rolls=map(int,input().split())
List=list(map(int,input().split()))

sum = 0
sumList =[]

for i in range(all - rolls + 1):
	sum = 0
	for j in range(rolls):
		sum = sum + (List[i + j] + 1) / 2
	sumList.append(sum)

sumList.sort(reverse=True)
print(sumList[0])

Fixed submission:
a,b=input().split()
c,d=map(int,input().split())
e=input()

if a==e:
	c = c - 1
else:
	d = d - 1

print(c,d)

Buggy python submission (verdict: Runtime Error):
all,rolls=map(int,input().split())
List=list(map(int,input().split()))

sum = 0
sumList =[]

for i in range(all - rolls + 1):
	sum = 0
	for j in range(rolls):
		sum = sum + (List[i + j] + 1) / 2
	sumList.append(sum)

sumList.sort(reverse=True)
print(sumList[0])

Fixed submission:
a,b=input().split()
c,d=map(int,input().split())
e=input()

if a==e:
	c = c - 1
else:
	d = d - 1

print(c,d)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import heapq
X, Y, A, B, C = map(int, input().split())
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]

p.sort(reverse = True)
q.sort(reverse = True)
r.sort(reverse = True)

red = p[:X]
green = q[:Y]
heapq.heapify(red)
heapq.heapify(green)

while True :
    if not r :
        break
    if red[0] > green[0] :
        if green[0] < r[0] :
            heapq.heapreplace(green, r[0])
            r.pop(0)
        else :
            break
    else :
        if red[0] < r[0] :
            heapq.heapreplace(red, r[0])
            r.pop(0)
        else :
            break

print(sum(red)+sum(green))

Fixed submission:
import sys
input = sys.stdin.readline
X, Y, A, B, C = map(int, input().split())
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]

p.sort(reverse = True)
q.sort(reverse = True)
red = p[:X]
green = q[:Y]
ans = red + green + r
ans.sort(reverse = True)
print(sum(ans[:X+Y]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
X, Y, A, B, C = map(int, input().split())
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]

p.sort(reverse = True)
q.sort(reverse = True)
r.sort(reverse = True)

red = p[:X]
green = q[:Y]
ri = 1
gi = 1

while True :
    if not r :
        break
    red_min = min(red[-ri], red[-1])
    green_min = min(green[-gi], green[-1])
    if red_min > green_min :
        if green_min < r[0] :
            green.remove(green_min)
            green.append(r[0])
            gi += 1
            r.pop(0)
        else :
            break
    else :
        if red_min < r[0] :
            red.remove(red_min)
            red.append(r[0])
            ri += 1
            r.pop(0)
        else :
            break

print(sum(red)+sum(green))


Fixed submission:
import sys
input = sys.stdin.readline
X, Y, A, B, C = map(int, input().split())
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]

p.sort(reverse = True)
q.sort(reverse = True)
red = p[:X]
green = q[:Y]
ans = red + green + r
ans.sort(reverse = True)
print(sum(ans[:X+Y]))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import defaultdict

N = int(input())
A = [int(x) for x in input().split()]

d = defaultdict(int)
for i in A :
    d[i] += 1

for i in A :
    d[i] -= 1
    ans = 0
    for a in d :
        ans += d.get(a)*(d.get(a)-1)//2
    print(ans)
    d[i] += 1


Fixed submission:
import sys
input = sys.stdin.readline
from collections import defaultdict

N = int(input())
A = [int(x) for x in input().split()]

d = defaultdict(int)
for i in A :
    d[i] += 1

ans = 0
ans2 = defaultdict(int)
for a in d :
    ans += d.get(a)*(d.get(a)-1)//2
    ans2[a] = d.get(a)*(d.get(a)-1)//2 - (d.get(a)-1)*(d.get(a)-2)//2

for i in A :
    print(ans - ans2[i])
Buggy python submission (verdict: Runtime Error):
a, b, c = input().split()

if c - a - b > 0:
    if 4 * a * b < (c - a - b) ** 2:
        print("Yes")
    else:
        print("No")
else:
    print("No")


Fixed submission:
a, b, c = map(int, input().split())

if c - a - b > 0:
    if 4 * a * b < (c - a - b) ** 2:
        print("Yes")
    else:
        print("No")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
S = input()
if S = "AAA" or "BBB" :
    print("Yes")
else :
    print("No")


Fixed submission:
S = input()
if S == "AAA" or S == "BBB" :
    print("No")
else :
    print("Yes")
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
A, B = map(int, input().split())

xmin = 1
while :
    if int(0.08*xmin) == A and int(0.1*xmin) == B :
        print(int(xmin))
        exit()
    elif int(0.08*xmin) < A or int(0.1*xmin) < B :
        xmin += 1
    else :
        print(-1)
        exit()


Fixed submission:
import sys
input = sys.stdin.readline
A, B = map(int, input().split())

xmin = 1
while xmin :
    if int(0.08*xmin) == A and int(0.1*xmin) == B :
        print(int(xmin))
        exit()
    elif int(0.08*xmin) < A or int(0.1*xmin) < B :
        xmin += 1
    else :
        print(-1)
        exit()
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

ans = list(S)
rev = 0
head = []
foot = []
for i in range(Q) :
    query = input()
    if query == "1" :
        rev += 1
    else :
        a, F, C = query.split()
        if F == "1" and rev%2 == 0:
            head.insert(0, C)
        elif F == "1" and rev%2 == 1:
            foot.append(C)
        elif F == "2" and rev%2 == 0:
            foot.append(C)
        else :
            head.insert(0, C)

if rev%2 == 0 :
    print("".join(head + ans + foot))
else :
    print("".join((reversed(head + ans + foot))))

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

rev = 0
head = ""
foot = ""
for i in range(Q) :
    query = input()
    if query == "1" :
        rev += 1
    else :
        a, F, C = query.split()
        if F == "1" and rev%2 == 0:
            head = C + head
        elif F == "1" and rev%2 == 1:
            foot = foot + C
        elif F == "2" and rev%2 == 0:
            foot = foot + C
        else :
            head = C + head

if rev%2 == 0 :
    print("".join(list(head + S + foot)))
else :
    print("".join(list(reversed(head + S + foot))))

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

ans = list(S)
for i in range(Q) :
    query = input()
    if query == "1" :
        ans.reverse()
    else :
        a, F, C = query.split()
        if F == "1" :
            ans.insert(0, C)
        else :
            ans.append(C)

print("".join(ans))


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

rev = 0
head = ""
foot = ""
for i in range(Q) :
    query = input()
    if query == "1" :
        rev += 1
    else :
        a, F, C = query.split()
        if F == "1" and rev%2 == 0:
            head = C + head
        elif F == "1" and rev%2 == 1:
            foot = foot + C
        elif F == "2" and rev%2 == 0:
            foot = foot + C
        else :
            head = C + head

if rev%2 == 0 :
    print("".join(list(head + S + foot)))
else :
    print("".join(list(reversed(head + S + foot))))

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

for i in range(Q) :
    query = input()
    if query == "1" :
        S = "".join(list(reversed(S)))
    else :
        a, F, C = query.split()
        if F == "1" :
            S = C + S
        else :
            S = S + C

print(S)


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

rev = 0
head = ""
foot = ""
for i in range(Q) :
    query = input()
    if query == "1" :
        rev += 1
    else :
        a, F, C = query.split()
        if F == "1" and rev%2 == 0:
            head = C + head
        elif F == "1" and rev%2 == 1:
            foot = foot + C
        elif F == "2" and rev%2 == 0:
            foot = foot + C
        else :
            head = C + head

if rev%2 == 0 :
    print("".join(list(head + S + foot)))
else :
    print("".join(list(reversed(head + S + foot))))
Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

ans = 0
if P == 2 or P == 5 :
    for i in range(N) :
        if int(S[i])%P == 0 :
            ans += i+1
    print(ans)
    exit()

modP = [0 for _ in range(P)]
modP[0] = 1
for i in range(N) :
    modP[int(S[i:])%P] += 1

for r in modP :
    ans += r*(r-1)//2

print(ans)


Fixed submission:
N, P = map(int, input().split())
S = input()

ans = 0
if P == 2 or P == 5 :
    for i in range(N) :
        if int(S[i])%P == 0 :
            ans += i+1
    print(ans)
    exit()

modP = [0 for _ in range(P)]
modP[0] = 1
now = 0
t = 1
for i in range(N) :
    now += int(S[-1-i]) * t
    now %= P
    modP[now] += 1
    t = t * 10 % P

for r in modP :
    ans += r*(r-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

ans = 0
if P == 2 or P == 5 :
    for i in range(N) :
        if int(S[i])%P == 0 :
            ans += i+1
    print(ans)
    exit()

modP = [0 for _ in range(P)]
modP[0] = 1
for i in range(N) :
    modP[int(S[i:])%P] += 1

for r in modP :
    ans += r*(r-1)//2

print(ans)


Fixed submission:
N, P = map(int, input().split())
S = input()

ans = 0
if P == 2 or P == 5 :
    for i in range(N) :
        if int(S[i])%P == 0 :
            ans += i+1
    print(ans)
    exit()

modP = [0 for _ in range(P)]
modP[0] = 1
now = 0
t = 1
for i in range(N) :
    now += int(S[-1-i]) * t
    now %= P
    modP[now] += 1
    t = t * 10 % P

for r in modP :
    ans += r*(r-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

ans = 0
for i in range(N) :
    for j in range(i+1,N+1) :
        if int(S[i:j])%P == 0 :
            ans += 1

print(ans)


Fixed submission:
N, P = map(int, input().split())
S = input()

ans = 0
if P == 2 or P == 5 :
    for i in range(N) :
        if int(S[i])%P == 0 :
            ans += i+1
    print(ans)
    exit()

modP = [0 for _ in range(P)]
modP[0] = 1
now = 0
t = 1
for i in range(N) :
    now += int(S[-1-i]) * t
    now %= P
    modP[now] += 1
    t = t * 10 % P

for r in modP :
    ans += r*(r-1)//2

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
list_S = [input() for i in range(N)]

from collections import Counter
dict_C = Counter(list_S)

keys = [k for k,v in dict_C.items() if v == max(dict_C.values())]
keys.sort()

for ans in keys :
    print(ans)


Fixed submission:
N = int(input())
list_S = [input() for i in range(N)]

from collections import Counter
dict_C = Counter(list_S)
max = max(dict_C.values())

keys = [k for k,v in dict_C.items() if v == max]
keys.sort()

for ans in keys :
    print(ans)