Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = list(map(int,input().split()))
t = []
for i in s:
  t.append(i)
  t = t[::-1]
print(*t)

Fixed submission:
n = int(input())
l = list(map(int,input().split()))
if n%2==0:
  a = l[1::2][::-1]
  b = l[::2]
  for i in a:
    print(i,end=' ')
  for j in b:
    print(j,end=' ')
  
else:
  a = l[0::2][::-1]
  b = l[1::2]
  for i in a:
    print(i,end=' ')
  for j in b:
    print(j,end=' ')
Buggy python submission (verdict: Runtime Error):
n = int(input())
t = []
for i in range(1,int(n**0.5)):
  if n%i==0:
    t.append(max(len(str(int(n/i))),len(str(i))))            
print(min(t))


Fixed submission:
n = int(input())
t = []
for i in range(1,int(n**0.5)+1):
  if n%i==0:
    t.append(max(len(str(int(n/i))),len(str(i))))            
print(min(t))
Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
list_A = list(input() for i in range(n))
list_B = list(input() for i in range(m))
t = "No"
for i in range(n-m+1):
  for t in range(n-m+1):
    if all(list_A[i][t:t+m+1]==list_B[i][t:t+m+1]):
      print("Yes")

Fixed submission:
n,m = map(int,input().split())
list_A = list(input() for i in range(n))
list_B = list(input() for i in range(m))
k = "No"
for i in range(n-m+1):
  for t in range(n-m+1):
      if all(list_A[v+i][t:t+m]==list_B[v] for v in range(m)):
        k = "Yes"
        break
print(k)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
list_a = []
list_b = []
su = 0
for i in range(n):
  a,b = map(int,input().split())
  list_a.append(a)
  list_b.append(b)
list_a = list_a[::-1]
list_b = list_b[::-1]
for i in range(n):
  if list_a[i]%list_b[i]==0:
    continue
  else:
    x = list_b[i]-(list_a[i]%list_b[i])
    su += x
    for j in range(i,n):
      list_a[j] += x
 # print(list_a)
print(su)


Fixed submission:
n = int(input())
list_a = []
list_b = []
su = 0
for i in range(n):
  a,b = map(int,input().split())
  list_a.append(a)
  list_b.append(b)
list_a = list_a[::-1]
list_b = list_b[::-1]
for i in range(n):
  list_a[i] += su
  if list_a[i]%list_b[i]==0:
    continue
  else:
    x = list_b[i]-(list_a[i]%list_b[i])
    su += x
#  print(list_a)
print(su)
Buggy python submission (verdict: Time Limit Exceeded):
k,s = map(int,input().split())
l = [a+b for a in range(k+1) for b in range(k+1)]
sum = 0
for i in range(k+1):
  sum += l.count(s-i)
print(sum)

Fixed submission:
k,s = map(int,input().split())
sum =0
for i in range(k+1):
  for l in range(k+1):
    if 0<=s-i-l<=k:
      sum += 1
print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
k,s = map(int,input().split())
l = [a+b+c for a in range(k+1) for b in range(k+1) for c in range(k+1)]
print(l.count(s))

Fixed submission:
k,s = map(int,input().split())
sum =0
for i in range(k+1):
  for l in range(k+1):
    if 0<=s-i-l<=k:
      sum += 1
print(sum)
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
l = [list(map(int,input().split())) for _ in range(m)]

dp = [[0 for _ in range(n)] for _ in range(n)]
INF = 10**18
for i in range(n):
    for j in range(n):
        if i!=j:
            dp[i][j] = INF
for a,b,t in l:
    dp[a-1][b-1] = t
    dp[b-1][a-1] = t
#print(dp)

#最短リスト
for c in range(n):
    for f in range(n):
        for la in range(n):
            dp[f][la] = min(dp[f][la] , dp[f][c] +  dp[c][la])

#各辺が何かしらの最短ルートに含まれているか
ans = 0
for a, b, t in l:
    cou = 0
    for j in range(n):
        for k in range(n):
            #jからkの最短ルートにa,bが入ってるいるか
            if dp[j][k] != dp[j][a-1] + t + dp[b-1][k]:
                cou += 1
    if cou==n**2:
        ans += 1

print(ans)


Fixed submission:
n,m = map(int,input().split())
l = [list(map(int,input().split())) for _ in range(m)]

