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

def solve(s,n):
    if "W" not in s or "R" not in s or s.find("WR")==-1:
        return 0

    else:
        arr=list(s)
        i=0
        j=n-1
        count=0
        brr=list(sorted(s))

        while arr!=brr:
            if arr[i]=="W" and arr[j]=="R":
                
                count += 1
                arr[i]="R"
                arr[j]="W"
                i+=1
                j-=1

            elif arr[i]=="W" and arr[j]=="W":
                j-=1
            elif arr[i]=="R" and arr[j]=="R":
                i+=1
            else:
                i+=1
                j-=1
        return count
print(solve(s,n))





Fixed submission:
n=int(input())
s=raw_input()

def solve(s,n):
    if "W" not in s or "R" not in s or s.find("WR")==-1:
        return 0

    else:
        arr=list(s)
        i=0
        j=n-1
        count=0

        while i<j:
            if arr[i]=="W" and arr[j]=="R":
                count += 1
                i+=1
                j-=1

            elif arr[i]=="W" and arr[j]=="W":
                j-=1
            elif arr[i]=="R" and arr[j]=="R":

                i+=1
            else:
                i+=1
                j-=1
        return count
print(solve(s,n))

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

def solve(s,n):
    if "W" not in s or "R" not in s:
        return 0
    if s.find("WR")==-1:
        return 0
    else:
        arr=list(s)
        i=0
        j=len(s)-1
        count=0
        r=s.count("R")
        w=s.count("W")
        s1="R"*r+"W"*w
        brr=list(s1)

        while arr!=brr:
            if arr[i]=="W":
                if arr[j]=="R":
                    count += 1
                    arr[i]="R"
                    arr[j]="W"
                    i+=1
                    j-=1

                else:
                    j-=1
            else:
                if arr[j]=="R":
                    i+=1
                else:
                    i+=1
                    j-=1
        return count
print(solve(s,n))





Fixed submission:
n=int(input())
s=raw_input()

def solve(s,n):
    if "W" not in s or "R" not in s or s.find("WR")==-1:
        return 0

    else:
        arr=list(s)
        i=0
        j=n-1
        count=0

        while i<j:
            if arr[i]=="W" and arr[j]=="R":
                count += 1
                i+=1
                j-=1

            elif arr[i]=="W" and arr[j]=="W":
                j-=1
            elif arr[i]=="R" and arr[j]=="R":

                i+=1
            else:
                i+=1
                j-=1
        return count
print(solve(s,n))
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
def fn(n):
  lis=[]
  i=1
  while i**2<=2*n:
    lis.append(i)
    i+=1
  rtn=0
  for i,a in enumerate(lis):
    for j in range(i,len(lis)):
      b=lis[j]
      for k in range(j,len(lis)):
        c=lis[k]
        if a**2+b**2+c**2==2*n:
          x,y,z=(a-b+c)/2,(a+b-c)/2,(b+c-a)/2
          if x%1==0 and y%1==0 and z%1==0: 
            if x>0 and y>0 and z>0:
              if x==y and y==z: rtn+=1
              elif x==y or y==z or z==x: rtn+=3
              else: rtn+=6
  return rtn

for i in range(1,N+1): print(fn(i))

Fixed submission:
N=int(input())
def fn(N):
  lis=[]
  i=1
  T=-1
  while i**2<=2*N:
    lis.append(i)
    i+=1
  rtn=[0]*N
  for i,a in enumerate(lis):
    for j in range(i,len(lis)):
      b=lis[j]
      for k in range(j,len(lis)):
        c=lis[k]
        tmp=a**2+b**2+c**2
        if tmp%2==0 and tmp<=2*N:
          x,y,z=(a-b+c)/2,(a+b-c)/2,(b+c-a)/2
          if x%1==0 and y%1==0 and z%1==0: 
            if x>0 and y>0 and z>0:
              if x==y and y==z: rtn[int(tmp/2)-1]+=1
              elif x==y or y==z or z==x: rtn[int(tmp/2)-1]+=3
              else: rtn[int(tmp/2)-1]+=6
  return rtn


