Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
P = tuple(map(int, input().split()))
cnt = N
for a in P:
    for b in P[:P.index(a)+1]:
        if a > b:
            cnt -= 1
            break
print(cnt)

Fixed submission:
N = int(input())
P = list(map(int, input().split()))
cnt = 0
max = 2*10**5
for a in P:
    if a <=max:
        #print(a)
        cnt += 1
    max = min(max,a)
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
P = list(map(int, input().split()))
cnt = N
for a in P:
    for b in P[:P.index(a)+1]:
        if a > b:
            cnt -=1
            break
print(cnt)

Fixed submission:
N = int(input())
P = list(map(int, input().split()))
cnt = 0
max = 2*10**5
for a in P:
    if a <=max:
        #print(a)
        cnt += 1
    max = min(max,a)
print(cnt)
Buggy python submission (verdict: Runtime Error):
N, K, S = map(int, input().split())

l = [S] + [0]*(K-1) + [S%10**9]*(N-K) #長さKで要素が全てSの列（条件満たすK通り）と長さNーKの適当な列で長さN列を生成

print(*l) #*lは標準出力時にはいれつの[]を出力しないため


Fixed submission:
H= int(input())
W= int(input())
N= int(input())

import math

u = N/max(H,W)

print(math.ceil(u))
Buggy python submission (verdict: Runtime Error):
import math    
A, B = map(int, input().split())
#最小公倍数least common multiple (lcm)を定義する
def lcm(A,B):
    return (A*B)// math.gcd(A, B)

print(lcm(A,B))
    


