Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
from collections import defaultdict

def main():
    N, M = map(int, input().split())
    ans = 0
    d = defaultdict(list)
    for _ in range(N):
        A, B = map(int, input().split())
        d[A].append(-B)
    cand = []
    heapq.heapify(cand)
    for day in range(M-1,-1,-1):
        diff = M - day
        if d[diff]:
            cand += d[diff]
            heapq.heapify(cand)
        if cand:
            ans -= heapq.heappop(cand)

    print(ans)



if __name__ == "__main__":
    main()

Fixed submission:
from heapq import heappush, heappop

def main():
    N, M = map(int, input().split())

    # 日数別に仕事を管理
    works = [[] for _ in range(M)]

    for i in range(N):
        A, B = map(int, input().split())
        
        if A > M:
            continue
        
        deadline = M - A
        # heapqは最小値を返す仕組みしかないので、最大値を返すため-する
        works[deadline].append(-B)
    
    ans = 0
    heapque = []

    # i日目に始めることができる仕事で報酬の高いものを選ぶ
    for i in range(M - 1, -1, -1):
        for work in works[i]:
            # heapqに候補を追加
            heappush(heapque, work)
        
        if heapque:
            ans += heappop(heapque)
    
    print(-ans)


main()

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

def main():
    N, M = map(int, input().split())
    ans = 0
    d = defaultdict(list)
    for _ in range(N):
        A, B = map(int, input().split())
        d[A].append(-B)
    cand = []
    heapq.heapify(cand)
    for day in range(M-1,-1,-1):
        diff = M - day
        if d[diff]:
            cand += d[diff]
        heapq.heapify(cand)
        if cand:
            ans -= heapq.heappop(cand)

    print(ans)



if __name__ == "__main__":
    main()

Fixed submission:
from heapq import heappush, heappop

def main():
    N, M = map(int, input().split())

    # 日数別に仕事を管理
    works = [[] for _ in range(M)]

    for i in range(N):
        A, B = map(int, input().split())
        
        if A > M:
            continue
        
        deadline = M - A
        # heapqは最小値を返す仕組みしかないので、最大値を返すため-する
        works[deadline].append(-B)
    
    ans = 0
    heapque = []

    # i日目に始めることができる仕事で報酬の高いものを選ぶ
    for i in range(M - 1, -1, -1):
        for work in works[i]:
            # heapqに候補を追加
            heappush(heapque, work)
        
        if heapque:
            ans += heappop(heapque)
    
    print(-ans)


main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    S = input()

    tmp = [[0] * len(S)] * (10 ** 6)
    tmp[0] = [1] * len(S)

    for i in range(10**6):
        for j, s in enumerate(S):
            if s == 'R':
                tmp[i+1][j+1] += tmp[i][j]
            if s == 'L':
                tmp[i+1][j-1] += tmp[i][j]
        tmp[i+2] = [0] * len(S)
        if i >= 2:
            if tmp[i] == tmp[i-2]:
                if i % 2 == 0:
                    print(*tmp[i])
                    exit()
                else:
                    print(*tmp[i-1])
                    exit()
main()

Fixed submission:
def runlength(S):
    d = []
    cnt = 1
    for i in range(len(S)-1):
        if S[i] != S[i+1]:
            d.append(cnt)
            cnt = 1
        else:
            cnt += 1
    else:
        d.append(cnt)
    return d


def main():
    S = input()

    d = runlength(S)
    ans = [0] * len(S)
    pre = 0
    for i in range(0,len(d),2):
        tmp = d[i] + d[i+1]
        if d[i] % 2 == 0:
            ans[pre+d[i]-1] = tmp // 2
            ans[pre+d[i]] = tmp - ans[pre+d[i]-1]
        else:
            ans[pre+d[i]] = tmp // 2
            ans[pre+d[i]-1] = tmp - ans[pre+d[i]]

        pre += tmp

    print(*ans)






if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    S = input()

    tmp = [[0] * len(S)] * (10 ** 6)
    tmp[0] = [1] * len(S)

    for i in range(10**6):
        for j, s in enumerate(S):
            if s == 'R':
                tmp[i+1][j+1] += tmp[i][j]
                #print(tmp[i+1])
            if s == 'L':
                tmp[i+1][j-1] += tmp[i][j]
                #print(tmp[i+1])
        tmp[i+2] = [0] * len(S)
        if i >= 2:
            if tmp[i] == tmp[i-2]:
                if i % 2 == 0:
                    print(*tmp[i])
                    exit()
                else:
                    print(*tmp[i-1])
                    exit()
