Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin,stdout


def convert(x):
    if x == 'R': return 0
    if x == 'G': return 1
    if x == 'B': return 2
    

def solve(S,N):
    presum = [[0 for _ in range(3)] for _ in range(N + 1)]
    
    for i in range(N):
        for j in range(3): 
            presum[i + 1][j] += presum[i][j]
        presum[i + 1][S[i]] += 1

    #Exclusion 

    
    sumall = 0
    for i in range(N - 2):
        for k in range(i + 2,N):
            if S[i] != S[k]:
                c = 3 - S[i] - S[k] 
                sumall += presum[k][c] - presum[i + 1][c]
                
                if (i + k) % 2 == 0:
                    j = (i + k)//2
                    if S[j] == c: sumall -= 1
    return str(sumall)


if __name__ == "__main__": 
    #Input 
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    S = [convert(x) for x in S]
    
    stdout.write(solve(S,N))
    





Fixed submission:
from sys import stdin,stdout


def solve(S,N):
    color = [[] for _ in range(3)]

    for i in range(N):
        if S[i] == 'R': color[0].append(i)
        elif S[i] == 'G': color[1].append(i)
        else: color[2].append(i)
    
    
    #Exclusion 
    total = len(color[0])*len(color[1])*len(color[2])

    remain = 0
    for i in range(N - 2):
        for j in range(i + 1,N - 1):
            k = j + j - i
            if k >= N: break
            
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                remain += 1
    return total - remain


    


if __name__ == "__main__": 
    #Input 
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    stdout.write(str(solve(S,N)))

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin,stdout


def convert(x):
    if x == 'R': return 0
    if x == 'G': return 1
    if x == 'B': return 2
    

def solve(S,N):
    presum = [[0 for _ in range(3)] for _ in range(N + 1)]
    
    for i in range(N):
        for j in range(3): 
            presum[i + 1][j] += presum[i][j]
        presum[i + 1][S[i]] += 1

    #Exclusion 

    
    sumall = 0
    for i in range(N - 2):
        for k in range(i + 2,N):
            if S[i] != S[k]:
                c = 3 - S[i] - S[k] 
                sumall += presum[k][c] - presum[i + 1][c]
                
                if (i + k) % 2 == 0:
                    j = (i + k)//2
                    if S[j] == c: sumall -= 1
    return sumall


if __name__ == "__main__": 
    #Input 
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    S = [convert(x) for x in S]
    
    print(solve(S,N))
    





Fixed submission:
from sys import stdin,stdout


def solve(S,N):
    color = [[] for _ in range(3)]

    for i in range(N):
        if S[i] == 'R': color[0].append(i)
        elif S[i] == 'G': color[1].append(i)
        else: color[2].append(i)
    
    
    #Exclusion 
    total = len(color[0])*len(color[1])*len(color[2])

    remain = 0
    for i in range(N - 2):
        for j in range(i + 1,N - 1):
            k = j + j - i
            if k >= N: break
            
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                remain += 1
    return total - remain


    


if __name__ == "__main__": 
    #Input 
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    stdout.write(str(solve(S,N)))