dp = [[0 for _ in range(n)] for _ in range(n)]
INF = 10**18
for i in range(n):
    for j in range(n):
        if i!=j:
            dp[i][j] = INF
for a,b,t in l:
    dp[a-1][b-1] = t
    dp[b-1][a-1] = t
#print(dp)

#最短リスト
for c in range(n):
    for f in range(n):
        for la in range(n):
            dp[f][la] = min(dp[f][la] , dp[f][c] +  dp[c][la])

#各辺が何かしらの最短ルートに含まれているか
ans = 0
for a, b, t in l:
    cou = 0
    for j in range(n):
        for k in range(n):
            #jからkの最短ルートにa,bが入ってるいるか
            if dp[j][k] != dp[j][a-1] + t + dp[b-1][k]:
                cou += 1
    if cou==n**2:
        ans += 1

print(ans)
Buggy python submission (verdict: Runtime Error):
a,b,c = input().split()
if b == "+":
  print(int(a)+int(b))
else:
  print(int(a)-int(b))

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

Buggy python submission (verdict: Runtime Error):
a,b,c = input()
if b == "+":
  print(int(a)+int(b))
else:
  print(int(a)-int(b))

Fixed submission:
a,b,c = input().split()
if b == "+":
  print(int(a)+int(c))
else:
  print(int(a)-int(c))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split()))
s = 1
if n%2!=0:
  l1 = [i for i in range(0,n,2)]
  for i in l1:
    a = l.count(i)
    if ( (i==0 and a==1) or (i!=0 and a==2)):
      s *= 2
    else:
      s = 0
      break
  else:
    s //= 2
else:
  l1 = [i for i in range(1,n,2)]
  for i in l1:
    a=l.count(i)
    if a==2:
      s *= 2
    else:
      s = 0
      break
print(int(s)%(10**9+7))
      

Fixed submission:
n = int(input())
l = list(map(int,input().split()))
s = 1
if n%2!=0:
  if l.count(0)!=1:
    s =0
  else:
    l.remove(0)
    if len(set(l))==(n-1)//2:
      s = 2**((n-1)//2)
    else:
      s =0
else:
    if len(set(l))==n//2:
      s = 2**(n//2)
    else:
      s = 0
print(int(s)%(10**9+7))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split()))
s = 1
if n%2!=0:
  l1 = [i for i in range(0,n,2)]
  for i in l1:
    a = l.count(i)
    if (i==0 and a==1) or (i!=0 and a==2):
      s *= 2
    else:
      s = 0
      break
  else:
    s //= 2
else:
  l1 = [i for i in range(1,n,2)]
  for i in l1:
    a=l.count(i)
    if a==2:
      s *= 2
    else:
      s = 0
      break
print(int(s))
      
    

