Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
S = input()
h = [0]*N

for i, a in enumerate(S):
    if a=='R':
        h[i] = 0
    elif a=='G':
        h[i] = 1
    else:
        h[i] = 9

cnt = -1
ans = 0
for i in range(N):
    cnt = h[i]
    for j in range(i+1, N):
        num = cnt + h[j]
        if num == 1 or num == 9 or num==10:
            for k in range(j+1, N):
                if num + h[k] == 10 and k!=2*j-i:
                    ans += 1

print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
S = input()
h = [0]*N
h = 'RGB'*1000

#h = h.replace('R', '1').replace('G', '2').replace('B', '7')
'''
cnt = -1
ans = 0
for i in range(N):
    cnt = int(h[i])
    for j in range(i+1, N):
        num = cnt + int(h[j])
        if num == 3 or num == 9 or num==8:
            for k in range(j+1, N):
                if num + int(h[k]) == 10 and k!=2*j-i:
                    ans += 1
'''

#h = h.replace('R', '1').replace('G', '2').replace('B', '7')

c_r = c_g = c_b = 0
h = ''
for i, a in enumerate(S):
    if a == 'R':
        c_r += 1
        h += '1'
    if a == 'G':
        c_g += 1
        h += '2'
    if a == 'B':
        c_b += 1
        h += '7'

ans = c_r * c_g * c_b

for i in range(N):
    for j in range(i+1, N):
        if 2*j-i<N and int(h[i]) + int(h[j]) + int(h[2*j-i]) == 10:
            ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
S = input()
h = [0]*N

for i, a in enumerate(S):
    if a=='R':
        h[i] = 0
    elif a=='G':
        h[i] = 1
    else:
        h[i] = 9

cnt = -1
ans = 0
for i in range(N):
    cnt = h[i]
    for j in range(i+1, N):
        num = cnt + h[j]
        for k in range(j+1, N):
            if num + h[k] == 10 and k!=2*j-i:
                ans += 1

print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
S = input()
h = [0]*N
h = 'RGB'*1000

#h = h.replace('R', '1').replace('G', '2').replace('B', '7')
'''
cnt = -1
ans = 0
for i in range(N):
    cnt = int(h[i])
    for j in range(i+1, N):
        num = cnt + int(h[j])
        if num == 3 or num == 9 or num==8:
            for k in range(j+1, N):
                if num + int(h[k]) == 10 and k!=2*j-i:
                    ans += 1
'''

#h = h.replace('R', '1').replace('G', '2').replace('B', '7')

c_r = c_g = c_b = 0
h = ''
for i, a in enumerate(S):
    if a == 'R':
        c_r += 1
        h += '1'
    if a == 'G':
        c_g += 1
        h += '2'
    if a == 'B':
        c_b += 1
        h += '7'

ans = c_r * c_g * c_b

for i in range(N):
    for j in range(i+1, N):
        if 2*j-i<N and int(h[i]) + int(h[j]) + int(h[2*j-i]) == 10:
            ans -= 1
print(ans)