Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N = int(input())
arr = list(map(int, input().split()))
memo = [0]*((10**9)*2)
ans = 0
for i in range(N):
    if (i+1)-arr[i] >= 0:
        ans += memo[(i+1)-arr[i]]
    if (i+1)+arr[i] > 0:
        memo[(i+1)+arr[i]] += 1
print(ans)

Fixed submission:
from collections import defaultdict
d = defaultdict(int)
N = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += d[(i+1)-arr[i]]
    d[(i+1)+arr[i]] += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
arr = list(map(int, input().split()))
memo = [0]*((max(arr)+N)*2)
ans = 0
for i in range(N):
    if (i+1)-arr[i] >= 0:
        ans += memo[(i+1)-arr[i]]
    if (i+1)+arr[i] > 0:
        memo[(i+1)+arr[i]] += 1
print(ans)

Fixed submission:
from collections import defaultdict
d = defaultdict(int)
N = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += d[(i+1)-arr[i]]
    d[(i+1)+arr[i]] += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
arr = list(map(int, input().split()))
memo = [0]*(max(arr)*2+1000)
ans = 0
for i in range(N):
    if (i+1)-arr[i] >= 0:
        ans += memo[(i+1)-arr[i]]
    if (i+1)+arr[i] > 0:
        memo[(i+1)+arr[i]] += 1
print(ans)

Fixed submission:
from collections import defaultdict
d = defaultdict(int)
N = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += d[(i+1)-arr[i]]
    d[(i+1)+arr[i]] += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
arr = list(map(int, input().split()))
memo = [0]*(max(arr)*2)
ans = 0
for i in range(N):
    if (i+1)-arr[i] >= 0:
        ans += memo[(i+1)-arr[i]]
    if (i+1)+arr[i] > 0:
        memo[(i+1)+arr[i]] += 1
print(ans)

Fixed submission:
from collections import defaultdict
d = defaultdict(int)
N = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += d[(i+1)-arr[i]]
    d[(i+1)+arr[i]] += 1
print(ans)

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

Fixed submission:
from collections import defaultdict
d = defaultdict(int)
N = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += d[(i+1)-arr[i]]
    d[(i+1)+arr[i]] += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N):
    for k in range(i+A[i]+1,N):
        if k - i == A[i] + A[k]:
            ans += 1
print(ans)

Fixed submission:
from collections import defaultdict
d = defaultdict(int)
N = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += d[(i+1)-arr[i]]
    d[(i+1)+arr[i]] += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)
S = input()[::-1]
N = len(S)
cnt = [0]*(N+1)
ans = 0
tmp = ''
for i in range(N):
    tmp = S[i] + tmp
    cnt[i] = int(tmp)%2019
cnt2 = [0]*2020
for i in cnt:
    cnt2[i] += 1
for i in cnt2:
    if i >= 2:
        ans += C(i,2)
print(ans)

Fixed submission:
import math
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)
S = input()[::-1]
N = len(S)
cnt = [0]*(N+1)
ans = 0
keta = 1
for i in range(N):
    cnt[i] = (cnt[i-1] + int(S[i])*keta)%2019
    keta *= 10
    keta %= 2019
cnt2 = [0]*2020
for i in cnt:
    cnt2[i] += 1
for i in cnt2:
    if i >= 2:
        ans += C(i,2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)
S = input()[::-1]
N = len(S)
cnt = [0]*(N+1)
ans = 0
keta = 1
for i in range(N):
    cnt[i] = (cnt[i-1] + int(S[i])*keta)%2019
    keta *= 10
cnt2 = [0]*2020
for i in cnt:
    cnt2[i] += 1
for i in cnt2:
    if i >= 2:
        ans += C(i,2)
print(ans)

Fixed submission:
import math
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)
S = input()[::-1]
N = len(S)
cnt = [0]*(N+1)
ans = 0
keta = 1
for i in range(N):
    cnt[i] = (cnt[i-1] + int(S[i])*keta)%2019
    keta *= 10
    keta %= 2019
cnt2 = [0]*2020
for i in cnt:
    cnt2[i] += 1
for i in cnt2:
    if i >= 2:
        ans += C(i,2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
cnt = [0]*N
ans = 0
for i in range(N):
    cnt = [0]*N
    for k in range(i,N):
        cnt[k] = (cnt[k-1]*10 + int(S[k]))%2019
        if cnt[k] == 0:
            ans += 1
            break
print(ans)

Fixed submission:
import math
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)
S = input()[::-1]
N = len(S)
cnt = [0]*(N+1)
ans = 0
keta = 1
for i in range(N):
    cnt[i] = (cnt[i-1] + int(S[i])*keta)%2019
    keta *= 10
    keta %= 2019
