Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
total = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == 'R':
            if S[j] == 'R':
                continue
            elif S[j] == 'G':
                if 2*j-i < N:
                    total += S[j+1:2*j-i].count('B')
                    total += S[2*j-i+1:N].count('B')
                else:
                    total += S[j+1:N].count('B')
            elif S[j] == 'B':
                if 2*j-i < N:
                    total += S[j+1:2*j-i].count('G')
                    total += S[2*j-i+1:N].count('G')
                else:
                    total += S[j+1:N].count('G')
        elif S[i] == 'G':
            if S[j] == 'G':
                continue
            elif S[j] == 'B':
                if 2*j-i < N:
                    total += S[j+1:2*j-i].count('R')
                    total += S[2*j-i+1:N].count('R')
                else:
                    total += S[j+1:N].count('R')
            elif S[j] == 'R':
                if 2*j-i < N:
                    total += S[j+1:2*j-i].count('B')
                    total += S[2*j-i+1:N].count('B')
                else:
                    total += S[j+1:N].count('B')
        elif S[i] == 'B':
            if S[j] == 'B':
                continue
            elif S[j] == 'G':
                if 2*j-i < N:
                    total += S[j+1:2*j-i].count('R')
                    total += S[2*j-i+1:N].count('R')
                else:
                    total += S[j+1:N].count('R')
            elif S[j] == 'R':
                if 2*j-i < N:
                    total += S[j+1:2*j-i].count('G')
                    total += S[2*j-i+1:N].count('G')
                else:
                    total += S[j+1:N].count('G')

print(total)


Fixed submission:
N = int(input())
S = input()
total = 0

for i in range(N-2):
    if S[i] == "R":
        num_G = S[i+1:].count('G')
        num_B = S[i+1:].count('B')
        total += num_G * num_B
    elif S[i] == "G":
        num_B = S[i+1:].count('B')
        num_R = S[i+1:].count('R')
        total += num_B * num_R
    elif S[i] == "B":
        num_G = S[i+1:].count('G')
        num_R = S[i+1:].count('R')
        total += num_G * num_R

for i in range(N-2):
    for j in range(i+1, N-1):
        if 2*j-i >= N:
            continue
        if S[j] != S[i] and S[2*j-i] != S[j] and S[2*j-i] != S[i]:
            total -= 1

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
total = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == 'R':
            if S[j] == 'R':
                continue
            elif S[j] == 'G':
                total += S[j+1:N].count('B')
                if 2*j-i < N:
                    if S[2*j-i] == 'B':
                        total -= 1
            elif S[j] == 'B':
                total += S[j+1:N].count('G')
                if 2*j-i < N:
                    if S[2*j-i] == 'G':
                        total -= 1
        elif S[i] == 'G':
            if S[j] == 'G':
                continue
            elif S[j] == 'R':
                total += S[j+1:N].count('B')
                if 2*j-i < N:
                    if S[2*j-i] == 'B':
                        total -= 1
            elif S[j] == 'B':
                total += S[j+1:N].count('R')
                if 2*j-i < N:
                    if S[2*j-i] == 'R':
                        total -= 1
        if S[i] == 'B':
            if S[j] == 'B':
                continue
            elif S[j] == 'G':
                total += S[j+1:N].count('R')
                if 2*j-i < N:
                    if S[2*j-i] == 'R':
                        total -= 1
            elif S[j] == 'R':
                total += S[j+1:N].count('G')
                if 2*j-i < N:
                    if S[2*j-i] == 'G':
                        total -= 1

print(total)


Fixed submission:
N = int(input())
S = input()
total = 0

for i in range(N-2):
    if S[i] == "R":
        num_G = S[i+1:].count('G')
        num_B = S[i+1:].count('B')
        total += num_G * num_B
    elif S[i] == "G":
        num_B = S[i+1:].count('B')
        num_R = S[i+1:].count('R')
        total += num_B * num_R
    elif S[i] == "B":
        num_G = S[i+1:].count('G')
        num_R = S[i+1:].count('R')
        total += num_G * num_R

for i in range(N-2):
    for j in range(i+1, N-1):
        if 2*j-i >= N:
            continue
        if S[j] != S[i] and S[2*j-i] != S[j] and S[2*j-i] != S[i]:
            total -= 1

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
total = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == 'R':
            if S[j] == 'R':
                continue
            elif S[j] == 'G':
                total += S[j+1:N].count('B')
                if 2*j-i < N:
                    if S[2*j-i] == 'B':
                        total -= 1
            elif S[j] == 'B':
                total += S[j+1:N].count('G')
                if 2*j-i < N:
                    if S[2*j-i] == 'G':
                        total -= 1
        elif S[i] == 'G':
            if S[j] == 'G':
                continue
            elif S[j] == 'R':
                total += S[j+1:N].count('B')
                if 2*j-i < N:
                    if S[2*j-i] == 'B':
                        total -= 1
            elif S[j] == 'B':
                total += S[j+1:N].count('R')
                if 2*j-i < N:
                    if S[2*j-i] == 'R':
                        total -= 1
        if S[i] == 'B':
            if S[j] == 'B':
                continue
            elif S[j] == 'G':
                total += S[j+1:N].count('R')
                if 2*j-i < N:
                    if S[2*j-i] == 'R':
                        total -= 1
            elif S[j] == 'R':
                total += S[j+1:N].count('G')
                if 2*j-i < N:
                    if S[2*j-i] == 'G':
                        total -= 1

print(total)


Fixed submission:
N = int(input())
S = input()
total = 0

for i in range(N-2):
    if S[i] == "R":
        num_G = S[i+1:].count('G')
        num_B = S[i+1:].count('B')
        total += num_G * num_B
    elif S[i] == "G":
        num_B = S[i+1:].count('B')
        num_R = S[i+1:].count('R')
        total += num_B * num_R
    elif S[i] == "B":
        num_G = S[i+1:].count('G')
        num_R = S[i+1:].count('R')
        total += num_G * num_R

for i in range(N-2):
    for j in range(i+1, N-1):
        if 2*j-i >= N:
            continue
        if S[j] != S[i] and S[2*j-i] != S[j] and S[2*j-i] != S[i]:
            total -= 1

print(total)