main()

Fixed submission:
def runlength(S):
    d = []
    cnt = 1
    for i in range(len(S)-1):
        if S[i] != S[i+1]:
            d.append(cnt)
            cnt = 1
        else:
            cnt += 1
    else:
        d.append(cnt)
    return d


def main():
    S = input()

    d = runlength(S)
    ans = [0] * len(S)
    pre = 0
    for i in range(0,len(d),2):
        tmp = d[i] + d[i+1]
        if d[i] % 2 == 0:
            ans[pre+d[i]-1] = tmp // 2
            ans[pre+d[i]] = tmp - ans[pre+d[i]-1]
        else:
            ans[pre+d[i]] = tmp // 2
            ans[pre+d[i]-1] = tmp - ans[pre+d[i]]

        pre += tmp

    print(*ans)






if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [0] * N
for i in range(N):
    A[i] = int(input())

set_A = set(A)
count_A = A.count(max(A))
for a in A:
    if a == max(A) and count_A == 1:
        print(sorted(set(set_A))[-2])
    else:
        print(max(A))

Fixed submission:
def main():
    N = int(input())
    A = [0] * N
    for i in range(N):
        A[i] = int(input())

    set_A = set(A)
    max_A = max(A)
    count_A = A.count(max_A)
    for a in A:
        if a == max_A and count_A == 1:
            print(sorted(set_A)[-2])
        else:
            print(max_A)

main()
Buggy python submission (verdict: Runtime Error):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = [0] * N
    for i, a in enumerate(reversed(A)):
        tmp = N - i 
        cnt = 1
        tmp_sum = 0
        if tmp == 1:
            if a % 2 == sum(ans) % 2:
                break
            else:
                ans[tmp-1] = 1
                break
        while tmp <= N:
            tmp_sum += ans[tmp-1]
            cnt = cnt + 1
            tmp = tmp * cnt
        
        if a % 2 == tmp_sum % 2:
            continue
        else:
            ans[tmp-1] = 1
    if ans.count(0) == len(ans):
        print(0)
    else:
        print(len(ans))
        print(*ans)
main()

Fixed submission:
def main():
    N = int(input())
    a = [0] + list(map(int, input().split()))

    b = [0] * (N + 1) 

    for i in range(N,0,-1):
        sum = 0
        for j in range(2*i, N+1, i):
            sum += b[j]
        if sum % 2 != a[i] % 2:
            b[i] = 1
    
    ans = []
    for i in range(1,N+1):
        if b[i] == 1:
            ans.append(i)
    
    print(len(ans))
    print(*ans)

main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]

    check = [False] * N
    index = 0
    ans = 0
    cnt = 0
    while True:
        pre = -1
        for i in range(index,N,1):
            if check[i]:
                continue
            if A[i] > pre:
                pre = A[i]
                check[i] = True
                cnt += 1
            else:
                cnt = min(cnt,i)

        ans += 1
        if cnt == N:
            break
    
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
from collections import deque
from bisect import bisect_left

def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    d = deque([])
    d.append(A[0])
    for i in range(1,N):
        p = bisect_left(d, A[i])
        if p == 0:
            d.appendleft(A[i])
        else:
            d[p-1] = A[i]

    print(len(d))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def main():
    N, K = map(int, input().split())
    MOD = 10 ** 9 + 7
    for i in range(1,K+1):
        blue = cmb(K-1,i-1)
        red = cmb(N-K+1,N-K-i+1)
        ans = blue * red
        ans %= MOD
        print(ans)
        


if __name__ == "__main__":
    main()

Fixed submission:
import math

def comb(n, k):
    return math.factorial(n) // math.factorial(k) // math.factorial(n - k)

def main():
    MOD = 10 ** 9 + 7
    N, K = map(int, input().split())
    for i in range(1,K+1):
        if i > N - K + 1:
            print(0)
            continue
        blue = comb(N-K+1,i) % MOD
        red = comb(K-1,i-1) % MOD

        ans = blue * red % MOD

        print(ans)
            

main()

Buggy python submission (verdict: Runtime Error):
from math import factorial

def permutations_count(n, r):
    ''' 順列 '''
    return factorial(n) // factorial(n - r)

def combinations_count(n, r):
    ''' 組み合わせ '''
    return factorial(n) // (factorial(n - r) * factorial(r))

N, K = map(int, input().split())

for i in range(1,K+1):
    #if i == 1:
    #    print(N-K+1)
    #else:
    if K % i == 0:
        print(combinations_count(N-K+1,i)%(10 ** 9 + 7))
    else:
        print(permutations_count(N-K+1,i)%(10 ** 9 + 7))

