Buggy python submission (verdict: Time Limit Exceeded):
import copy
N = int(input())
S = input()
ans = 0
R = []
G = []
B = []
r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == "R":
        r += 1
    elif S[i] == "G":
        g += 1
    else:
        b += 1
    R.append(r)
    G.append(g)
    B.append(b)

for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            L = "RGB"
            L = L.replace(S[i],"",1)
            L = L.replace(S[k],"",1)
            if L == "R":
                ans += R[k]-R[i]
            if L == "G":
                ans += G[k]-G[i]
            if L == "B":
                ans += B[k]-B[i]
            if (k-i) % 2 == 0:
                if S[int((k-i)/2 + i)] == L:
                    ans -= 1


print(ans)

Fixed submission:
import copy
N = int(input())
S = input()

R = set()
G = set()
B = set()

for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "G":
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)

for r in R:
    for g in G:
        L = g - r
        if g + L in B:
            ans -= 1
        if r - L in B:
            ans -= 1
        if L % 2 == 0 and L//2 + r in B:
            ans -= 1
            



print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import copy
N = int(input())
S = input()
ans = 0

for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            L = "RGB"
            L = L.replace(S[i],"",1)
            L = L.replace(S[k],"",1)
            ans += S[i+1:k].count(L)
            if (k-i) % 2 == 0:
                if S[int((k-i)/2 + i)] == L:
                    ans -= 1


print(ans)

Fixed submission:
import copy
N = int(input())
S = input()

R = set()
G = set()
B = set()

for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "G":
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)

for r in R:
    for g in G:
        L = g - r
        if g + L in B:
            ans -= 1
        if r - L in B:
            ans -= 1
        if L % 2 == 0 and L//2 + r in B:
            ans -= 1
            



print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import copy
N = int(input())
S = input()
ans = 0

for i in range(N-3):
    A = ["R", "G", "B"]
    A.remove(S[i])
    for k in range(i+3, N):
        B = copy.copy(A)
        if S[i] != S[k]:
            B.remove(S[k])
            ans += S[i+1:k].count(B[0])
            if (k-i) % 2 == 0:
                if S[int((k-i)/2 + i)] == B[0]:
                    ans -= 1


print(ans)

Fixed submission:
import copy
N = int(input())
S = input()

R = set()
G = set()
B = set()

for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "G":
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)

for r in R:
    for g in G:
        L = g - r
        if g + L in B:
            ans -= 1
        if r - L in B:
            ans -= 1
        if L % 2 == 0 and L//2 + r in B:
            ans -= 1
            



print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(N-2):
    for k in range(i+3, N):
        if S[i] != S[k]:
            A = ["R", "G", "B"]
            A.remove(S[i])
            A.remove(S[k])
            ans += S[i+1:k].count(A[0])
            if (k-i) % 2 == 0:
                if S[int((k-i)/2 + i)] == A[0]:
                    ans -= 1


print(ans)

Fixed submission:
import copy
N = int(input())
S = input()

R = set()
G = set()
B = set()

for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "G":
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)

for r in R:
    for g in G:
        L = g - r
        if g + L in B:
            ans -= 1
        if r - L in B:
            ans -= 1
        if L % 2 == 0 and L//2 + r in B:
            ans -= 1
            



print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(N-3):
    for j in range(i+1, N-1):
        if S[i] != S[j]:
            for k in range(j+1, N):
                if j-i != k-j and S[i] != S[k] and S[j] != S[k]:
                    ans += 1
print(ans)

Fixed submission:
import copy
N = int(input())
S = input()

R = set()
G = set()
B = set()

for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "G":
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)

for r in R:
    for g in G:
        L = g - r
        if g + L in B:
            ans -= 1
        if r - L in B:
            ans -= 1
        if L % 2 == 0 and L//2 + r in B:
            ans -= 1
            



print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] != S[j]:
            for k in range(j+1, N):
                if j-i != k-j and S[i] != S[k] and S[j] != S[k]:
                    ans += 1
print(ans)

Fixed submission:
import copy
N = int(input())
S = input()

R = set()
G = set()
B = set()

for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "G":
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)

for r in R:
    for g in G:
        L = g - r
        if g + L in B:
            ans -= 1
        if r - L in B:
            ans -= 1
        if L % 2 == 0 and L//2 + r in B:
            ans -= 1
            



print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        for k in range(j+1, N):
            if S[i] == S[j]:
                break
            if S[i] == S[k] or S[j] == S[k]:
                pass
            else:
                if j-i != k-j:
                    ans += 1
print(ans)

Fixed submission:
import copy
N = int(input())
S = input()

R = set()
G = set()
B = set()

for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "G":
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)

for r in R:
    for g in G:
        L = g - r
        if g + L in B:
            ans -= 1
        if r - L in B:
            ans -= 1
        if L % 2 == 0 and L//2 + r in B:
            ans -= 1
            



print(ans)