lis=fn(N)
for i in range(N): print(lis[i])
Buggy python submission (verdict: Runtime Error):
n = int(input())
int ans = (n + 1) // 2
print(ans)
  

Fixed submission:
n = int(input())
ans = (n + 1) // 2;
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
int ans = (n + 1) // 2;
print(ans)
  

Fixed submission:
n = int(input())
ans = (n + 1) // 2;
print(ans)
Buggy python submission (verdict: Runtime Error):
s = inpput()
if ''.join(sorted(s)) == 'abc':
  print('Yes')
else:
  print('No')

Fixed submission:
s = input()
if ''.join(sorted(s)) == 'abc':
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def solve(inp):
    S = inp.readline().strip()
    Q = int(inp.readline().strip())
    Queries = [inp.readline().strip() for i in range(Q)]

    r = S
    dir = False
    for Query in Queries:
        if Query == "1":
            dir = not dir
        else:
            (T, F, C) = Query.split(' ')
            if dir == (F == "1"):
                r = r + C
            else:
                r = C + r

    if dir:
        r = "".join(reversed(r))

    return r


def main():
    import sys
    result = solve(sys.stdin)
    if result is not None and result != '':
        print(result)


if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def solve(inp):
    S = inp.readline().strip()
    Q = int(inp.readline().strip())
    Queries = [inp.readline().strip() for i in range(Q)]

    l = []
    r = []
    dir = False
    for Query in Queries:
        if Query == "1":
            dir = not dir
        else:
            (T, F, C) = Query.split(' ')
            if dir == (F == "1"):
                r.append(C)
            else:
                l.append(C)

    if dir:
        return "".join(reversed(r)) + "".join(reversed(S)) + "".join(l)
    else:
        return "".join(reversed(l)) + S + "".join(r)


def main():
    import sys
    result = solve(sys.stdin)
    if result is not None and result != '':
        print(result)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def solve(inp):
    N = int(inp.readline().strip())
    H = list(map(int, inp.readline().strip().split(' ')))

    r = 0
    for i in range(N):
        r0 = 0
        prev = H[i]
        for j in range(i + 1, N):
            if prev < H[j]:
                break
            r0 += 1
            prev = H[j]
        r = max(r, r0)

    return str(r)


def main():
    import sys
    result = solve(sys.stdin)
    if result:
        print(result)


if __name__ == '__main__':
    main()


Fixed submission:
def solve(inp):
    N = int(inp.readline().strip())
    H = list(map(int, inp.readline().strip().split(' ')))

    r = 0
    i = 0
    while i < N:
        r0 = 0
        prev = H[i]
        j = i + 1
        while j < N:
            if prev < H[j]:
                break
            r0 += 1
            prev = H[j]
            j += 1
        r = max(r, r0)
        i = j

    return str(r)


def main():
    import sys
    result = solve(sys.stdin)
    if result:
        print(result)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
# もらうdpでとく
N, K = map(int, input().split())  # N個のマス, K個の集合
move = []
for _ in range(K):
    l, r = map(int, input().split())
    for d in range(l, r+1):
        move.append(d)
move.sort()

MOD = 998244353
dp = [0]*(N)  # スタート地点0~ゴール地点N-1までのN個
dp[0] = 1
for i in range(N):
    for d in move:
        if i-d < 0:  # 遷移元がないならcontinue
            continue
        dp[i] += dp[i-d]
        dp[i] %= MOD
print(dp[N-1])


Fixed submission:
"Binary Indexed Tree"
"""
- iが与えられたときa1~aiまでの和を計算する O(log n)
- iとxが与えられたときai += x とする O(log n)
"""
# BIT: Binary Indexed Tree (1-indexed), 0-indexだと困るので。
# A1~Aiまでの和 O(logN)


def BIT_query(idx):
    res_sum = 0
    while idx > 0:
        res_sum += BIT[idx]
        idx -= idx&(-idx)  # 次に求める区間のindexは今見ているindexから今見ている区間の長さ(これはidx&(-idx)で求めることができる)を引くことでindexを求めることができる
    return res_sum