Fixed submission:
a, b = map(int, input().split())
#greatest commmon divisor(gdc)を定義する
def gcd(a, b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b
#least common multiple (lcm)を定義する
def lcm(a,b):
    return (a*b)// gcd(a, b)

print(lcm(a,b))
Buggy python submission (verdict: Runtime Error):
A, B, C = map(int, input().split())

if sum(A, B, C)<=22:
	print("bust")
else:
	print("win")

Fixed submission:
A, B, C = map(int, input().split())

if A + B + C>=22:
	print("bust")
else:
	print("win")
Buggy python submission (verdict: Runtime Error):
N, M, X, Y = map(int, input().split())
 
x = list(map(int, input().split()))
y = list(map(int, input().split()))    
 
for z in range(-100, 101):
    if (X < z) and (max(x) < z) and (z <= min(y)) and (z <= Y):
        print("No War")
    else:
        print("War")

Fixed submission:
a, b, c = (int(x) for x in input().split())
print(a + b + c + max(a, b, c)*9)

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

x = list(map(int, input().split()))
y = list(map(int, input().split()))    
for z in range(X+1, Y+1):
    if (max(x) < z) and (z <= min(y)):
        print("No War")
        break
    else:
        print("War")

Fixed submission:
a, b, c = (int(x) for x in input().split())
print(a + b + c + max(a, b, c)*9)

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

x = list(map(int, input().split()))
y = list(map(int, input().split()))    

for z in range(-100, 101):
    if (X < z) and (max(x) < z) and (z <= min(y) and (z <= Y):
        print("No War")
    else:
        print("War")

Fixed submission:
a, b, c = (int(x) for x in input().split())
print(a + b + c + max(a, b, c)*9)

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

x = list(map(int, input().split()))
y = list(map(int, input().split()))    

for z in range(-100, 101):
    if (X < z) and (max(x) < z) and (min(y) >= z) and (Y >= z):
        print("No War")
    else:
        print("War")

Fixed submission:
a, b, c = (int(x) for x in input().split())
print(a + b + c + max(a, b, c)*9)

Buggy python submission (verdict: Runtime Error):
A = int(input("A =",))
B = int(input("B =",))
C = int(input("C =",))

if A == max(A, B, C):
    x = 10*A + B + C
    print("お小遣いは", x)
elif B == max(A, B, C):
    x = A + 10*B + C
    print("お小遣いは", x)
elif C == max(A, B, C):
    x = A + B + 10*C
    print("お小遣いは", x)


Fixed submission:
a, b, c = (int(x) for x in input().split())
print(a + b + c + max(a, b, c)*9)
Buggy python submission (verdict: Runtime Error):
N, M, X, Y = map(int, input().split())
 
x = list(map(int, input().split()))
y = list(map(int, input().split()))    
 
for z in range(-100, 101):
    if (X < z) and (max(x) < z) and (z <= min(y) and (z <= Y):
        print("No War")
    else:
        print("War")

Fixed submission:
N, M, X, Y = map(int, input().split())
 
x = list(map(int, input().split()))
y = list(map(int, input().split()))    

if (X < max(x)) and (max(x) < min(y)) and (min(y) <= Y):
    print("No War")
else:
    print("War")
Buggy python submission (verdict: Runtime Error):
K = int(input())

if K%2==0:
	print((K/2)*(K/2))
else:
	print((k-1)*(k-1)/4)

Fixed submission:
K = int(input())

if K%2==0:
	print(round((K/2)*(K/2)))
else:
	print(round((K-1)*(K+1)/4))
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
l=list(map(int,input().split()))
m=1
maxx=10**18
flag=0
for i in range(n):
    m*=l[i]
    if m==0:
        print(0)
        flag=1
        break
if flag!=1:
    if m>maxx:
        print(-1)
    else:
        print(m)


Fixed submission:
n=int(input())
l=list(map(int,input().split()))
m=1
maxx=10**18
flag=0
for i in range(n):
   if l[i]==0:
       print(0)
       flag=1
       break
if flag!=1:
    flag=0
    for i in range(n):
        m*=l[i]
        if m>maxx:
            print(-1)
            flag=1
            break

    if flag!=1:
        print(m)
Buggy python submission (verdict: Runtime Error):
m,d=map(int,input().split())
count=0
for i in range(1,m+1):
	for j in range(1,d+1):
		if d>=10:
			d1=int(str(d)[-1])
    		d2=int(str(d)[0])
    		if d1>=2 and d2>=2 and d1*d2==m:
      			count+=1
print(count)

Fixed submission:
m,d=map(int,input().split())
count=0
for i in range(1,m+1):
    for j in range(1,d+1):
        if j>=10:
            d1=int(str(j)[-1])
            d2=int(str(j)[0])
            if d1>=2 and d2>=2 and d1*d2==i:
                count+=1
print(count)

Buggy python submission (verdict: Runtime Error):
m,d=map(int,input().split())
count=0
for i in range(1,m+1):
  for j in range(1,d+1):
    d1=int(str(d)[-1])
    d2>=2
    if d>=10 and d1>=2 and d2>=2 and d1*d2==m:
      count+=1
print(count)

Fixed submission:
m,d=map(int,input().split())
count=0
for i in range(1,m+1):
    for j in range(1,d+1):
        if j>=10:
            d1=int(str(j)[-1])
            d2=int(str(j)[0])
            if d1>=2 and d2>=2 and d1*d2==i:
                count+=1
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
	return sys.stdin.readline().strip()
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[sys.maxsize]*n
dp[0]=0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j<n:
            dp[j]=min(dp[j], dp[i]+ abs(l[i]-l[j]))


print(dp[-1])
    
    


Fixed submission:
import sys
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[sys.maxsize]*n
dp[0]=0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j<n:
            dp[j]=min(dp[j], dp[i]+ abs(l[i]-l[j]))


print(dp[-1])

Buggy python submission (verdict: Runtime Error):
import sys
def input():
	sys.stdin.readline().strip()
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[sys.maxsize]*n
dp[0]=0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j<n:
            dp[j]=min(dp[j], dp[i]+ abs(l[i]-l[j]))


print(dp[-1])
    
    


Fixed submission:
import sys
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[sys.maxsize]*n
dp[0]=0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j<n:
            dp[j]=min(dp[j], dp[i]+ abs(l[i]-l[j]))


print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[sys.maxsize]*n
dp[0]=0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j<n:
            dp[j]=min(dp[j], dp[i]+ abs(l[i]-l[j]))


print(dp[-1])
    
    


Fixed submission:
import sys
n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[sys.maxsize]*n
dp[0]=0
for i in range(n):
    for j in range(i+1,i+k+1):
        if j<n:
            dp[j]=min(dp[j], dp[i]+ abs(l[i]-l[j]))


print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
def knapsack(n,w,wts,vals):
    if n==0 or w==0:
        return 0
    inc,exc=-1,-1
    if wts[n-1]<=w:
        inc= vals[n-1]+ knapsack(n-1,w-wts[n-1],wts,vals)
    exc= knapsack(n-1,w,wts,vals)

    return max(inc,exc)

n,w=map(int,input().split())
wts,vals=[],[]
for i in range(n):
    a,b=map(int,input().split())
    wts.append(a)
    vals.append(b)

print(knapsack(n,w,wts,vals))


Fixed submission:
def knapsack_dp(n,w,wts,val):
    dp=[[0]*(w+1) for i in range(n+1)]

    for i in range(n+1):
        for j in range(w+1):
            if i==0 or j==0:
                dp[i][j]=0
            elif wts[i-1]<=j:
                dp[i][j]=max(dp[i-1][j],val[i-1]+dp[i-1][j-wts[i-1]])
            else:
                dp[i][j]=dp[i-1][j]

    return dp[n][w]


n,w=map(int,input().split())
wts,vals=[],[]
for i in range(n):
    a,b=map(int,input().split())
    wts.append(a)
    vals.append(b)

print(knapsack_dp(n,w,wts,vals))
Buggy python submission (verdict: Runtime Error):
 = int(input())
A = [int(x) for x in input().split()]

reg = A[0]
flg=True
if 0 in A:
    print(0)
    flg=False
    reg=0
for i in range(1, N):
    reg *= A[i]
    if reg>10**18:
        print(-1)
        flg=False
        break
if flg:
    print(reg)

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

reg = A[0]
flg=True
if 0 in A:
    print(0)
    flg=False
    reg=0
for i in range(1, N):
    reg *= A[i]
    if reg>10**18:
        print(-1)
        flg=False
        break
if flg:
    print(reg)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = [int(x) for x in input().split()]

reg = A[0]
flg=True
if 0 in A:
    print(0)
    flg=False
    reg=0
for i in range(1, N):
    reg *= a
    if reg>10**18:
        print(-1)
        flg=False
        break
if flg:
    print(reg)

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

reg = A[0]
flg=True
if 0 in A:
    print(0)
    flg=False
    reg=0
for i in range(1, N):
    reg *= A[i]
    if reg>10**18:
        print(-1)
        flg=False
        break
if flg:
    print(reg)
Buggy python submission (verdict: Runtime Error):
import numpy as np
import collections
N=int(raw_input())
A=map(int, raw_input().split())
count_A=collections.Counter(A).most_common(1)

cnt = 0
if N%2 == 0:
    if A.count(0) >= 1:
        print 0
        cnt = 1
    elif count_A [0][1] != 2:
        print 0
        cnt = 1
    if cnt == 0:
        print 2**(N/2)%(10**9+7)
else:
    if A.count(0) >= 2:
        print 0
        cnt = 1
    elif count_A[0][1] != 2:
        print 0
        cnt = 1
    if cnt == 0:
        print 2**(N/2)%(10**9+7)

Fixed submission:
str = raw_input().split()
print int(str[0]) + int(str[2]) if str[1] == "+" else int(str[0]) - int(str[2])
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N=int(raw_input())
A=map(int, raw_input().split())


cnt = 0
if N%2 == 0:
    if A.count(0) >= 1:
        print 0
        cnt = 1
    else:
        for i in np.arange(1, N, 2):
            if A.count(i) != 2:
                print 0
                cnt = 1
                break
    if cnt == 0:
        print 2**(N/2)%(10**9+7)
else:
    if A.count(0) >= 2:
        print 0
        cnt = 1
    else:
        for i in np.arange(2, N, 2):
            if A.count(i) != 2:
                print 0
                cnt = 1
                break
    if cnt == 0:
        print 2**(N/2)%(10**9+7)

Fixed submission:
import collections
N=int(raw_input())
A=map(int, raw_input().split())
count_A=collections.Counter(A).most_common(1)
cnt = 0
if N == 1:
    if A[0] == 0:
        print 1
    else:
        print 0
elif N%2 == 0:
    if A.count(0) != 0:
        print 0
        cnt = 1
    elif count_A[0][1] != 2:
        print 0
        cnt = 1
    if cnt == 0:
        print (2**(N/2)) % (10**9+7)
else:
    if A.count(0) != 1:
        print 0
        cnt = 1
    elif count_A[0][1] != 2:
        print 0
        cnt = 1
    if cnt == 0:
        print (2**(N/2)) % (10**9+7)
Buggy python submission (verdict: Time Limit Exceeded):
from math import gcd
n = int(input())
ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
            if i==j and j==k: ans+=i
            else:
                tmp = gcd(k, gcd(i,j))
                ans+=tmp
print(ans)

Fixed submission:
from math import gcd
n = int(input())
ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
            if i==j and j==k: ans+=i
            else:
                tmp = gcd(k, gcd(i,j))
                ans+=tmp
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from math import gcd
n = int(input())
ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
            a = list(set([i,j,k]))
            if len(a)==1: ans+=a[0]
            elif len(a)==2:
                a = gcd(max(a), min(a))
                ans+=a
            else:
                tmp = gcd(i,j)
                tmp = gcd(tmp, k)
                ans+=tmp
print(ans)

Fixed submission:
from math import gcd
n = int(input())
ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
            if i==j and j==k: ans+=i
            else:
                tmp = gcd(k, gcd(i,j))
                ans+=tmp
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from math import gcd
n = int(input())
ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
            a = gcd(i,j)
            ans+=gcd(a, k)
print(ans)


Fixed submission:
from math import gcd
n = int(input())
ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
            if i==j and j==k: ans+=i
            else:
                tmp = gcd(k, gcd(i,j))
                ans+=tmp
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math
def com_cnt(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = list(map(int,input().split()))
l = [0]*(N+1)
for a in A:
    l[a] += 1
for i in A:
    ans = 0
    l[i]-=1
    for j in l:
        if j>1:
            ans+=com_cnt(j, 2)
    print(ans)
    l[i]+=1


Fixed submission:
from collections import Counter as C
N = int(input())
A = list(map(int,input().split()))
c = C(A)
s = 0
for i in c.values():
    s += i*(i-1)//2
for j in A:
    print(s-(c[j]-1))
Buggy python submission (verdict: Runtime Error):
from collections import deque
s = deque(input().replace('\n', ''))
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s.append(query[2])
        else:
            s.appendleft(query[2])
if not flag:
    s = s[::-1]

print(''.join(s))


Fixed submission:
from collections import deque
s = deque(input().replace('\n', ''))
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s.append(query[2])
        else:
            s.appendleft(query[2])
s = ''.join(s)
print(s if flag else s[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
s = input().replace('\n', '')
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s += query[2]
        else:
            s = query[2] + s
print(s if flag else s[::-1])


Fixed submission:
from collections import deque
s = deque(input().replace('\n', ''))
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s.append(query[2])
        else:
            s.appendleft(query[2])
s = ''.join(s)
print(s if flag else s[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
s = input().replace('\n', '')
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s += query[2]
        else:
            s = query[2] + s
print(s if flag else s[::-1])


Fixed submission:
from collections import deque
s = deque(input().replace('\n', ''))
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s.append(query[2])
        else:
            s.appendleft(query[2])
s = ''.join(s)
print(s if flag else s[::-1])
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
A = list(map(int, input().split()))
B = [0]*1001
for i in range(1, 1001):
    B[i] = (1+i)/2
C = [B[A[j]] for j in range(n)]
ans = []
for i in range(n-k+1):
    ans.append(sum(C[i:i+k]))
print(max(ans))


Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
B = [(1+i)/2 for i in A]
ans = sum(B[:k])
t = ans
for i in range(n-k):
    t-=B[i]
    t+=B[i+k]
    if t>ans: ans=t
print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int, input().split()))
B = [0]*1001
B[1] = 1
for i in range(2, 1001):
    B[i] = B[i-1] + 0.5
ans = 0
for i in range(n-(n-k)):
    t = 0
    for j in range(k):
        t+=B[A[i+j]]
    ans = max(ans, t)
print(ans)


Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
B = [(1+i)/2 for i in A]
ans = sum(B[:k])
t = ans
for i in range(n-k):
    t-=B[i]
    t+=B[i+k]
    if t>ans: ans=t
print(ans)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
A = list(map(int, input().split()))
B = [0]*1001
for i in range(1, 1001):
    a = 0
    for j in range(1, i+1):
        a += j
    B[i] = a/i
ans = 0
for i in range(n-(n-m)):
    t = 0
    for j in range(m):
        t+=B[A[i+j]]
    ans = max(ans, t)
print(ans)


Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
B = [(1+i)/2 for i in A]
ans = sum(B[:k])
t = ans
for i in range(n-k):
    t-=B[i]
    t+=B[i+k]
    if t>ans: ans=t
print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
p = list(map(int, input().split()))
a, tmp = 0, 0
for i in range(n-2):
    if sum(p[i:i+k])>=tmp:
        tmp = sum(p[i:i+k])
        a = p[i:i+k]
ans = 0
b = [i for i in range(1001)]
for i in a:
    ans += sum(b[:i+1])/i
print(ans)


Fixed submission:
n, k = map(int, input().split())
A = list(map(int, input().split()))
B = [(1+i)/2 for i in A]
ans = sum(B[:k])
t = ans
for i in range(n-k):
    t-=B[i]
    t+=B[i+k]
    if t>ans: ans=t
print(ans)
Buggy python submission (verdict: Runtime Error):
x = int(input())

def get_numprime(n):
  nums = [i + 1 for i in range(2, n, 2)]
  prime = [2]
  while True:
    p = nums[0]
    if  p>=x:
      return p
    prime.append(p)
    nums = [e for e in nums if e%p !=0]

if x==2:
  print(x)
else:
  print(get_numprime(x+20))


Fixed submission:
import math
x = int(input())

def is_prime(x):
  for i in range(2, int(math.sqrt(x))+1):
    if x%i==0:
      return False
  return True

while not is_prime(x):
  x+=1
print(x)
Buggy python submission (verdict: Runtime Error):
n = int(input())
R = list(map(int, input().split()))
c, i = 0, 1
for r in R:
  if r==i: i+=1
  else: c+=1
print(c if c<N else '-1')

Fixed submission:
from collections import deque
n = int(input())
r = deque(map(int, input().split()))
i=0
while True:
    if max(len(r),i)==i:
        break
    if r[i]!=i+1:
        r.popleft()
    else:
        i+=1
print('-1' if len(r)==0 else n-len(r))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
cnt = 0
for i in range(1000):
    c = [str(i // 100), str((i // 10) % 10), str(i % 10)]
    t = 0
    for j in range(n):
        if s[j]==c[t]:
            t+=1
        if t==3:
            cnt+=1
            break
print(cnt)


Fixed submission:
n = int(input())
s = input()
cnt = 0
for i in range(1000):
    c = [str(i // 100), str((i // 10) % 10), str(i % 10)]
    t = 0
    for j in range(n):
        if s[j]==c[t]:
            t+=1
        if t==3:
            cnt+=1
            break
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n = int(input())
s = list(input())
print(len(set(itertools.combinations(s, 3))))

Fixed submission:
n = int(input())
s = input()
cnt = 0
for i in range(1000):
    c = [str(i // 100), str((i // 10) % 10), str(i % 10)]
    t = 0
    for j in range(n):
        if s[j]==c[t]:
            t+=1
        if t==3:
            cnt+=1
            break
print(cnt)
Buggy python submission (verdict: Runtime Error):
def cal(S):
    tmp, a, cnt = S[0], 1, 0
    flag = True
    for s in S[1:]:
        if S[0]!=s: flag=False
        if flag: a+=1
        if tmp[-1]==s:
            s = '*'
            ans += 1
        tmp += s
    return a, cnt


S = input().replace('\n', '')
k = int(input())
ans = 0
if len(list(set(S)))==1:
    ans = len(S)*k//2
else:
    a, cnt = cal(S)
    b, _ = cal(S[::-1])
    ans = cnt*k
    if S[0]==S[-1]:
        ans -= ((a//2)+(b//2)-((a+b)//2))*(k-1)
print(ans)

Fixed submission:
def cal(S):
    tmp, a, cnt = S[0], 1, 0
    flag = True
    for s in S[1:]:
        if S[0]!=s: flag=False
        if flag: a+=1
        if tmp[-1]==s:
            s = '*'
            cnt += 1
        tmp += s
    return a, cnt


S = input().replace('\n', '')
k = int(input())
ans = 0
if len(list(set(S)))==1:
    ans = len(S)*k//2
else:
    a, cnt = cal(S)
    b, _ = cal(S[::-1])
    ans = cnt*k
    if S[0]==S[-1]:
        ans -= ((a//2)+(b//2)-((a+b)//2))*(k-1)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, k, q = map(int,input().split())
p = np.array([k]).repeat(n)

for i in range(q):
  tmp = int(input())
  p-=1
  p[tmp-1]+=1

for j in range(n):
  print('Yes' if p[j]>=1 else 'No')

Fixed submission:
n, k, q = map(int,input().split())
p = [k-q]*n

for i in range(q):
  tmp = int(input())
  p[tmp-1]+=1

for ans in p:
  print('Yes' if ans>=1 else 'No')
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
strs = ["".join(sorted(list(input()))) for _ in range(n)]
cnt = 0
for i in range(n):
  for j in range(i+1, n):
    if strs[i]==strs[j]:
      cnt+=1
print(cnt)

Fixed submission:
n = int(input())
strs = ["".join(sorted(list(input()))) for _ in range(n)]
ans = {}
cnt = 0
for i in range(n):
  if strs[i] in ans:
    cnt += ans[strs[i]]
    ans[strs[i]] +=  1
  else:
    ans[strs[i]] = 1

print(cnt)