cnt2 = [0]*2020
for i in cnt:
    cnt2[i] += 1
for i in cnt2:
    if i >= 2:
        ans += C(i,2)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
R = [0]*N
G = [0]*N
B = [0]*N
ans = 0
for i in range(N):
    R[i] = R[i-1]
    G[i] = G[i-1]
    B[i] = B[i-1]
    if S[i] == 'R':
        R[i] += 1
    elif S[i] == 'G':
        G[i] += 1
    else:
        B[i] += 1
for i in range(N):
    for k in range(i,N):
        if (S[i] != S[k]) and (S[i] != 'R' and S[k] != 'R'):
            ans += R[N-1] - R[k]
            if k+(k-i) < N and S[k+(k-i)] == 'R':
                ans -= 1
        if (S[i] != S[k]) and (S[i] != 'G' and S[k] != 'G'):
            ans += G[N-1] - G[k]
            if k+(k-i) < N and S[k+(k-i)] == 'G':
                ans -= 1
        if (S[i] != S[k]) and (S[i] != 'B' and S[k] != 'B'):
            ans += B[N-1] - B[k]
            if k+(k-i) < N and S[k+(k-i)] == 'B':
                ans -= 1
print(ans)

Fixed submission:
N = int(input())
S = input()
R = [0]*N
G = [0]*N
B = [0]*N
ans = 0
for i in range(N):
    R[i] = R[i-1]
    G[i] = G[i-1]
    B[i] = B[i-1]
    if S[i] == 'R':
        R[i] += 1
    elif S[i] == 'G':
        G[i] += 1
    else:
        B[i] += 1
for i in range(N):
    for k in range(i,N):
        if (S[i] != S[k]) and (S[i] != 'R' and S[k] != 'R'):
            ans += R[N-1] - R[k]
            if k+(k-i) < N and S[k+(k-i)] == 'R':
                ans -= 1
        if (S[i] != S[k]) and (S[i] != 'G' and S[k] != 'G'):
            ans += G[N-1] - G[k]
            if k+(k-i) < N and S[k+(k-i)] == 'G':
                ans -= 1
        if (S[i] != S[k]) and (S[i] != 'B' and S[k] != 'B'):
            ans += B[N-1] - B[k]
            if k+(k-i) < N and S[k+(k-i)] == 'B':
                ans -= 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
K, N = map(int, input().split())
arr = list(map(int, input().split()))
dist = [K-arr[N-1]+arr[0]]
for i in range(1,N):
    dist.append(arr[i]-arr[i-1])
ans = sum(dist)
for i in range(N):
    ans = min(ans,sum(dist)-dist[i])
print(ans)

Fixed submission:
K, N = map(int, input().split())
arr = list(map(int, input().split()))
dist = [K-arr[N-1]+arr[0]]
for i in range(1,N):
    dist.append(arr[i]-arr[i-1])
tmp = sum(dist)
ans = sum(dist)
for i in range(N):
    ans = min(ans,tmp-dist[i])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, X, Y = map(int, input().split())
dist = [0]*N
for i in range(N):
    for k in range(i,N):
        if i == X - 1 and k == Y - 1:
            dist[1] += 1
            continue
        if X - 1 <= k <= Y - 1 and i <= X - 1:
            tmp = min(k-i,(X-1)-i+1+(Y-1-k))
            dist[tmp] += 1
            continue
        if X - 1 < i < Y - 1 and k >= Y - 1:
            tmp = min(k-i,i-(X-1)+1+k-(Y-1))
            dist[tmp] += 1
            continue
        if X - 1 <= k <= Y - 1 and X - 1 <= i <= Y - 1:
            tmp = min(k-i,i-(X-1)+1+(Y-1-k))
            dist[tmp] += 1
            continue
        if i <= X - 1 and k >= Y - 1:
            dist[k-i-(Y-X-1)] += 1
            continue
        dist[k-i] += 1
for i in range(1,N):
    print(dist[i])

Fixed submission:
N, X, Y = map(int, input().split())
dist = [0]*N
for i in range(N):
    for k in range(i+1,N):
        if i == X - 1 and k == Y - 1:
            dist[1] += 1
            continue
        if X - 1 <= k <= Y - 1 and i <= X - 1:
            tmp = min(k-i,(X-1)-i+1+(Y-1-k))
            dist[tmp] += 1
            continue
        if X - 1 < i < Y - 1 and k >= Y - 1:
            tmp = min(k-i,i-(X-1)+1+k-(Y-1))
            dist[tmp] += 1
            continue
        if X - 1 <= k <= Y - 1 and X - 1 <= i <= Y - 1:
            tmp = min(k-i,i-(X-1)+1+(Y-1-k))
            dist[tmp] += 1
            continue
        if i <= X - 1 and k >= Y - 1:
            dist[k-i-(Y-X-1)] += 1
            continue
        dist[k-i] += 1
