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

N = int(input())
S = input()

counter = Counter(S)
r_count = counter['R']
g_count = counter['G']
b_count = counter['B']

ans = r_count * g_count * b_count
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k > N-1:
            break
        if i < j < k and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()

counter = Counter(S)
r_count = counter['R']
g_count = counter['G']
b_count = counter['B']

ans = r_count * g_count * b_count
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k > N-1:
            break
        if i < j < k and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

def main():
    N = int(input())
    S = input()
    
    R_index = []
    G_index = []
    B_index = []
    
    for i, s in enumerate(S):
        if s == 'R':
            R_index.append(i)
        elif s == 'G':
            G_index.append(i)
        elif s == 'B':
            B_index.append(i)
    
    ans = 0
    for i in R_index:
        sj = bisect_left(G_index, i)
        for ij in range(sj, len(G_index)):
            j = G_index[ij]
            sk = bisect_left(B_index, j)
            for ik in range(sk,len(B_index)):
                k = B_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
        sj = bisect_left(B_index, i)
        for ij in range(sj,len(B_index)):
            j = B_index[ij]
            sj = bisect_left(G_index, j)
            for ik in range(sj,len(G_index)):
                k = G_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
    for i in G_index:
        sj = bisect_left(B_index, i)
        for ij in range(sj, len(B_index)):
            j = B_index[ij]
            sk = bisect_left(R_index, j)
            for ik in range(sk,len(R_index)):
                k = R_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
        sj = bisect_left(R_index, i)
        for ij in range(sj,len(R_index)):
            j = R_index[ij]
            sj = bisect_left(B_index, j)
            for ik in range(sj,len(B_index)):
                k = B_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
    for i in B_index:
        sj = bisect_left(R_index, i)
        for ij in range(sj, len(R_index)):
            j = R_index[ij]
            sk = bisect_left(G_index, j)
            for ik in range(sk,len(G_index)):
                k = G_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
        sj = bisect_left(G_index, i)
        for ij in range(sj,len(G_index)):
            j = G_index[ij]
            sj = bisect_left(R_index, j)
            for ik in range(sj,len(R_index)):
                k = R_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
from collections import Counter

N = int(input())
S = input()

counter = Counter(S)
r_count = counter['R']
g_count = counter['G']
b_count = counter['B']

ans = r_count * g_count * b_count
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k > N-1:
            break
        if i < j < k and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

N = int(input())
S = input()

R_index = []
G_index = []
B_index = []

for i, s in enumerate(S):
    if s == 'R':
        R_index.append(i)
    elif s == 'G':
        G_index.append(i)
    elif s == 'B':
        B_index.append(i)

ans = 0
for i in R_index:
    sj = bisect_left(G_index, i)
    for ij in range(sj, len(G_index)):
        j = G_index[ij]
        sk = bisect_left(B_index, j)
        for ik in range(sk,len(B_index)):
            k = B_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

    sj = bisect_left(B_index, i)
    for ij in range(sj,len(B_index)):
        j = B_index[ij]
        sj = bisect_left(G_index, j)
        for ik in range(sj,len(G_index)):
            k = G_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

for i in G_index:
    sj = bisect_left(B_index, i)
    for ij in range(sj, len(B_index)):
        j = B_index[ij]
        sk = bisect_left(R_index, j)
        for ik in range(sk,len(R_index)):
            k = R_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

    sj = bisect_left(R_index, i)
    for ij in range(sj,len(R_index)):
        j = R_index[ij]
        sj = bisect_left(B_index, j)
        for ik in range(sj,len(B_index)):
            k = B_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

for i in B_index:
    sj = bisect_left(R_index, i)
    for ij in range(sj, len(R_index)):
        j = R_index[ij]
        sk = bisect_left(G_index, j)
        for ik in range(sk,len(G_index)):
            k = G_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

    sj = bisect_left(G_index, i)
    for ij in range(sj,len(G_index)):
        j = G_index[ij]
        sj = bisect_left(R_index, j)
        for ik in range(sj,len(R_index)):
            k = R_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()

counter = Counter(S)
r_count = counter['R']
g_count = counter['G']
b_count = counter['B']

ans = r_count * g_count * b_count
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k > N-1:
            break
        if i < j < k and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

N = int(input())
S = input()

R_index = []
G_index = []
B_index = []

for i, s in enumerate(S):
    if s == 'R':
        R_index.append(i)
    elif s == 'G':
        G_index.append(i)
    elif s == 'B':
        B_index.append(i)

ans = 0
for i in R_index:
    sj = bisect_left(G_index, i)
    for ij in range(sj, len(G_index)):
        j = G_index[ij]
        sk = bisect_left(B_index, j)
        for ik in range(sk,len(B_index)):
            k = B_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

    sj = bisect_left(B_index, i)
    for ij in range(sj,len(B_index)):
        j = B_index[ij]
        sj = bisect_left(G_index, j)
        for ik in range(sj,len(G_index)):
            k = G_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

for i in G_index:
    sj = bisect_left(B_index, i)
    for ij in range(sj, len(B_index)):
        j = B_index[ij]
        sk = bisect_left(R_index, j)
        for ik in range(sk,len(R_index)):
            k = R_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

    sj = bisect_left(R_index, i)
    for ij in range(sj,len(R_index)):
        j = R_index[ij]
        sj = bisect_left(B_index, j)
        for ik in range(sj,len(B_index)):
            k = B_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

for i in B_index:
    sj = bisect_left(R_index, i)
    for ij in range(sj, len(R_index)):
        j = R_index[ij]
        sk = bisect_left(G_index, j)
        for ik in range(sk,len(G_index)):
            k = G_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

    sj = bisect_left(G_index, i)
    for ij in range(sj,len(G_index)):
        j = G_index[ij]
        sj = bisect_left(R_index, j)
        for ik in range(sj,len(R_index)):
            k = R_index[ik]
            if i < j < k and j - i != k - j:
                ans += 1

print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()

counter = Counter(S)
r_count = counter['R']
g_count = counter['G']
b_count = counter['B']

ans = r_count * g_count * b_count
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k > N-1:
            break
        if i < j < k and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)

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

N = int(input())
S = input()

R_index = []
G_index = []
B_index = []

for i, s in enumerate(S):
    if s == 'R':
        R_index.append(i)
    elif s == 'G':
        G_index.append(i)
    elif s == 'B':
        B_index.append(i)

ans = 0
for i in R_index:
    for j in G_index:
        for k in B_index:
            if i < j < k and j - i != k - j:
                ans += 1
    for j in B_index:
        for k in G_index:
            if i < j < k and j - i != k - j:
                ans += 1

for i in G_index:
    for j in R_index:
        for k in B_index:
            if i < j < k and j - i != k - j:
                ans += 1
    for j in B_index:
        for k in R_index:
            if i < j < k and j - i != k - j:
                ans += 1

for i in B_index:
    for j in G_index:
        for k in R_index:
            if i < j < k and j - i != k - j:
                ans += 1
    for j in R_index:
        for k in G_index:
            if i < j < k and j - i != k - j:
                ans += 1

print(ans)

Fixed submission:
from collections import Counter

N = int(input())
S = input()

counter = Counter(S)
r_count = counter['R']
g_count = counter['G']
b_count = counter['B']

ans = r_count * g_count * b_count
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k > N-1:
            break
        if i < j < k and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)