Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()

ans = 0
maxi = n // 2
for i in range(maxi, 0, -1):
    dic = {}
    for j in range(n-i+1):
        if s[j:j+i] in dic.keys():
            if dic[s[j:j+i]] < j:
                print(i)
                exit()
        dic[s[j:j+i]] = j+i-1
print(ans)

Fixed submission:
def z_algorithm(s):
    n = len(s)
    z = [0 for i in range(n)]
    z[0] = n
    i, j = 1, 0
    while i < n:
        while i+j < n and s[j] == s[i+j]:
            j += 1
        z[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while k < j and k + z[k] < j:
            z[i+k] = z[k]
            k += 1
        i += k
        j -= k
    return z

n = int(input())
s = input()

ans = 0
for i in range(n):
    z = z_algorithm(s[i:])
    for j in range(len(z)):
        if z[j] <= j:
            ans = max(ans, z[j])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()

ans = 0
maxi = n // 2
for i in range(maxi, 0, -1):
    dic = {}
    for j in range(n-i+1):
        if s[j:j+i] in dic:
            if dic[s[j:j+i]] < j:
                print(i)
                exit()
        dic[s[j:j+i]] = j+i-1
print(ans)

Fixed submission:
def z_algorithm(s):
    n = len(s)
    z = [0 for i in range(n)]
    z[0] = n
    i, j = 1, 0
    while i < n:
        while i+j < n and s[j] == s[i+j]:
            j += 1
        z[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while k < j and k + z[k] < j:
            z[i+k] = z[k]
            k += 1
        i += k
        j -= k
    return z

n = int(input())
s = input()

ans = 0
for i in range(n):
    z = z_algorithm(s[i:])
    for j in range(len(z)):
        if z[j] <= j:
            ans = max(ans, z[j])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()

lst = [set([]), set([s[0]])]
ans = 0
for end in range(2, n+1):
    new_set = set([])
    for start in range(end):
        if s[start:end] in lst[start]:
            ans = max(ans, len(s[start:end]))
        new_set.add(s[start:end])
    new_set = new_set | lst[-1]
    lst.append(new_set)
print(ans)

Fixed submission:
def z_algorithm(s):
    n = len(s)
    z = [0 for i in range(n)]
    z[0] = n
    i, j = 1, 0
    while i < n:
        while i+j < n and s[j] == s[i+j]:
            j += 1
        z[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while k < j and k + z[k] < j:
            z[i+k] = z[k]
            k += 1
        i += k
        j -= k
    return z

n = int(input())
s = input()

ans = 0
for i in range(n):
    z = z_algorithm(s[i:])
    for j in range(len(z)):
        if z[j] <= j:
            ans = max(ans, z[j])
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input()
baai = 0

def hanten(string):
    ans = ''
    for i in range(len(string)):
        if string[i] == 'B':
            ans += 'W'
        else:
            ans += 'B'
    return ans

def saiki(string, baai):
    if string == 'BB':
        return baai + 1
    elif string[0] == 'B':
        for i in range(1, len(string)):
            if i % 2 == 1 and string[i] == 'B':
                new_string = hanten(string[1:i]) + string[i+1:]
                baai = saiki(new_string, baai)
            elif i % 2 == 0 and string[i] == 'W':
                new_string = hanten(string[1:i]) + string[i+1:]
                baai = saiki(new_string, baai)
    else:
        return baai
    return baai

baai = saiki(s, 0)

ans = 1
for i in range(1, n+1):
    ans *= i
    ans = ans % (10**9+7)

ans = ans * baai % (10**9+7)
print(ans)

Fixed submission:
mod = 10**9 + 7
n = int(input())
s = input()

crr = 0
lst= []
for i in range(2*n):
    if s[i] == 'B':
        if crr%2 == 1:
            lst.append(-1)
            crr -= 1
        else:
            lst.append(1)
            crr += 1
    else:
        if crr%2 == 1:
            lst.append(1)
            crr += 1
        else:
            lst.append(-1)
            crr -= 1

if crr!=0 or lst[0]!=1 or lst[-1]!=-1:
    print(0)
    exit()

plus = []
pcnt = 0
minus = []
mcnt = 0
where = []
for i in range(2*n):
    if lst[i] == 1:
        pcnt += 1
    else:
        mcnt += 1
        where.append(i)
    plus.append(pcnt)
    minus.append(mcnt)

ans = 1
for i in where:
    ans *= plus[i] - minus[i] + 1
    ans %= mod

for i in range(1, n+1):
    ans *= i
    ans %= mod

print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np

n, q = map(int, input().split())
edges=[[] for i in range(n+1)]
d_from_1=[-1 for i in range(n+1)]
ans = [0 for i in range(n+1)]

for i in range(1,n):
	a, b=[int(i) for i in input().split()]
	edges[a].append([b,1])
	edges[b].append([a,1])
def kyori_from_1(u):
	for v,w in edges[u]:
		if d_from_1[v]==-1:
			d_from_1[v]=d_from_1[u]+w
			kyori_from_1(v)

d_from_1[1]=0
kyori_from_1(1)

px = []
for i in range(q):
    p, x = map(int, input().split())
    px.append([p, x])


d_from_p = [-1 for i in range(n+1)]
def kyori_from_p(u):
	for v,w in edges[u]:
		if d_from_p[v]==-1:
			d_from_p[v]=d_from_p[u]+w
			kyori_from_p(v)

for i in range(q):
    d_from_p = [-1 for i in range(n+1)]
    d_from_p[px[i][0]] = 0
    kyori_from_p(px[i][0])
    for j in range(1, n+1):
        if px[i][0] == 1:
            ans[j] += px[i][1]
        elif d_from_1[j] >= d_from_1[px[i][0]] and d_from_p[j] < d_from_1[j]:
            ans[j] += px[i][1]

ans.pop(0)
print(' '.join(map(str, ans)))

Fixed submission:
n, q = map(int, input().split())
edges=[[] for i in range(n+1)]
for i in range(1,n):
	a, b=[int(i) for i in input().split()]
	edges[a].append(b)
	edges[b].append(a)
c = [0 for i in range(n+1)]
for i in range(q):
    p, x = map(int, input().split())
    c[p] += x

ind = [0 for i in range(n+1)]
ind[1] = 1
nxt = [1]
nxt_copy = []
while nxt != []:
    for i in nxt:
        for j in edges[i]:
            if ind[j] == 0:
                ind[j] = 1
                c[j] += c[i]
                nxt_copy.append(j)
    nxt = nxt_copy.copy()
    nxt_copy = []

c.pop(0)
print(' '.join(map(str, c)))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

n = int(input())
s = []
for i in range(n):
    s.append(Counter(input()))

ans = 0
while s != []:
    string = s.pop()
    s_new = []
    temp = 1
    for mojiretu in s:
        if string == mojiretu:
            temp += 1
        else:
            s_new.append(mojiretu)
    ans += temp * (temp-1) // 2
    s = s_new

print(ans)

Fixed submission:
n = int(input())
lst = []
dic = {}
for i in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    if s not in dic.keys():
        dic[s] = 1
    else:
        dic[s] += 1

ans = 0
for i in dic.values():
    ans += i * (i-1) // 2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

n = int(input())
s = []
for i in range(n):
    s.append(Counter(input()))

ans = 0
while s != []:
    string = s.pop()
    s_new = []
    temp = 1
    for mojiretu in s:
        if string == mojiretu:
            temp += 1
        else:
            s_new.append(mojiretu)
    ans += temp * (temp-1) // 2
    s = s_new.copy()

print(ans)

Fixed submission:
n = int(input())
lst = []
dic = {}
for i in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    if s not in dic.keys():
        dic[s] = 1
    else:
        dic[s] += 1

ans = 0
for i in dic.values():
    ans += i * (i-1) // 2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
 
def verify_anagrams(first_word, second_word):
    def get_counter(s):
        return Counter(s.upper().replace(' ', ''))
 
    return get_counter(first_word) == get_counter(second_word)

n = int(input())
s = []
for i in range(n):
    s.append(input())

ans = 0
roop = True

while s != []:
    string = s.pop()
    s_new = []
    temp = 1
    for mojiretu in s:
        if verify_anagrams(string, mojiretu):
            temp += 1
        else:
            s_new.append(mojiretu)
    ans += temp * (temp-1) // 2
    s = s_new.copy()

print(ans)

Fixed submission:
n = int(input())
lst = []
dic = {}
for i in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    if s not in dic.keys():
        dic[s] = 1
    else:
        dic[s] += 1

ans = 0
for i in dic.values():
    ans += i * (i-1) // 2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
 
def verify_anagrams(first_word, second_word):
    def get_counter(s):
        return Counter(s.upper().replace(' ', ''))
 
    return get_counter(first_word) == get_counter(second_word)

n = int(input())
s = []
for i in range(n):
    s.append(input())

ans = 0
for i in range(n):
    for j in range(i+1, n):
        if verify_anagrams(s[i], s[j]):
            ans += 1

print(ans)

Fixed submission:
n = int(input())
lst = []
dic = {}
for i in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    if s not in dic.keys():
        dic[s] = 1
    else:
        dic[s] += 1

ans = 0
for i in dic.values():
    ans += i * (i-1) // 2

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

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

a.insert(0, -1)

env = [0] * (n+1)
hako = [0] * (n+1)
ind = []

for _ in range(n):
    i = n - _
    if a[i] != env[i]:
        hako[i] = 1
        ind.append(str(i))
        yakusuu = make_divisors(i)
        for j in yakusuu:
            if env[j] == 0:
                env[j] = 1
            else:
                env[j] = 0

goukei = sum(hako)
print(goukei)
if ind == []:
    sys.exit()
mojiretu = ' '.join(ind)
print(mojiretu)

Fixed submission:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

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

a.insert(0, -1)

env = [0] * (n+1)
hako = [0] * (n+1)
ind = []

for _ in range(n):
    i = n - _
    if a[i] != env[i]:
        hako[i] = 1
        ind.append(str(i))
        yakusuu = make_divisors(i)
        for j in yakusuu:
            if env[j] == 0:
                env[j] = 1
            else:
                env[j] = 0

goukei = sum(hako)
print(goukei)
if ind != []:
    mojiretu = ' '.join(ind)
    print(mojiretu)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from collections import deque

n = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(input())

arr = np.array(a)
jun = np.argsort(arr)
jun = deque(jun)

ans = 0
while jun:
    ans += 1
    i = jun.pop()
    for j in range(i):
        if a[j] != a[i]:
            try:
                jun.remove(j)
            except:
                ValueError

print(ans)

Fixed submission:
# 広義最長部分増加数列の長さを求めるdef
# 配列LIS自体は広義最長部分増加文字列ではない
import bisect
def lis(seq):
    LIS = [seq[0]]
    for i in range(1, len(seq)):
        if seq[i] >= LIS[-1]:
            LIS.append(seq[i])
        else:
            LIS[bisect.bisect_right(LIS, seq[i])] = seq[i]
    return(len(LIS))

n = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(input())

b = a[::-1]
print(lis(b))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(input())

arr = np.array(a)
jun = np.argsort(arr)
jun = list(jun)

ans = 0
while jun:
    ans += 1
    i = jun.pop()
    for j in range(i):
        if a[j] != a[i]:
            try:
                jun.remove(j)
            except:
                ValueError

print(ans)

Fixed submission:
# 広義最長部分増加数列の長さを求めるdef
# 配列LIS自体は広義最長部分増加文字列ではない
import bisect
def lis(seq):
    LIS = [seq[0]]
    for i in range(1, len(seq)):
        if seq[i] >= LIS[-1]:
            LIS.append(seq[i])
        else:
            LIS[bisect.bisect_right(LIS, seq[i])] = seq[i]
    return(len(LIS))

n = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(input())

b = a[::-1]
print(lis(b))
Buggy python submission (verdict: Time Limit Exceeded):
import sys

n, m = map(int, input().split())
lst = [[] for _ in range(n+1)]
for i in range(m):
    a, b = map(int, input().split())
    lst[a].append(b)
    lst[b].append(a)

if m % 2 == 1:
    print(-1)
    sys.exit()

hen = m
while True:
    if hen == 0:
        break
    for i in range(1, n+1):
        if len(lst[i])%2 == 0 and len(lst[i]) != 0:
            x = lst[i].pop()
            y = lst[i].pop()
            lst[x].remove(i)
            lst[y].remove(i)
            print(i, x)
            print(i, y)
            hen -= 2
            break

Fixed submission:
import sys

n, m = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(m)]
lst = [set() for _ in range(n+1)]
for a, b in edge:
    lst[a].add(b)
    lst[b].add(a)

if m % 2 == 1:
    print(-1)
    sys.exit()

check = [0] * (n+1)
q = [1]
next_q = []
tree = [set() for _ in range(n+1)]
top = [1]
while q:
    for oya in q:
        check[oya] = 1
        for ko in lst[oya]:
            if check[ko] == 0:
                check[ko] = 1
                top.append(ko)
                next_q.append(ko)
                tree[oya].add(ko)
                tree[ko].add(oya)
    q = next_q.copy()
    next_q = []

ecount = [0] * (n+1)
for a, b in edge:
    if a in tree[b]:
        continue
    print(a, b)
    ecount[a] += 1

for i in top[::-1][:-1]:
    j = list(tree[i])[0]
    if ecount[i]%2 == 0:
        print(j, i)
        ecount[j] += 1
    else:
        print(i, j)
        ecount[i] += 1
    tree[i].remove(j)
    tree[j].remove(i)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

n, m = map(int, input().split())
lst = [[] for _ in range(n+1)]
for i in range(m):
    a, b = map(int, input().split())
    lst[a].append(b)
    lst[b].append(a)

if m % 2 == 1:
    print(-1)
    sys.exit()

hen = m
while True:
    if hen == 0:
        break
    for i in range(1, n+1):
        if len(lst[i])%2 == 0 and len(lst[i]) != 0:
            x = lst[i].pop()
            y = lst[i].pop()
            lst[x].remove(i)
            lst[y].remove(i)
            print(i, x)
            print(i, y)
            hen -= 2
            break

Fixed submission:
import sys

n, m = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(m)]
lst = [set() for _ in range(n+1)]
for a, b in edge:
    lst[a].add(b)
    lst[b].add(a)

if m % 2 == 1:
    print(-1)
    sys.exit()

check = [0] * (n+1)
q = [1]
next_q = []
tree = [set() for _ in range(n+1)]
top = [1]
while q:
    for oya in q:
        check[oya] = 1
        for ko in lst[oya]:
            if check[ko] == 0:
                check[ko] = 1
                top.append(ko)
                next_q.append(ko)
                tree[oya].add(ko)
                tree[ko].add(oya)
    q = next_q.copy()
    next_q = []

ecount = [0] * (n+1)
for a, b in edge:
    if a in tree[b]:
        continue
    print(a, b)
    ecount[a] += 1

for i in top[::-1][:-1]:
    j = list(tree[i])[0]
    if ecount[i]%2 == 0:
        print(j, i)
        ecount[j] += 1
    else:
        print(i, j)
        ecount[i] += 1
    tree[i].remove(j)
    tree[j].remove(i)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
dam = list(map(int, input().split()))
yama = [0] * n
yama_copy = [0] * n

min_dam = min(dam)
min_arg = dam.index(min(dam))

yama_bf = min_arg
yama_i = min_arg
for i in range(0, min_dam * 2 + 1, 2):
    yama_copy[yama_i] = i
    yama_i = (yama_i + 1) % n
    for j in range(n-1):
        yama_copy[yama_i] = (dam[yama_bf] - yama_copy[yama_bf]/2) * 2
        yama_bf = yama_i
        yama_i = (yama_i + 1) % n
    if dam[yama_bf] == yama_copy[yama_bf]/2 + yama_copy[min_arg]/2:
        break
    yama_copy = [0] * n
    yama_bf = min_arg
    yama_i = min_arg
yama_copy = map(int, yama_copy)
print(' '.join(map(str, yama_copy)))

Fixed submission:
import numpy as np

n = int(input())
dam = list(map(int, input().split()))
yama = [-1] * n

min_dam = min(dam)
min_dam_arg = np.argmin(dam)

s = sum(dam)
yama[0] = s - 2 * sum(dam[1::2])
for i in range(1, n):
    yama[i] = 2 * (dam[i-1] - yama[i-1]/2)

yama = map(int, yama)
print(' '.join(map(str, yama)))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
dam = list(map(int, input().split()))
yama = [0] * n
yama_copy = [0] * n

min_dam = min(dam)
min_arg = np.argmin(dam)

yama_bf = min_arg
yama_i = min_arg
for i in range(0, min_dam * 2 + 1, 2):
    yama_copy[yama_i] = i
    yama_i = (yama_i + 1) % n
    for j in range(n-1):
        yama_copy[yama_i] = (dam[yama_bf] - yama_copy[yama_bf]/2) * 2
        yama_bf = yama_i
        yama_i = (yama_i + 1) % n
    if dam[yama_bf] == yama_copy[yama_bf]/2 + yama_copy[min_arg]/2:
        break
    yama_copy = [0] * n
    yama_bf = min_arg
    yama_i = min_arg
yama_copy = map(int, yama_copy)
print(' '.join(map(str, yama_copy)))

Fixed submission:
import numpy as np

n = int(input())
dam = list(map(int, input().split()))
yama = [-1] * n

min_dam = min(dam)
min_dam_arg = np.argmin(dam)

s = sum(dam)
yama[0] = s - 2 * sum(dam[1::2])
for i in range(1, n):
    yama[i] = 2 * (dam[i-1] - yama[i-1]/2)

yama = map(int, yama)
print(' '.join(map(str, yama)))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

n = int(input())
edges = [[] for i in range(2*10**5)]
for i in range(n):
    xi, yi = map(int, input().split())
    xi -= 1
    yi = yi - 1 + 10**5
    edges[xi].append(yi)
    edges[yi].append(xi)

already = set([])
def dfs(x, y, node):
    if node in already:
        return x, y
    if node < 10**5:
        x += 1
    else:
        y += 1
    already.add(node)
    for next_node in edges[node]:
        x, y = dfs(x, y, next_node)
    return x, y

num = 0
for i in range(10**5):
    x, y = dfs(0, 0, i)
    num += x * y
print(num-n)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n = int(input())
edges = [[] for i in range(2*10**5)]
for i in range(n):
    xi, yi = map(int, input().split())
    xi -= 1
    yi = yi - 1 + 10**5
    edges[xi].append(yi)
    edges[yi].append(xi)

already = set([])
def dfs(x, y, node):
    if node in already:
        return x, y
    if node < 10**5:
        x += 1
    else:
        y += 1
    already.add(node)
    for next_node in edges[node]:
        x, y = dfs(x, y, next_node)
    return x, y

num = 0
for i in range(10**5):
    x, y = dfs(0, 0, i)
    num += x * y
print(num-n)

Buggy python submission (verdict: Runtime Error):
n = int(input())
edges = [[] for i in range(2*10**5)]
for i in range(n):
    xi, yi = map(int, input().split())
    xi -= 1
    yi = yi - 1 + 10**5
    edges[xi].append(yi)
    edges[yi].append(xi)

already = set([])
def dfs(x, y, node):
    if node in already:
        return x, y
    if node < 10**5:
        x += 1
    else:
        y += 1
    already.add(node)
    for next_node in edges[node]:
        x, y = dfs(x, y, next_node)
    return x, y

num = 0
for i in range(10**5):
    x, y = dfs(0, 0, i)
    num += x * y
print(num-n)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n = int(input())
edges = [[] for i in range(2*10**5)]
for i in range(n):
    xi, yi = map(int, input().split())
    xi -= 1
    yi = yi - 1 + 10**5
    edges[xi].append(yi)
    edges[yi].append(xi)

already = set([])
def dfs(x, y, node):
    if node in already:
        return x, y
    if node < 10**5:
        x += 1
    else:
        y += 1
    already.add(node)
    for next_node in edges[node]:
        x, y = dfs(x, y, next_node)
    return x, y

num = 0
for i in range(10**5):
    x, y = dfs(0, 0, i)
    num += x * y
print(num-n)
Buggy python submission (verdict: Time Limit Exceeded):
import copy

n = int(input())
lst = [[0]*2 for i in range(n)]
for i in range(n):
    lst[i][0], lst[i][1] = map(int, input().split())

lst.sort(key=lambda x:(x[1]))
lst.sort(key=lambda x:(x[0]))

lst_copy = copy.deepcopy(lst)
saidai = 0
for i in range(n-1):
    for j in range(i+1, n):
        cnt = 0
        p = lst[j][0] - lst[i][0]
        q = lst[j][1] - lst[i][1]
        lst_copy = copy.deepcopy(lst)
        while lst_copy != []:
            lst_k = lst_copy.pop(0)
            xs, ys = lst_k[0], lst_k[1]
            length = len(lst_copy)
            delete_lst = []
            lst_copy_copy = copy.deepcopy(lst_copy)
            for l in range(length):
                xl, yl = lst_copy[l][0], lst_copy[l][1]
                if xl == xs + p and yl == ys + q:
                    xs = xs + p
                    ys = ys + q
                    cnt += 1
                    lst_copy_copy.remove([xl, yl])
            lst_copy = lst_copy_copy
        saidai = max(saidai, cnt)

print(n-saidai)

Fixed submission:
import copy

n = int(input())
lst = [[0]*2 for i in range(n)]
for i in range(n):
    lst[i][0], lst[i][1] = map(int, input().split())

lst.sort(key=lambda x:(x[1]))
lst.sort(key=lambda x:(x[0]))

lst_copy = copy.deepcopy(lst)
saidai = 0
for i in range(n-1):
    for j in range(i+1, n):
        cnt = 0
        p = lst[j][0] - lst[i][0]
        q = lst[j][1] - lst[i][1]
        lst_copy = copy.deepcopy(lst)
        while lst_copy != []:
            xs, ys = lst_copy[0][0], lst_copy[0][1]
            lst_copy.pop(0)
            while [xs+p, ys+q] in lst_copy:
                cnt += 1
                xs, ys = xs+p, ys+q
                lst_copy.remove([xs, ys])
        saidai = max(saidai, cnt)

print(n-saidai)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
lst = list(s)

cnt = 0
madaikeru = True
while madaikeru:
    if len(s) < 3:
        madaikeru = False
    else:
        for i in range(len(s)-2):
            if lst[i]=='A' and lst[i+1]=='B' and lst[i+2]=='C':
                lst[i] = 'B'
                lst[i+1] = 'C'
                lst[i+2] = 'A'
                cnt += 1
                break
            if i == len(s)-3:
                madaikeru = False

print(cnt)

Fixed submission:
s = input()
s = s.replace('BC', 'D')
lst = list(s)

acount = 0
ans = 0
for i in range(len(s)):
    if s[i] == 'A':
        acount += 1
    elif s[i] == 'D':
        ans += acount
    else:
        acount = 0

print(ans)