Fixed submission:
n = int(input())
l = list(map(int,input().split()))
s = 1
if n%2!=0:
  if l.count(0)!=1:
    s =0
  else:
    l.remove(0)
    if len(set(l))==(n-1)//2:
      s = 2**((n-1)//2)
    else:
      s =0
else:
    if len(set(l))==n//2:
      s = 2**(n//2)
    else:
      s = 0
print(int(s)%(10**9+7))
Buggy python submission (verdict: Time Limit Exceeded):
a,b,x = map(int,input().split())
count =0
for i in range(a,b+1):
  if i%x ==0:
    count += 1
print(count)
    

Fixed submission:
a,b,x = map(int,input().split())
print(int(b//x)-int((a-1)//x))
Buggy python submission (verdict: Runtime Error):
A = input()
B = input()
C = input()
T = "a"
while not ((A=="" and T=="a") and (B==""and T=="b") and (C==""and T=="c")):
  if T =="a":
    T = A[0]
    A = A[1:]
  elif T =="b":
    T = B[0]
    B = B[1:]
  elif T =="c":
    T = C[0]
    C = C[1:]
if (A==""):
  print("a")
elif (B==""):
  print("b")
elif (C==""):
  print("c")

Fixed submission:
A = input()
B = input()
C = input()
T = "a"
while  not ((A=="" and T=="a") or (B=="" and T=="b") or (C=="" and T=="c")):
  if T =="a":
    T = A[0]
    A = A[1:]
  elif T =="b":
    T = B[0]
    B = B[1:]
  elif T =="c":
    T = C[0]
    C = C[1:]
if (A=="" and T=="a"):
  print("A")
elif (B=="" and T=="b"):
  print("B")
elif (C=="" and T=="c"):
  print("C")
Buggy python submission (verdict: Time Limit Exceeded):
n, A = map(int,input().split())
x = list(map(int,input().split()))

dp = [list( [0 for _ in range(10**4)] for _ in range(n+1)) for _ in range(n+1)]
dp[0][0][0] = 1


for i in range(n):
    for k in range(n):
        for s in range(sum(x)+1):
                dp[i+1][k][s] += dp[i][k][s]
                dp[i+1][k+1][s+x[i]] += dp[i][k][s]

ans = 0
for i in range(1,n+1):
    ans += dp[n][i][A*i]
print(ans)



Fixed submission:
n, A = map(int,input().split())
x = list(map(int,input().split()))

dp = [list( [0 for _ in range(3000)] for _ in range(n+1)) for _ in range(n+1)]
dp[0][0][0] = 1


for i in range(n):
    for k in range(n):
        for s in range(sum(x)+1):
                dp[i+1][k][s] += dp[i][k][s]
                dp[i+1][k+1][s+x[i]] += dp[i][k][s]

ans = 0
for i in range(1,n+1):
    ans += dp[n][i][A*i]
print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()
if "N" in s:
  if "S" in s:
    continue
  else:
    print("No")
    exit()
if "E" in s:
  if "W" in s:
    continue
  else:
    print("no")
    exit()
print("Yes")

Fixed submission:
s = input()
if "N" in s:
  if "S" not in s :
    print("No")
    exit()
if "E" in s:
  if "W" not in s:
    print("No")
    exit()
if "S" in s:
  if "N" not in s :
    print("No")
    exit()
if "W" in s:
  if "E" not in s:
    print("No")
    exit()
print("Yes")
Buggy python submission (verdict: Runtime Error):
s=list(input())
print(s)
l=len(s)
data =[]
for i in s:
  if i=="0":
    data.append(0)
  elif i=="1":
    data.append(1)
  else:
    if data!=[]:
      del data[-1]
print("".join(data))

Fixed submission:
s=list(input())
data =[]
for i in s:
  if i=="0":
    data.append(0)
  elif i=="1":
    data.append(1)
  else:
    if data!=[]:
      del data[-1]
 #joinはリストが文字列のみ
data = map(str,data)
print(''.join(data))
Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
if (a==b==5 and c==7) or (a==c==5 b==7) or (b==c==5 a==7):
  print('YES')
else:
  print('NO')

Fixed submission:
a,b,c = map(int,input().split())
if (a==b==5 and c==7) or (a==c==5 and b==7) or (b==c==5 and a==7):
  print('YES')
else:
  print('NO')
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
mod = 998244353
D = []
dp = [0] * (N+10)
for i in range(K):
    L, R = map(int, input().split())
    for i in range(L, R+1):
        D.append(i)

D = sorted(D)
dp[0] = 1
for i in range(N):
    for d in D:
        if i + d > N:
            continue
        dp[i + d] += dp[i]
        dp[i + d] %= mod

print(dp[N-1])



Fixed submission:
N, K = map(int, input().split())
mod = 998244353
D = []
dp = [0] * (N+1)
for i in range(K):
    L, R = map(int, input().split())
    D.append([L, R])

dp[0] = 1
a = 0
for i in range(N):
    a += dp[i]
    a %= mod
    for d in D:
        if i + d[0] > N:
            continue
        dp[i + d[0]] += a
        dp[i + d[0]] %= mod
        if i + d[1] + 1 > N:
            continue

        dp[i + d[1] + 1] -= a
        dp[i + d[1] + 1] %= mod


print(dp[N-1])
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))

pos = -1
seen = [False] * n
hist = []
cycle = []
i = 0
while(seen[i] == False):
    seen[i] = True
    hist.insert(0, i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

preCircle = len(hist)

cnt = 0
while(len(hist) != 0):
    b = hist[0]
    cycle.append(b)
    cnt += 1
    hist.pop(0)
    if (b == pos):
        break

preCircle -= cnt

cycle = list(reversed(cycle))

if (k < preCircle):
    print(hist[-k-1] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)



Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

pos = -1
seen = [False] * n
last = [-1] * n
hist = []
i = 0
cnt = 0

while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

cycle = hist[last[pos]:]
preCircle = last[pos]

if (k <= preCircle):
    print(hist[k] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))

pos = -1
seen = [False] * n
finished = [False] * n
hist = []
cycle = []
cycles = [-1] * n
cn = {}

def dfs(v, p):
    global pos
    seen[v] = True
    hist.insert(0, v)
    next = a[v]-1
    if (next != p):
        if (finished[next] == False):
            if (seen[next]):
                pos = next
                return
            dfs(next, v)
            if (pos != -1):
                return

    hist.pop(0)
    finished[v] = True

dfs(0, -1)
cnt = 0
while(len(hist) != 0):
    b = hist[0]
    cycle.append(b)
    cycles[b] = cnt
    cn[b] = cnt
    cnt += 1
    hist.pop(0)
    if (b == pos):
        break
cycle = list(reversed(cycle))

n = 0
ans = -1
while(cycles[n] == -1 and k > 0):
    k -= 1
    n = a[n] - 1
    if(k == 0):
        ans = n

if (k > 0):
    dc = len(cycle) - cn[n] - 1
    d = (k+dc) % len(cycle)
    ans = cycle[d] + 1

print(ans)



Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

pos = -1
seen = [False] * n
last = [-1] * n
hist = []
i = 0
cnt = 0

while(seen[i] == False):
    seen[i] = True
    last[i] = cnt
    cnt += 1
    hist.append(i)
    ne = a[i]-1
    i = ne
    if (seen[i] == True):
        pos = i

cycle = hist[last[pos]:]
preCircle = last[pos]

if (k <= preCircle):
    print(hist[k] + 1)
else:
    k -= preCircle
    ans = k % len(cycle)
    print(cycle[ans] + 1)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

ans = 0

for i in range(N-1):
    for j in range(i+1, N):
        if (abs(j - i) == A[i] + A[j]):
            ans += 1

print(ans)


Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))