for i in range(1,N):
    print(dist[i])
Buggy python submission (verdict: Runtime Error):
from decimal import *
getcontext().prec = ２8
a, b ,c = map(int, input().split())
if Decimal(a).sqrt() + Decimal(b).sqrt() < Decimal(c).sqrt():
    print('Yes')
else:
    print('No')

Fixed submission:
from decimal import *
getcontext().prec = 50
a, b ,c = map(int, input().split())
if Decimal(a).sqrt() + Decimal(b).sqrt() < Decimal(c).sqrt():
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
H, N = map(int, input().split())
a, b = [0]*N, [0]*N
for i in range(N):
    a[i], b[i] = map(int, input().split())
dp = [[float('inf')] * (H+1) for _ in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
for i in range(N):
    for j in range(H+1):
        if j - a[i] <= 0:
            dp[i+1][j] = min(dp[i][j],b[i])
        else:
            dp[i+1][j] = min(dp[i][j],dp[i+1][j-a[i]]+b[i])
print(dp[N][H])

Fixed submission:
H, N = map(int, input().split())
a, b = [0]*N, [0]*N
for i in range(N):
    a[i], b[i] = map(int, input().split())
dp = [[float('inf')] * (H+1) for _ in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
for i in range(N):
    for j in range(H+1):
        if j - a[i] <= 0:
            dp[i+1][j] = min(dp[i][j],b[i])
        else:
            dp[i+1][j] = min(dp[i][j],dp[i+1][j-a[i]]+b[i])
print(dp[N][H])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(1000):
    tmp = str(i).zfill(3)
    count = 0
    for k in S:
        if k == tmp[count]:
            count += 1
        if 3 == count:
            ans += 1
            break
print(ans)

Fixed submission:
N = int(input())
S = input()
ans = 0
for i in range(1000):
    tmp = str(i)
    while len(tmp) < 3:
        tmp = '0' + tmp
    count = 0
    target = S
    for i in tmp:
        target = target.replace(i,'')
    for k in S:
        if 3 <= count:
            break
        if k == tmp[count]:
            count += 1
    if count == 3:
        ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(1000):
    tmp = str(i)
    while len(tmp) < 3:
        tmp = '0' + tmp
    count = 0
    for k in S:
        if 3 <= count:
            break
        if k == tmp[count]:
            count += 1
    if count == 3:
        ans += 1
print(ans)

Fixed submission:
N = int(input())
S = input()
ans = 0
for i in range(1000):
    tmp = str(i)
    while len(tmp) < 3:
        tmp = '0' + tmp
    count = 0
    target = S
    for i in tmp:
        target = target.replace(i,'')
    for k in S:
        if 3 <= count:
            break
        if k == tmp[count]:
            count += 1
    if count == 3:
        ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(1000):
    tmp = str(i)
    while len(tmp) < 3:
        tmp = '0' + tmp
    count = 0
    for k in S:
        if count < 3 and k == tmp[count]:
            count += 1
    if count == 3:
        ans += 1
print(ans)

Fixed submission:
N = int(input())
S = input()
ans = 0
for i in range(1000):
    tmp = str(i)
    while len(tmp) < 3:
        tmp = '0' + tmp
    count = 0
    target = S
    for i in tmp:
        target = target.replace(i,'')
    for k in S:
        if 3 <= count:
            break
        if k == tmp[count]:
            count += 1
    if count == 3:
        ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
H, W, K = map(int, input().split())
cake = []
for _ in range(W):
    cake.append(input())
ans = []
count = 1
flg = False
for i in range(H):
    tmp = []
    for k in range(W):
        if flg and cake[i][k] == '#':
            count += 1
            tmp.append(count)
            flg = False
            continue
        if cake[i][k] == '#':
            flg = True
            tmp.append(count)
        else:
            tmp.append(count)
    flg = False
    count += 1
    ans.append(tmp)
for i in ans:
    for k in i:
        print(k,end=" ")
    print()

Fixed submission:
H, W, K = map(int, input().split())
grid = [input() for _ in range(H)]
ans = [[0]*W for _ in range(H)]
empty_index = [False]*H
first_flg = False
cnt = 0
for i in range(H):
    if '#' not in grid[i]:
        empty_index[i] = True
for i in range(H):
    if not empty_index[i] and not first_flg:
        first_flg = True
        first_index = i
    if not first_flg:
        continue
    if empty_index[i]:
        for k in range(W):
            ans[i][k] = ans[i-1][k]
    else:
        cnt += 1
        flg = False
        for k in range(W):
            if grid[i][k] == '#':
                if not flg:
                    flg = True
                else:
                    cnt += 1
            ans[i][k] = cnt
for i in range(H):
    if not empty_index[i]:
        break
    for k in range(W):
        ans[i][k] = ans[first_index][k]
for i in ans:
    print(*i)
Buggy python submission (verdict: Runtime Error):
import math
mod = 10**9 + 7
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)
X, Y = map(int,input().split())
if (X+Y) % 3 != 0:
    print(0)
    exit()
n = (2*Y-X)//3
m = (2*X-Y)//3
print(C(n+m,m) % mod)

Fixed submission:
import math
def calc(x_i,x_j,y_i,y_j):
    tmp = ((x_i)-(x_j))**2
    tmp += ((y_i)-(y_j))**2
    return math.sqrt(tmp)
N = int(input())
array = []
for _ in range(N):
    array.append(list(map(int, input().split())))
sum = 0
check = []
kai = 1
for i in range(1,N+1):
    kai *= i
    if i == N - 1:
        tmp = kai
for i in range(N):
    for k in range(N):
        if k == i:
            continue
        sum += calc(array[i][0],array[k][0],array[i][1],array[k][1])
sum *= tmp
print(sum/kai)
Buggy python submission (verdict: Runtime Error):
import math
mod = 10**9 + 7
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)
X, Y = map(int,input().split())
if (X+Y) % 3 != 0:
    print(0)
    exit()
