Buggy python submission (verdict: Time Limit Exceeded):
N=input()
S=input()
N=int(N)
ans=0
ind=[]
tmp={"R":0,"G":0,"B":0}
for i in S:
    tmp[i]+=1
    ind.append(tmp.copy())
#print(ind)
for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        for k in tmp.keys():
            if k!=S[i] and k!=S[j]:
                col=k
                break
        #print(i,j,col)
        if 2*j-i <N and S[j+j-i]==col:
            ans-=1
        ans+=(ind[-1][col]-ind[j][col])
print(ans)


Fixed submission:
N=input()
S=input()
N=int(N)
ans=0
ind=[]
tmp={"R":0,"G":0,"B":0}
for i in S:
    tmp[i]+=1
    ind.append(tmp.copy())
#print(ind)
for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        for k in tmp.keys():
            if k!=S[i] and k!=S[j]:
                col=k
                break
        #print(i,j,col)
        if 2*j-i <N and S[j+j-i]==col:
            ans-=1
        ans+=(ind[-1][col]-ind[j][col])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math
def main():
    N=input()
    S=input()
    N=int(N)
    ans=0
    ind=[]
    tmp={"R":0,"G":0,"B":0}
    for i in S:
        tmp[i]+=1
        ind.append(tmp.copy())
    for i in range(0,N-2):
        for j in range(i+1,N-1):
            if S[i]==S[j]:
                continue
            col=[k for k in ["R","G","B"] if k!=S[i] and k!=S[j]]
            col=col[-1]
            if 2*j-i <N and S[j+j-i]==col:
                ans-=1
            ans+=(ind[-1][col]-ind[j][col])
    print(ans)

main()


Fixed submission:
N=input()
S=input()
N=int(N)
ans=0
ind=[]
tmp={"R":0,"G":0,"B":0}
for i in S:
    tmp[i]+=1
    ind.append(tmp.copy())
#print(ind)
for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        for k in tmp.keys():
            if k!=S[i] and k!=S[j]:
                col=k
                break
        #print(i,j,col)
        if 2*j-i <N and S[j+j-i]==col:
            ans-=1
        ans+=(ind[-1][col]-ind[j][col])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math
def main():
    #N,K,C=map(int,input().split())
    #A=list(map(int,input().split()))
    N=input()
    S=input()
    N=int(N)
    ans=0
    ind=[]
    tmp={"R":0,"G":0,"B":0}
    for i in S:
        tmp[i]+=1
        ind.append(tmp.copy())
    #print(ind)
    for i in range(0,N-2):
        for j in range(i+1,N-1):
            if S[i]==S[j]:
                continue
            for k in tmp.keys():
                if k!=S[i] and k!=S[j]:
                    col=k
                    break
            #print(i,j,col)
            if 2*j-i <N and S[j+j-i]==col:
                ans-=1
            ans+=(ind[-1][col]-ind[j][col])
    print(ans)

main()


Fixed submission:
N=input()
S=input()
N=int(N)
ans=0
ind=[]
tmp={"R":0,"G":0,"B":0}
for i in S:
    tmp[i]+=1
    ind.append(tmp.copy())
#print(ind)
for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        for k in tmp.keys():
            if k!=S[i] and k!=S[j]:
                col=k
                break
        #print(i,j,col)
        if 2*j-i <N and S[j+j-i]==col:
            ans-=1
        ans+=(ind[-1][col]-ind[j][col])
print(ans)