L = []
R = []

ans = 0

for i in range(N):
    L.append(i + A[i])
    R.append(i - A[i])

r = collections.Counter(R)

for l in L:
    ans += r[l]

print(ans)
Buggy python submission (verdict: Runtime Error):
import itertools


N, M, Q = map(int, input().split())

a = []
b = []
c = []
d = []

for i in range(Q):
    ai, bi, ci, di = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)

A = [i for i in range(1, M+1)] # [1, 2, 3, 4, ... M]

A_pattern = list(itertools.combinations_with_replacement(A, M)) # 10^5

ans = 0

for a_pattern in A_pattern: # 数列全部調べる
    ansi = 0
    for i in range(Q): # 得点になるかどうか調べる

        if a_pattern[b[i]-1] - a_pattern[a[i]-1] == c[i]: # A[bi] - A[ai] == ci -> ansi += di
            ansi += d[i]

    ans = max(ans, ansi)


print(ans)


Fixed submission:
import itertools


N, M, Q = map(int, input().split())

a = []
b = []
c = []
d = []

for i in range(Q):
    ai, bi, ci, di = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)

A = [i for i in range(1, M+1)] # [1, 2, 3, 4, ... M]

A_pattern = list(itertools.combinations_with_replacement(A, N))

ans = 0

for a_pattern in A_pattern: # 数列全部調べる
    ansi = 0
    for i in range(Q): # 得点になるかどうか調べる
        if a_pattern[b[i]-1] - a_pattern[a[i]-1] == c[i]: # A[bi] - A[ai] == ci -> ansi += di
            ansi += d[i]

    ans = max(ans, ansi)


print(ans)

Buggy python submission (verdict: Runtime Error):
import itertools


N, M, Q = map(int, input().split())

a = []
b = []
c = []
d = []

for i in range(Q):
    ai, bi, ci, di = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)

A = [i for i in range(1, M+1)] # [1, 2, 3, 4, ... M]

A_pattern = list(itertools.combinations_with_replacement(A, M)) # 10^5

ans = 0

for a_pattern in A_pattern: # 数列全部調べる
    ansi = 0
    for i in range(Q): # 得点になるかどうか調べる
        if a_pattern[b[i]] - a_pattern[a[i]] == c[i]: # A[bi] - A[ai] == ci -> ansi += di
            ansi += d[i]

    ans = max(ans, ansi)


print(ans)


Fixed submission:
import itertools