Fixed submission:
import math

def comb(n, k):
    return math.factorial(n) // math.factorial(k) // math.factorial(n - k)

def main():
    MOD = 10 ** 9 + 7
    N, K = map(int, input().split())
    for i in range(1,K+1):
        if i > N - K + 1:
            print(0)
            continue
        blue = comb(N-K+1,i) % MOD
        red = comb(K-1,i-1) % MOD

        ans = blue * red % MOD

        print(ans)
            

main()

Buggy python submission (verdict: Runtime Error):
from operator import mul
from functools import reduce
import math

def permtest(n, k = 1):
    p = math.factorial(n) / math.factorial(n - k)
    return int(p)

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

N, K = map(int, input().split())

for i in range(1,K+1):
    if i == 1:
        print(N-K+1)
    else:
        if K % i == 0:
            print(cmb(N-K+1,i)%(10 ** 9 + 7))
        else:
            print(permtest(N-K+1,i)%(10 ** 9 + 7))

Fixed submission:
import math

def comb(n, k):
    return math.factorial(n) // math.factorial(k) // math.factorial(n - k)

def main():
    MOD = 10 ** 9 + 7
    N, K = map(int, input().split())
    for i in range(1,K+1):
        if i > N - K + 1:
            print(0)
            continue
        blue = comb(N-K+1,i) % MOD
        red = comb(K-1,i-1) % MOD

        ans = blue * red % MOD

        print(ans)
            

