Buggy python submission (verdict: Time Limit Exceeded):
from math import gcd
import sys
input = sys.stdin.readline

n = int(input())
s = input().strip()
R = [0] * (n + 1)
G = [0] * (n + 1)
B = [0] * (n + 1)
ans = 0
for i in range(len(s)):
    R[i + 1] = R[i]
    G[i + 1] = G[i]
    B[i + 1] = B[i]
    if s[i] == 'R':
        R[i + 1] += 1
    elif s[i] == 'G':
        G[i + 1] += 1
    else:
        B[i + 1] += 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'R' and s[j] == 'B':
            ans += G[j + 1] - G[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'G':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'R' and s[j] == 'G':
            ans += B[j + 1] - B[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'B':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'B' and s[j] == 'R':
            ans += G[j + 1] - G[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'G':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'B' and s[j] == 'G':
            ans += R[j + 1] - R[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'R':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'G' and s[j] == 'R':
            ans += B[j + 1] - B[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'B':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'G' and s[j] == 'B':
            ans += R[j + 1] - R[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'R':
                ans -= 1


print(ans)


Fixed submission:
from math import gcd
import sys
input = sys.stdin.readline

n = int(input())
s = input().strip()
R = [0] * (n + 1)
G = [0] * (n + 1)
B = [0] * (n + 1)
ans = 0
for i in range(len(s)):
    R[i + 1] = R[i]
    G[i + 1] = G[i]
    B[i + 1] = B[i]
    if s[i] == 'R':
        R[i + 1] += 1
    elif s[i] == 'G':
        G[i + 1] += 1
    else:
        B[i + 1] += 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'R' and s[j] == 'B':
            ans += G[j + 1] - G[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'G':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'R' and s[j] == 'G':
            ans += B[j + 1] - B[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'B':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'B' and s[j] == 'R':
            ans += G[j + 1] - G[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'G':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'B' and s[j] == 'G':
            ans += R[j + 1] - R[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'R':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'G' and s[j] == 'R':
            ans += B[j + 1] - B[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'B':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'G' and s[j] == 'B':
            ans += R[j + 1] - R[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'R':
                ans -= 1


print(ans)