N, M, Q = map(int, input().split())

a = []
b = []
c = []
d = []

for i in range(Q):
    ai, bi, ci, di = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
    d.append(di)

A = [i for i in range(1, M+1)] # [1, 2, 3, 4, ... M]

A_pattern = list(itertools.combinations_with_replacement(A, N))

ans = 0

for a_pattern in A_pattern: # 数列全部調べる
    ansi = 0
    for i in range(Q): # 得点になるかどうか調べる
        if a_pattern[b[i]-1] - a_pattern[a[i]-1] == c[i]: # A[bi] - A[ai] == ci -> ansi += di
            ansi += d[i]

    ans = max(ans, ansi)


print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
S = []
for i in range(H):
    s = input()
    S.append(s)

ansList = []

def dfs(h, w, root):
    root.append(S[h][w])
    if (h == H - 1 and w == W - 1):
        ansList.append(root.copy())
        return
    else:
        if (h+1 < H):
            dfs(h+1, w, root.copy())
        if (w+1 < W):
            dfs(h, w+1, root.copy())
dfs(0, 0, [])

ans = H * W
for a in ansList:
    ans = min(ans, a.count("#"))

print(ans)


Fixed submission:
H, W = map(int, input().split())
dp = [[H*W for __ in range(W)] for _ in range(H)]
dh = [1, 0]
dw = [0, 1]
S = []
for i in range(H):
    s = input()
    S.append(s)

if (S[0][0] == '#'):
    dp[0][0] = 1
else:
    dp[0][0] = 0


for i in range(H):
    for j in range(W):
        for k in range(2):
            nh = i + dh[k]
            nw = j + dw[k]
            if nh >= H or nw >= W:
                continue
            add = 0
            if (S[nh][nw] == "#" and S[i][j] == "."):
                add = 1
            dp[nh][nw] = min(dp[nh][nw], dp[i][j] + add)


print(dp[H-1][W-1])
Buggy python submission (verdict: Runtime Error):
h, n = map(int, input().split())

a = list(map(int, input().split()))

a = sorted(a)

if(a[0] + a[1] >= h):
    print("Yes")
else:
    print("No")

Fixed submission:
h, n = map(int, input().split())

a = list(map(int, input().split()))

a = sorted(a)

if(sum(a) >= h):
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
N = int(input())

talk = [[-1 for i in range(N)] for j in range(N)]

for i in range(N):
    A = int(input())
    for j in range(A):
        x, y = map(int, input().split())
        talk[i][x-1] = y

ans = 0

for i in range(1 << N):
    honest = []
    ansi = 0
    for j in range(N):
        if(1 << j and i):
            honest.append(1)
        else:
            honest.append(0)

    for j in range(N):
        if(honest[i] == 0):
            continue
        flag = True
        for k in range(N):
            if(honest[k] != talk[j][k] and talk[j][k] != -1):
                flag = False

        if(flag):
            ansi += 1

    ans = max(ans, ansi)

print(ans)



Fixed submission:
N = int(input())
people = [[-1 for __ in range(N)] for _ in range(N)]
for i in range(N):
    A = int(input())
    for j in range(A):
        x, y = map(int, input().split())
        x -= 1
        people[i][x] = y

ans = 0


for i in range(1 << N):
    ansi = 0
    flag = True
    for j in range(N):
        peoplei = 0
        if (i & 1 << j):
            peoplei = 1
            ansi += 1
        for k in range(N):
            if (i & 1 << k):
                if(people[k][j] != -1 and people[k][j] != peoplei):
                    flag = False

    if flag:
        ans = max(ans, ansi)


print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
a = 0

for i in range(1, int(N ** 0.5)):
    if (N % i == 0):
        a = i

b = N // a

ans = (a - 1) + (b - 1)

print(ans)



Fixed submission:
N = int(input())
a = 0

for i in range(1, int(N ** (1/2)+1)):
    if (N % i == 0):
        a = i

b = N // a

ans = (a - 1) + (b - 1)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
ans = 0

for i in range(N-1):
    a = L[i]
    for j in range(i+1, N):
        b = L[j]
        r = bisect.bisect_left(L, a+b)
        l = max(i, j) + 1
        ans += max(0, r-l)
print(ans)


Fixed submission:
import bisect
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
ans = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        ans += bisect.bisect_left(L, L[i]+L[j]) - (j+1)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
ans = 0