main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def main():
    N = int(input())
    V = 10 ** 5 + 5
    edge = [[] * (2 * V) for _ in range(2 * V)]
    visited = [False] * (2 * V)
    for i in range(N):
        x, y = map(int, input().split())
        y += V
        edge[x].append(y)
        edge[y].append(x)

    def dfs(i):
        if visited[i]:
            return
        visited[i] = True
        cnt[i//V] += 1
        for j in edge[i]:
            dfs(j)
    ans = 0
    for i in range(2 * V):
        if visited[i]:
            continue
        cnt = [0,0]
        dfs(i)
        ans += cnt[0] * cnt[1]

    print(ans - N)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def main():
    N = int(input())
    V = 10 ** 5 + 5
    edge = [[] * (2 * V) for _ in range(2 * V)]
    visited = [False] * (2 * V)
    for i in range(N):
        x, y = map(int, input().split())
        y += V
        edge[x].append(y)
        edge[y].append(x)

    ans = 0
    for i in range(2 * V):
        if visited[i]:
            continue
        cnt = [0,0]
        q = deque([i])
        while q:
            s = q.pop() # dfsをやりたいのでpopleftではなくpop
            if visited[s]:
                continue
            visited[s] = True
            cnt[s//V] += 1
            for v in edge[s]:
                if visited[v]:
                    continue
                q.append(v)
        ans += cnt[0] * cnt[1]

    print(ans - N)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, M = map(int, input().split())
    S = list(map(int, input().split())) + [0]
    T = list(map(int, input().split())) + [0]
    dp0 = [[0] * (M + 5) for _ in range(N + 5)]
    dp1 = [[0] * (M + 5) for _ in range(N + 5)]
    MOD = 10 ** 9 + 7
    dp0[0][0] = 1
    for i in range(N + 1):
        for j in range(M + 1):
            dp0[i+1][j] += dp0[i][j]
            dp1[i][j] += dp0[i][j]
            dp1[i][j+1] += dp1[i][j]
            if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]

    ans = dp1[N][M] % MOD
    print(ans)



if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, M = map(int, input().split())
    S = list(map(int, input().split())) + [0]
    T = list(map(int, input().split())) + [0]
    dp0 = [[0] * (M + 5) for _ in range(N + 5)]
    dp1 = [[0] * (M + 5) for _ in range(N + 5)]
    MOD = 10 ** 9 + 7
    dp0[0][0] = 1
    for i in range(N + 1):
        for j in range(M + 1):
            dp0[i+1][j] += dp0[i][j]
            dp0[i+1][j] %= MOD
            dp1[i][j] += dp0[i][j]
            dp1[i][j] %= MOD
            dp1[i][j+1] += dp1[i][j]
            dp1[i][j+1] %= MOD
            if S[i] == T[j]:
                dp0[i+1][j+1] += dp1[i][j]
                dp0[i+1][j+1] %= MOD

    ans = dp1[N][M] % MOD
    print(ans)



if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
a = set(int(i) for i in input().split())

dp = [0] * (N + 1)
dp[0] = 1

for i in range(1,N+1):
    if i in a:
        dp[i] = 0
        continue
    dp[i] = (dp[i-1] + dp[i-2]) % 1000000007

print(dp[N])

Fixed submission:
N, M = map(int, input().split())
a = set(int(input()) for i in range(M))

dp = [0] * (N + 1)
dp[0] = 1

for i in range(1,N+1):
    if i in a:
        dp[i] = 0
        continue
    dp[i] = (dp[i-1] + dp[i-2]) % 1000000007

print(dp[N])

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
a = set(int(i) for i in input().split())

dp = [0] * (N + 1)
dp[0] = 1

for i in range(1,N+1):
    if i in a:
        dp[i] = 0
        continue
    dp[i] = dp[i-1] + dp[i-2]

print(dp[N])

Fixed submission:
N, M = map(int, input().split())
a = set(int(input()) for i in range(M))

dp = [0] * (N + 1)
dp[0] = 1

for i in range(1,N+1):
    if i in a:
        dp[i] = 0
        continue
    dp[i] = (dp[i-1] + dp[i-2]) % 1000000007

print(dp[N])

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

a = [int(input()) for i in range(M)]
dp = [0] * (N + 1)
dp[0] = 0
if 1 in a:
    dp[1] = 0
    dp[2] = 1
else:
    if 2 in a:
        dp[1] = 1
        dp[2] = 0
    else:
        dp[1] = 1
        dp[2] = 2
for i in range(3,N+1):
    if i in a:
        dp[i] = 0
    else:
        dp[i] = (dp[i-1] + dp[i-2]) % 1000000007

print(dp[N])

Fixed submission:
N, M = map(int, input().split())
a = set(int(input()) for i in range(M))

dp = [0] * (N + 1)
dp[0] = 1

for i in range(1,N+1):
    if i in a:
        dp[i] = 0
        continue
    dp[i] = (dp[i-1] + dp[i-2]) % 1000000007

print(dp[N])

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

a = [int(input()) for i in range(M)]
dp = [0] * (N + 1)
dp[0] = 0
if 1 in a:
    dp[1] = 0
    dp[2] = 1
else:
    if 2 in a:
        dp[1] = 1
        dp[2] = 0
    else:
        dp[1] = 1
        dp[2] = 2
for i in range(3,N+1):
    if i in a:
        dp[i] = 0
    else:
        dp[i] = (dp[i-1] + dp[i-2]) % 1000000007

print(dp[N])

Fixed submission:
N, M = map(int, input().split())
a = set(int(input()) for i in range(M))

dp = [0] * (N + 1)
dp[0] = 1

for i in range(1,N+1):
    if i in a:
        dp[i] = 0
        continue
    dp[i] = (dp[i-1] + dp[i-2]) % 1000000007

print(dp[N])
Buggy python submission (verdict: Runtime Error):
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0
    for l in range(K+1):
        for r in range(K+1-l):
            if l + r > K:
                continue
            D = K - l - r
            S = []
            now = 0
            for i in range(l):
                now += V[i]
                S.append(V[i])
            for j in range(N-1, N-1-r, -1):
                now += V[j]
                S.append(V[j])

            sort_S = sorted(S)
            for d in range(min(D,len(S))):
                if sort_S[d] < 0:
                    now += abs(sort_S[d])

            ans = max(ans, now)

    print(ans)
            

main()

Fixed submission:
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0
    for l in range(K+1):
        for r in range(K+1-l):
            if l + r > N:
                continue
            D = K - l - r
            S = []
            now = 0
            for i in range(l):
                now += V[i]
                S.append(V[i])
            for j in range(N-r,N):
                now += V[j]
                S.append(V[j])

            sort_S = sorted(S)
            for d in range(min(D,len(S))):
                if sort_S[d] < 0:
                    now += abs(sort_S[d])

            ans = max(ans, now)

    print(ans)
            

main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, M = map(int, input().split())
    #L = [0] * M
    #R = [0] * M
    
    for i in range(M):
        L, R = map(int, input().split())
        A = [j for j in range(L,R+1)]
        if i == 0:
            tmp = set(A)
            continue
        set_A = set(A)
        tmp = set(list(set_A.intersection(tmp)))
    
    print(len(tmp))



main()

Fixed submission:
def main():
    N, M = map(int, input().split())
    
    left = 0
    right = float('inf')
    
    for i in range(M):
        L, R = map(int, input().split())
        left = max(left, L)
        right = min(right, R)
    
    if left <= right:
        print(right - left + 1)
    else:
        print(0)

main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(4100000)
input = sys.stdin.readline

def dijkstra(s,N,cost):
    d = [float('inf')] * N
    used = [False] * N
    d[s] = 0

    while True:
        v = -1
        for i in range(N):
            if (not used[i]) and (v == -1 or d[i] < d[v]):
                v = i
        
        if v == -1:
            break
        
        used[v] = True
        for j in range(N):
            d[j] = min(d[j], d[v] + cost[v][j])
    return d


def main():
    N  = int(input())
    cost = [[float('inf')] * N for _ in range(N)]
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        u -= 1
        v -= 1
        cost[u][v] = w
        cost[v][u] = w

    d = dijkstra(0,N,cost)

    for i in range(N):
        if d[i] % 2 == 0:
            print(0)
        else:
            print(1)


if __name__ == "__main__":
    main()

Fixed submission:
import heapq
import sys

input = sys.stdin.readline

def dijkstra_heap(s,edge,N):
    d = [float('inf')] * N
    used = [True] * N
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist, a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

def main():
    N = int(input())
    edge = [[] for i in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split())
        u -= 1
        v -= 1
        edge[u].append([w,v])
        edge[v].append([w,u])

    D = dijkstra_heap(0,edge,N)
    first = D[0]
    for d in D:
        if abs(d - first) % 2 == 0:
            print(0)
        else:
            print(1)

main()

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

X = [0] * M
Y = [0] * M
Z = [0] * M
for i in range(M):
    X[i], Y[i], Z[i] = map(int, input().split())

X1 = list(set(X))

print(N-len(X1))

Fixed submission:
import heapq
import sys

input = sys.stdin.readline

def dijkstra_heap(s,edge,N):
    d = [float('inf')] * N
    used = [True] * N
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist, a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

def main():
    N = int(input())
    edge = [[] for i in range(N)]

    for i in range(N-1):
        u, v, w = map(int, input().split())
        u -= 1
        v -= 1
        edge[u].append([w,v])
        edge[v].append([w,u])

    D = dijkstra_heap(0,edge,N)
    first = D[0]
    for d in D:
        if abs(d - first) % 2 == 0:
            print(0)
        else:
            print(1)

main()
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
import fractions
import copy


def gcd(L):
    return reduce(fractions.gcd, L)

def main():
    N = int(input())
    A = list(map(int, input().split()))

    ans = 0
    for a in A:
        B = copy.deepcopy(A)
        B.remove(a)
        ans = max(ans,gcd(B))

    print(ans)






main()

Fixed submission:
import fractions


def main():
    N = int(input())
    A = list(map(int, input().split()))

    gcd_ans = []
 
    l = [0] * N
    r =[0] * N
    for i in range(N-1):
        l[i+1] = fractions.gcd(l[i],A[i])
        r[i+1] = fractions.gcd(r[i],A[N-1-i])
    r.reverse()
 
    for i in range(N):
        gcd_ans.append(fractions.gcd(l[i],r[i]))
 
    print(max(gcd_ans))


main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, Q = map(int, input().split())
    S = input()
    S_sum = [0] * (N+1)
    for i in range(1,N+1):
        S_sum[i] = S[:i].count('AC')

    for _ in range(Q):
        l, r = map(int, input().split())
        print(S_sum[r]-S_sum[l])


main()

Fixed submission:
def main():
    N, Q = map(int, input().split())
    S = input()
    S_sum = [0] * (N+1)
    for i in range(N):
        S_sum[i+1] = S_sum[i] + (1 if S[i:i+2] == 'AC' else 0)

    for _ in range(Q):
        l, r = map(int, input().split())
        print(S_sum[r-1]-S_sum[l-1])


main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    S = list(input())

    ans = 0
    while True:
        cnt = 0
        for i in range(1,len(S)):
            if S[i] != S[i-1]:
                S.pop(i)
                S.pop(i-1)
                cnt += 2
                break
        if cnt == 0:
            break
        ans += cnt

    print(ans)



main()

Fixed submission:
def main():
    S = list(input())

    print(min(S.count('0'),S.count('1'))*2)

    # 当たり前だけど全探索はTLEでアウト
    # ans = 0
    # while True:
    #    cnt = 0
    #    for i in range(1,len(S)):
    #        if S[i] != S[i-1]:
    #            S.pop(i)
    #            S.pop(i-1)
    #            cnt += 2
    #            break
    #    if cnt == 0:
    #        break
    #    ans += cnt

    #print(ans)



main()