# Ai += x  O(logN)
def BIT_updata(idx, x):
    while idx <= N:
        BIT[idx] += x
        idx += (idx&(-idx))  # 次に更新すべき区間のindexは今見ているindexに今見ている区間の長さを足すことでindexを求めることができる
    return


# n = 8
# BIT = [0]*(n+1)  # 最初に0で初期化する
# a = [1, 2, 3, 4, 5, 6, 7, 8]
# for i, e in enumerate(a):
#     BIT_updata(i+1, e)
#
#
# # A0~A2の和: 6
# print(BIT_query(3))  # ここのアクセスは1-indexで考えている
# # A3~A6の和: 18
# print(BIT_query(6)-BIT_query(2))  # ここのアクセスは1-indexで考えている
# print(BIT)

# BIT_queryで10~15マス前の和を現在のところに加える
N, K = map(int, input().split())
move = []
for _ in range(K):
    l, r = map(int, input().split())
    move.append([l, r])
MOD = 998244353
BIT = [0]*(N+1)  # スタート地点1~ゴール地点NまでのN個 (1-indexed)
BIT_updata(1, 1)
for i in range(2, N+1):
    for l, r in move:
        # lマス前からrマス前までの区間和を現在のマスに加える
        BIT_updata(i, (BIT_query(i-l)-BIT_query(i-r-1))%MOD)
ans = BIT_query(N) - BIT_query(N-1)
print(ans%MOD)
# BIT_query(N-10) - BIT_query(N-15)を現在のところにタス
# いや，微妙に違う?
Buggy python submission (verdict: Time Limit Exceeded):
N, X, M = map(int, input().split())  # 数列の項数N, 数列の第一項X, modM
MOD = M
A = [X]
# 1~N+1のN項
for _ in range(N-1):
    A.append(A[-1]**2%MOD)
print(sum(A))


Fixed submission:
# happendはsetで保持しておき，roopはlistで保持しておく
N, X, M = map(int, input().split())  # modMは10**5以下が与えられる
happend = set()
A = [X]
MOD = M
# 第1~N項まで計N項の和をもとめる
start = 0
for _ in range(N-1):
    if A[-1]**2 % MOD not in happend:
        happend.add(A[-1]**2 % MOD)
        A.append(A[-1]**2 % MOD)
    else:
        start = A.index(A[-1]**2 % MOD)
        break

roop_count = (N-start)//len(A[start:])

ans = sum(A[:start]) + sum(A[start:])*roop_count + sum(A[start:start+(N-start) % len(A[start:])])
# print(A[:start], A[start:], A[start:start+(N-start) % len(A[start:])])
print(ans)
Buggy python submission (verdict: Runtime Error):
from collections import deque
H, W = map(int, input().split())
CY, CX = map(lambda x: int(x)-1, input().split())
DY, DX = map(lambda x: int(x)-1, input().split())

gridgraph = [input() for _ in range(H)]
q = deque()
q.append([CY, CX])
# 更新されるなら追加
adjacent = [[1, 0], [0, 1], [-1, 0], [0, -1]]
INF = float("inf")
distant = [[INF]*W for _ in range(H)]
distant[CY][CX] = 0
visited = set()
while len(q):
    cy, cx = q.popleft()
    if (cy, cx) in visited:
        continue
    visited.add((cy, cx))
    for dy, dx in adjacent:
        ny, nx = cy+dy, cx+dx
        if ny < 0 or ny >= H or nx < 0 or nx >= W:
            continue
        if gridgraph[ny][nx] == "." and distant[ny][nx] > distant[cy][cx]:
            distant[ny][nx] = distant[cy][cx]
            q.appendleft([ny, nx])
    for i in range(-2, 3, 1):
        for j in range(-2, 3, 1):
            target_y, target_x = cy+i, cx+j
            if target_y < 0 or target_y >= H or target_x < 0 or target_x >= W:
                continue
            if gridgraph[target_y][target_x] == "." and distant[target_y][target_x] > distant[cy][cx]+1:
                distant[target_y][target_x] = distant[cy][cx]+1
                q.append([target_y, target_x])