for i in range(N-1):
    a = L[i]
    for j in range(i+1, N):
        b = L[j]
        ans += bisect.bisect_left(L, a+b) - bisect.bisect_right(L, max(a,b))

print(ans)


Fixed submission:
import bisect
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
ans = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        ans += bisect.bisect_left(L, L[i]+L[j]) - (j+1)

print(ans)
Buggy python submission (verdict: Runtime Error):
S = list(input())
K = int(input())

if(len(set(S)) == 1):
    print(len(S) // 2 * K)
else:
    m = 0
    ans = 0
    if(S[0] == S[-1]):
        l = 0
        r = len(S) - 1
        while(S[l] == S[l+1]):
            l += 1
        while(S[-r] == S[-r-1]):
            r += 1
        m = l//2 + r//2 - (l+r)//2
        m *= K - 1

    for i in range(len(S)-1):
        if(S[i] == S[i+1]):
            ans += 1
            S[i+1] = "3"
    ans *= K
    ans -= m
    print(ans)


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

if(len(set(S)) == 1):
    print(len(S) * K//2)
else:
    ans = 0
    cnt = 1
    a = []
    for i in range(len(S)-1):
        if(S[i] == S[i+1]):
            cnt += 1
        else:
            a.append(cnt)
            cnt = 1
    a.append(cnt)

    for i in a:
        ans += (i//2)*K

    if (S[0] == S[-1]):
        ans -= (a[0]//2 + a[-1]//2  - (a[0] + a[-1])//2)*(K-1)

    print(ans)

Buggy python submission (verdict: Runtime Error):
S = list(input())
K = int(input())

if(len(set(S)) == 1):
    print(len(S) // 2 * K)
else:
    m = 0
    ans = 0
    if(S[0] == S[-1]):
        l = 0
        r = len(S) - 1
        while(S[l] == S[l+1]):
            l += 1
        while(S[-r] == S[-r]):
            r += 1
        m = l//2 + r//2 - (l+r)//2
        m *= K - 1
    
    for i in range(len(S)-1):
        if(S[i] == S[i+1]):
            ans += 1
            S[i+1] = "3"
    ans *= K
    ans -= m
    print(ans)





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

if(len(set(S)) == 1):
    print(len(S) * K//2)
else:
    ans = 0
    cnt = 1
    a = []
    for i in range(len(S)-1):
        if(S[i] == S[i+1]):
            cnt += 1
        else:
            a.append(cnt)
            cnt = 1
    a.append(cnt)

    for i in a:
        ans += (i//2)*K

    if (S[0] == S[-1]):
        ans -= (a[0]//2 + a[-1]//2  - (a[0] + a[-1])//2)*(K-1)

    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
s = input()

m = []

cnt = 1
for i in range(n-1):
    if(s[i] != s[i+1]):
        m.append(cnt)
        cnt = 1
    else:
        cnt += 1

m.append(cnt)

sums = []

ans = 0

l = 0
r = 0
turn = 0

sums.append(0)
for i in range(len(m)):
    sums.append(sums[i] + m[i])

if(2*k + 1 >= len(m)):
    ans = n - 1
else:
    for i in range(len(m) - (2 * k + 1)):
        ansi = 0
        for j in range(0, i):
            ansi += m[j]-1
        for j in range(i+2*k+1, len(m)):
            ansi += m[j]-1
        ansi += sums[i+2*k+1] - sums[i] - 1
        ans = max(ans, ansi)
print(ans)


Fixed submission:
n, k = map(int, input().split())
s = input()

m = []

cnt = 1
for i in range(n-1):
    if(s[i] != s[i+1]):
        m.append(cnt)
        cnt = 1
    else:
        cnt += 1

m.append(cnt)

sums = []

ans = 0

l = 0
r = 0
turn = 0

sums.append(0)
for i in range(len(m)):
    sums.append(sums[i] + m[i])

if(2*k + 1 >= len(m)):
    ans = n - 1
else:
    for i in range(len(m) - (2 * k + 1)):
        ansi = 0
        ansi += sums[i] - sums[0] - i
        ansi += sums[len(m)] - sums[i+2*k+1] - (len(m) - (2 * k + 1))
        ansi += sums[i+2*k+1] - sums[i] - 1
        ans = max(ans, ansi)
print(ans)