n = (2*Y-X)//3
m = (2*X-Y)//3
print(C(n+m,m) % mod)

Fixed submission:
MOD = 10**9 + 7
def comb(n, k, MOD):
    if n < k or n < 0 or k < 0:
        return 0
    k = min(k, n - k)
    if k == 0:
        return 1
    iinv = [1] * (k + 1)
    ans = n
    for i in range(2, k + 1):
        iinv[i] = MOD - iinv[MOD % i] * (MOD // i) % MOD
        ans *= (n + 1 - i) * iinv[i] % MOD
        ans %= MOD
    return ans
X, Y = map(int,input().split())
if (X+Y) % 3 != 0:
    print(0)
    exit()
n = (2*Y-X)//3
m = (2*X-Y)//3
print(comb(n+m,m,MOD))
Buggy python submission (verdict: Runtime Error):
N = int(input())
array = list(map(int, input().split()))
if array[0] == 1:
    print(0)
    exit()
array.sort()
index = array.index(2)
count = 1
count_old = array.count(1)
ans = 1
first = array[index]
for i in range(index,N-1):
    if first != array[i+1]:
        ans *= count_old ** count
        count_old = count
        count = 1
    else:
        count += 1
    first = array[i+1]
    ans %= 998244353
ans *= count_old ** count
ans %= 998244353
print(ans)

Fixed submission:
N = int(input())
array = list(map(int, input().split()))
if array[0] != 0:
    print(0)
    exit()
array.sort()
if 1 in array and len(set(array[1:])) == 1:
    print(1)
    exit()
if not (2 in array):
    print(0)
    exit()
if 0 in array[1:]:
    print(0)
    exit()
index = array.index(2)
count = 1
count_old = array.count(1)
ans = 1
first = array[index]
for i in range(index,N-1):
    if first != array[i+1]:
        if first != array[i+1] - 1:
            print(0)
            exit()
        ans *= count_old ** count
        count_old = count
        count = 1
    else:
        count += 1
    first = array[i+1]
    ans %= 998244353
ans *= count_old ** count
ans %= 998244353
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N = int(input())
arr = list(map(int, input().split()))
arr.sort()
ans = 0
for a in range(N):
    for b in range(a+1,N):
        for c in range(a):
            if arr[a] < arr[b] + arr[c] and arr[b] < arr[c] + arr[a] and arr[c] < arr[a] + arr[b]:
                ans += 1
print(ans)

Fixed submission:
import bisect
N = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
for i in range(N):
    for k in range(i+1,N):
        tmp = bisect.bisect_left(a,a[i]+a[k])
        ans += tmp - k - 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
array = list(map(int, input().split()))
array.sort()
ans = 0
for i in range(N):
    for k in range(i+1,N):
        for j in range(1,N):
            if (array[i] < array[k] + array[-j]) and (array[-j] < array[i] + array[k]) and (array[k] < array[i] + array[-j]):
                ans += N-j-k
                break
print(ans)


Fixed submission:
import bisect
N = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
for i in range(N):
    for k in range(i+1,N):
        tmp = bisect.bisect_left(a,a[i]+a[k])
        ans += tmp - k - 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
array = list(map(int, input().split()))
array.sort()
ans = 0
# print(array)
for i in range(N):
    for k in range(i+1,N):
        for j in range(k+1,N):
            if (array[i] < array[k] + array[j]) and (array[j] < array[i] + array[k]) and (array[k] < array[i] + array[j]):
                ans += 1
                # print(array[i],array[k],array[j])
            else:
                break
print(ans)

Fixed submission:
import bisect
N = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
for i in range(N):
    for k in range(i+1,N):
        tmp = bisect.bisect_left(a,a[i]+a[k])
        ans += tmp - k - 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
array = list(map(int, input().split()))
ans = 0
for i in range(N):
    for k in range(i,N):
        for j in range(k,N):
            if i != k and k != j and j != i:
                if (array[i] < array[k] + array[j]) and (array[j] < array[i] + array[k]) and (array[k] < array[i] + array[j]):
                    ans += 1
print(ans)

Fixed submission:
import bisect
N = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
for i in range(N):
    for k in range(i+1,N):
        tmp = bisect.bisect_left(a,a[i]+a[k])
        ans += tmp - k - 1
print(ans)
Buggy python submission (verdict: Runtime Error):
S = input()
K = int(input())
N = len(S)
cnt = 0
flg1 = False
flg2 = False
flg3 = False
flg4 = False
if N == 2:
    a = S[10000]
for i in range(N-1):
    if S[i] != S[i+1]:
        flg3 = True
        break
if (flg3 == False) and N % 2 == 1:
    print((N*K)//2)
    exit()
if (S[0] == S[N-1]) and K != 1:
    if S[0] != S[1]:
        cnt += 1
        flg1 = True
if (S[N-2] == S[N-1]):
    flg4 = True
for i in range(N-1):
    if flg2:
        flg2 = False
        continue
    if (i == N - 2) and flg1:
        break
    if S[i] == S[i+1]:
        cnt += 1
        flg2 = True
if flg1 and flg4 == False:
    print(cnt*K-1)
else:
    print(cnt*K)

Fixed submission:
def f(s,n):
    res = 0
    flg = False
    for i in range(1,n):
        if flg:
            flg = False
            continue
        if S[i] == S[i-1]:
            flg = True
            res += 1
    return res
S = input()
K = int(input())
if len(set(list(S))) == 1:
    print(len(S)*K//2)
    exit()
ans = f(S,len(S))
S = S * 2
print(ans + (f(S,len(S)) - ans) * (K-1))

Buggy python submission (verdict: Runtime Error):
S = input()
K = int(input())
N = len(S)
cnt = 0
flg1 = False
flg2 = False
flg3 = False
flg4 = False
flg5 = False
for i in range(N-1):
    if S[i] != S[i+1]:
        flg3 = True
        break
if (flg3 == False) and N % 2 == 1:
    print((N*K)//2)
    exit()


if (S[0] == S[N-1]) and K != 1:
    if S[0] != S[1]:
        cnt += 1
        flg1 = True
        flg5 = True
    if (S[1] == S[2]) and (2 < N) and (flg5 == False):
        cnt += 1
        flg1 = True
if (S[N-2] == S[N-1]):
    flg4 = True

for i in range(N-1):
    if flg2:
        flg2 = False
        continue
    if (i == N - 2) and flg1:
        break
    if S[i] == S[i+1]:
        cnt += 1
        flg2 = True
if flg1 and flg4 == False:
    print(cnt*K-1)
else:
    print(cnt*K)

Fixed submission:
def f(s,n):
    res = 0
    flg = False
    for i in range(1,n):
        if flg:
            flg = False
            continue
        if S[i] == S[i-1]:
            flg = True
            res += 1
    return res
S = input()
K = int(input())
if len(set(list(S))) == 1:
    print(len(S)*K//2)
    exit()
ans = f(S,len(S))
S = S * 2
print(ans + (f(S,len(S)) - ans) * (K-1))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))
dic = {}
for i in range(N):
    dic[a.index(a[i])+1] = a[i]
dic_sorted = sorted(dic.items(), key=lambda x:x[1])
for i in range(N):
    print(dic_sorted[i][0],'',end="")

Fixed submission:
N = int(input())
a = list(map(int, input().split()))
dic = {}
for i in range(N):
    dic[i+1] = a[i]
dic_sorted = sorted(dic.items(), key=lambda x:x[1])
for i in range(N):
    print(dic_sorted[i][0],'',end="")