if distant[DY][DX] == INF:
    print(-1)
else:
    print(distant[DY][DX])


Fixed submission:
H, W, M = map(int, input().split())
row = [0]*H  # 行
column = [0]*W  # 列

target = set()
for _ in range(M):
    # h行, w列に爆破対象がある
    h, w = map(lambda x: int(x)-1, input().split())
    row[h] += 1
    column[w] += 1
    target.add((h, w))

max_row_value = max(row)
max_column_value = max(column)
max_row_idx = []
max_column_idx = []

for i in range(H):
    if row[i] == max_row_value:
        max_row_idx.append(i)
for j in range(W):
    if column[j] == max_column_value:
        max_column_idx.append(j)

# なければ出力して即終了
# print(max_row_idx)
# print(max_column_idx)
# print(target)
for r in max_row_idx:
    for c in max_column_idx:
        if (r, c) not in target:
            # この式がFalseになるのは，(r, c)に爆発対象があるときのみ，それはたかだか10**5回程度
            # 必ず上記の条件式がTrueになるときがくる
            # (r, c) in targetがTrueになるのは10**5回程度なので！
            print(max_row_value+max_column_value)
            exit()

print(max_row_value+max_column_value-1)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))

# imos法して，もう一回同じことを行う
# K回のシミューレション，途中で全てがKになったら打ち切る
for _ in range(K):
    new = [0] * N
    imos = [0] * N
    for i, a in enumerate(A):  # 0-indexにしても問題ない
        imos[max(0, i-a)] += 1
        if i+a+1 <= N-1:  # 右端まで照らすなら，終端は管理しない
            imos[i+a+1] -= 1
    for i in range(N):
        new = new[i-1] + imos[i]
    # Aを上書き
    A = new
    if sum(A) == N*N:
        break
print(*A)
    

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

# imos法して，もう一回同じことを行う
# K回のシミューレション，途中で全てがKになったら打ち切る
for _ in range(K):
    new = [0] * N
    imos = [0] * N
    for i, a in enumerate(A):  # 0-indexにしても問題ない
        imos[max(0, i-a)] += 1
        if i+a+1 <= N-1:  # 右端まで照らすなら，終端は管理しない
            imos[i+a+1] -= 1
    for i in range(N):
        new[i] = new[i-1] + imos[i]
    # Aを上書き
    A = new
    if sum(A) == N*N:
        break
print(*A)
Buggy python submission (verdict: Runtime Error):
A, B = map(str, input().split())
A = int(A)
B = B.strip(".")
B = int(B)
print(A*B//100)


Fixed submission:
A, B = map(str, input().split())
A = int(A)
B = B.replace(".", "")
B = int(B)
print(A*B//100)

Buggy python submission (verdict: Runtime Error):
A, B = map(float, input().split())
print("{.0f}".format(A*B))


Fixed submission:
A, B = map(str, input().split())
A = int(A)
B = B.replace(".", "")
B = int(B)
print(A*B//100)
Buggy python submission (verdict: Runtime Error):
T = str(input())
T += "P"
ans = ""

# 末尾が"P"なら無条件でD, 末尾が"D"なら今見ている文字の次が"P"なら"D", "D"なら"P"

for i in range(len(T)-1):
    if T[i] == "?":
        if ans[-1] == "P":
            ans += "D"
        elif ans[-1] == "D":
            if T[i+1] == "P":
                ans += "D"
            elif T[i+1] in ["D", "?"]:
                ans += "P"
    else:
        ans += T[i]
print(ans)


Fixed submission:
T = str(input())
T += "P"
ans = ""

# 末尾が"P"なら無条件でD, 末尾が"D"なら今見ている文字の次が"P"なら"D", "D"なら"P"

for i in range(len(T)-1):
    if i == 0 and T[i] == "?":
        if T[i+1] == "P":
            ans += "D"
        else:
            ans += "P"
    else:
        if T[i] == "?":
            if ans[-1] == "P":
                ans += "D"
            elif ans[-1] == "D":
                if T[i+1] == "P":
                    ans += "D"
                elif T[i+1] in ["D", "?"]:
                    ans += "P"
        else:
            ans += T[i]
print(ans)
Buggy python submission (verdict: Runtime Error):
# 深さdの上限を先に決める
N = int(input())
# Aは，葉として残す必要があるものの数
A = list(map(int, input().split()))
# 例外の処理をもうちょい詰める
limit = [1]
for i in range(1, N+1):

    if i == N:
        limit.append(A[i])
    else:
        limit.append((limit[-1]-A[i-1])*2)
if N == 0:
    if A[0] != 1:
        print(-1)
        exit()
    # if A[0] == 0:
    #     print(3-A[-1])
    #     exit()
    # else:
    #     print(-1)
    #     exit()

# 最後にsumをとる
ans = [A[-1]]
now_leaf = A[-1]
for i in range(N-1, -1, -1):
    # print(A[i])
    ans.append(min(now_leaf+A[i], limit[i]))
    now_leaf = ans[-1]
# 構築したものに対してジャッジする, 葉を確保できているかどうか
ans.reverse()

for i in range(N):
    if (ans[i]-A[i])*2 >= A[i+1]:
        continue
    else:
        print(-1)
        exit()

print(sum(ans))


Fixed submission:
# 深さdの上限を先に決める
N = int(input())
# Aは，葉として残す必要があるものの数
A = list(map(int, input().split()))

# 深さdのノード数の上限
limit = [1]
for i in range(1, N+1):
    if i == N:  # 一番深いところの上限はA[N]
        limit.append(A[i])
    else:
        limit.append((limit[-1]-A[i-1])*2)
        if limit[-1] < 0:
            print(-1)
            exit()

# Nが0の時の処理
if N == 0:
    if A[0] != 1:
        print(-1)
        exit()

# 一番深いところから構築して最後にsumをとる
ans = [A[-1]]
now_leaf = A[-1]  # 一番深いところから構築しており，今現在の葉の数
for i in range(N-1, -1, -1):
    # print(A[i])
    ans.append(min(now_leaf+A[i], limit[i]))
    now_leaf = ans[-1]
# 構築したものに対してジャッジする, 葉を確保できているかどうか
for i in range(N):
    # ans[N-i]とすることで，今は一番浅いところから見ている
    if (ans[N-i]-A[i])*2 < A[i+1]:  # 葉が確保できているか
        print(-1)
        exit()
print(sum(ans))
Buggy python submission (verdict: Runtime Error):
from math import degrees, atan2
from collections import defaultdict

MOD = 10**9+7
N = int(input())
# y, xの順に渡すので注意
# print(math.degrees(math.atan2(3**(1/2), 1)))

points = [list(map(int, input().split())) for _ in range(N)]
tmp = defaultdict(int)
hoge = defaultdict(lambda: defaultdict(int))
for i in range(N):
    a, b = points[i]  # bをy座標, aをx座標と考える
    v = degrees(atan2(b, a))
    if v > 0:
        tmp[degrees(atan2(b, a)) % -90] += 1
        tmp[degrees(atan2(b, a)) % 90] += 1
    else:
        tmp[degrees(atan2(b, a)) % -90] += 1
        tmp[degrees(atan2(b, a)) % 90] += 1
# 90になる
# 絶対値にしてmod90で同じものがあるかどうか
# 数えることができている


ans = pow(2, N, MOD)
print(ans)
for k, v in tmp.items():
    if k > 0:
        ans -= v * tmp[k%(-90)]
    else:
        ans -= v * tmp[k%(90)]
print(ans-1)


Fixed submission:
from math import gcd

N = int(input())
# 常にy座標の方を正にする
zero = 0
d = {}
for _ in range(N):
    a, b = map(int, input().split())
    if a == 0 and b == 0:
        zero += 1
        continue
    # 最大公約数で割って互いに素である状態にする（既約分数にする）
    g = gcd(a, b)
    a //= g
    b //= g
    # 常にbを正の状態に保つ
    if b < 0:
        a = -a
        b = -b
    if b == 0 and a == -1:
        a = 1

    if a > 0:
        if (a, b) in d:
            d[(a, b)][0] += 1
        else:
            d[(a, b)] = [1, 0]
    else:
        if (b, -a) in d:
            d[(b, -a)][1] += 1
        else:
            d[(b, -a)] = [0, 1]
MOD = 10**9+7
ans = 1
for (a, b), (k, l) in d.items():
    ans *= (pow(2, k, MOD)-1) + (pow(2, l, MOD)-1) + 1
    ans %= MOD
ans += zero
ans -= 1
print(ans%MOD)
Buggy python submission (verdict: Runtime Error):
import sys
from copy import deepcopy
sys.setrecursionlimit(10**7)


def dfs(now):
    global ans
    if len(now) == N:
        tmp_ans = 0
        for a, b, c, d in query:
            a -= 1
            b -= 1
            if now[b] - now[a] == c:
                tmp_ans += d
        ans = max(tmp_ans, ans)
    else:
        for i in range(now[-1], M+1):
            tmp = deepcopy(now)
            tmp.append(i)
            dfs(tmp)


N, M, Q = map(int, input().split())  # N個の数列, 上限M, Q個のクエリ
query = [list(map(int, input().split())) for _ in range(Q)]

A = [1]
ans = 0
dfs(A)
print(ans)


Fixed submission:
import math
A, B, N = map(int, input().split())
ans = math.floor(A*min(N, B-1)/B) - A*math.floor(min(N, B-1)/B)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()[::-1]
N = len(S)
sum_of_mod = [0] * 2019
sum_of_mod[0] += 1
digit = 0
now = 0
for i in range(N):
    now = int(S[i])*(10**digit) + now
    sum_of_mod[now % 2019] += 1
    digit += 1

ans = 0
for i in range(2019):
    ans += sum_of_mod[i] * (sum_of_mod[i]-1) // 2
print(ans)


Fixed submission:
S = input()[::-1]
N = len(S)
sum_of_mod = [0] * 2019
sum_of_mod[0] += 1
digit = 1
now = 0
for i in range(N):
    now = int(S[i])*digit + now
    now %= 2019
    digit *= 10
    digit %= 2019
    sum_of_mod[now] += 1

ans = 0
for i in range(2019):
    ans += sum_of_mod[i] * (sum_of_mod[i]-1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()[::-1]
sum_of_mod = [0] * 2019
now = int(S[0])
digit = 1
sum_of_mod[now % 2019] += 1
for c in S:
    now = int(c)*(10**digit) + now
    sum_of_mod[now % 2019] += 1
    digit += 1
ans = 0
for m in sum_of_mod:
    ans += m * (m-1) // 2
print(ans)


Fixed submission:
S = input()[::-1]
N = len(S)
sum_of_mod = [0] * 2019
sum_of_mod[0] += 1
digit = 1
now = 0
for i in range(N):
    now = int(S[i])*digit + now
    now %= 2019
    digit *= 10
    digit %= 2019
    sum_of_mod[now] += 1

ans = 0
for i in range(2019):
    ans += sum_of_mod[i] * (sum_of_mod[i]-1) // 2
print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())  # 0~NのN+1個の数字, K個以上選んで出来上がる数
ans = 0
MOD = 10**9+7
# 出来上がる数字の幅を考える
for k in range(K, N+2):  # K~N+1までループを回す
  upper = k*(2N-(k-1))//2
  lower = k*(k-1)//2
  ans += upper - lower + 1
  ans %= MOD
print(ans)

Fixed submission:
N, K = map(int, input().split())  # 0~NのN+1個の整数, K個以上選ぶ, 注意 ; 3, 5 と 1, 3, 4は別物になる。0と5, 2と3 を選んだパターンは同じになる

MOD = 10**9+7
ans = 0
for k in range(K, N+2):
    ans += k*(2*N-(k-1))//2 - k*(k-1)//2 + 1
    ans %= MOD
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = str(input())
S += "x"*4001
c = Counter(S)
after_happen = {}
after_happen[0] = c.copy()
after_happen[0][S[0]] -= 1

for i in range(1, N):
    after_happen[i] = after_happen[i-1].copy()
    after_happen[i][S[i]] -= 1
# print(after_happen)
ans = 0
rgb = set(["R", "G", "B"])
for i in range(N-2):
    for j in range(i+1, N-1):
        # 同じ文字ならcontinue
        if S[i] == S[j]:
            continue
        # jより後ろにtargetがいくつ存在するか確認する, after_happen[j] := jより後ろの情報
        target = rgb - set([S[i], S[j]])
        target = list(target)[0]
        ans += after_happen[j][target]
        if S[j+(j-i)] == target:
            ans -= 1
print(ans)


Fixed submission:
from collections import Counter
N = int(input())
S = str(input())
S += "x"*4000
all_string = Counter(S)

after_happen = {0: all_string}
for i in range(1, N+1):
    tmp = after_happen[i-1].copy()
    after_happen[i] = tmp
    after_happen[i][S[i-1]] -= 1
ans = 0
rgb = set(["R", "G", "B"])
for i in range(N-2):
    for j in range(i+1, N-1):
        # 最後に引くのを忘れない
        # j+1以降で探す
        if S[i] == S[j]:
            continue
        target = rgb-set([S[i], S[j]])
        target = list(target)[0]
        # print(target)
        ans += after_happen[j][target]
        # 余分に数えている分を引きたい
        if S[j+(j-i)] == target:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = str(input())
S += "x"*4001
c = Counter(S)
after_happen = {}
after_happen[0] = c.copy()
after_happen[0][S[0]] -= 1

for i in range(1, N):
    after_happen[i] = after_happen[i-1].copy()
    after_happen[i][S[i]] -= 1
# print(after_happen)
ans = 0
rgb = set(["R", "G", "B"])
for i in range(N-2):
    for j in range(i+1, N-1):
        # 同じ文字ならcontinue
        if S[i] == S[j]:
            continue
        # jより後ろにtargetがいくつ存在するか確認する, after_happen[j] := jより後ろの情報
        target = rgb - set([S[i], S[j]])
        target = list(target)[0]
        ans += after_happen[j][target]
        if S[j+(j-i)] == target:
            ans -= 1
print(ans)


Fixed submission:
from collections import Counter
N = int(input())
S = str(input())
S += "x"*4000
all_string = Counter(S)

after_happen = {0: all_string}
for i in range(1, N+1):
    tmp = after_happen[i-1].copy()
    after_happen[i] = tmp
    after_happen[i][S[i-1]] -= 1
ans = 0
rgb = set(["R", "G", "B"])
for i in range(N-2):
    for j in range(i+1, N-1):
        # 最後に引くのを忘れない
        # j+1以降で探す
        if S[i] == S[j]:
            continue
        target = rgb-set([S[i], S[j]])
        target = list(target)[0]
        # print(target)
        ans += after_happen[j][target]
        # 余分に数えている分を引きたい
        if S[j+(j-i)] == target:
            ans -= 1
print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()
Q = int(input())
flag = True  # flagがTrueのとき、偶数回数の反転が行われている
cnt = 0
for _ in range(Q):
    query = list(map(str, input().split()))
    # tmp.append(query)
    if len(query) == 1:  # 1のqueryのとき
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = query[1], query[2]
        if f == "1":  # 先頭に追加するもの
            if flag:
                s = c + s  # 偶数回の反転なら素直に先頭に追加する
            else:
                s += c  # 奇数会の反転なら後ろに追加する
        else:  # 末尾に追加する
            if flag:
                s += c
            else:
                s = c + s

if cnt % 2 == 1 and cnt != 0:
    s = s[::-1]
print(s)


Fixed submission:
S = input()
if "A" in S and "B" in S:
    print("Yes")
else:
    print("No")