Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
n = int(input())
#n, k = map(int, sys.stdin.readline().split())
s = input()
#a = list(map(int, sys.stdin.readline().split()))

def main():
    r = []
    g = []
    b = []

    for i in range(n):
        if s[i] == "R":
            r.append(i)
        elif s[i] == "G":
            g.append(i)
        else:
            b.append(i)

    R = len(r)
    G = len(g)
    B = len(b)

    if min(R, G, B) == R:
        if max(R, G, B) == G:
            w = r
            x = b
            y = g
        else:
            w = r
            x = g
            y = b
    elif min(R, G, B) == G:
        if max(R, G, B) == R:
            w = g
            x = b
            y = r
        else:
            w = g
            x = r
            y = b
    else:
        if max(R, G, B) == R:
            w = b
            x = g
            y = r
        else:
            w = b
            x = r
            y = g


    ans = 0
    z = len(y)
    for i in range(len(w)):
        for j in range(len(x)):
            d = abs(w[i] - x[j])
            e = 0
            p = min(w[i], x[j]) - d
            q = max(w[i], x[j]) + d
            if (w[i] + x[j]) % 2 == 0:
                t = int((w[i] + x[j]) / 2)
            else:
                t = 10000
            if p in y:
                e += 1
            if q in y:
                e += 1
            if t in y:
                e += 1

            ans += (z - e)

    print(ans)
            
main()
    


Fixed submission:
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
n = int(input())
#n, k = map(int, sys.stdin.readline().split())
s = input()
#a = list(map(int, sys.stdin.readline().split()))

def main():
    r = []
    g = []
    b = []

    for i in range(n):
        if s[i] == "R":
            r.append(i)
        elif s[i] == "G":
            g.append(i)
        else:
            b.append(i)


    ans = 0
    z = len(b)
    for i in range(len(r)):
        for j in range(len(g)):
            d = abs(r[i] - g[j])
            e = 0
            p = min(r[i], g[j]) - d
            q = max(r[i], g[j]) + d
            if (r[i] + g[j]) % 2 == 0:
                t = int((r[i] + g[j]) / 2)
                if p >= 0:
                    if s[p] == "B":
                        e += 1
                if q < n:
                    if s[q] == "B":
                        e += 1
                if s[t] == "B":
                    e += 1
            else:
                if p >= 0:
                    if s[p] == "B":
                        e += 1
                if q < n:
                    if s[q] == "B":
                        e += 1

            ans += (z - e)

    print(ans)
            
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
n = int(input())
#n, k = map(int, sys.stdin.readline().split())
s = input()
#a = list(map(int, sys.stdin.readline().split()))


r = []
g = []
b = []

for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)

R = len(r)
G = len(g)
B = len(b)

if min(R, G, B) == R:
    if max(R, G, B) == G:
        w = r
        x = b
        y = g
    else:
        w = r
        x = g
        y = b
elif min(R, G, B) == G:
    if max(R, G, B) == R:
        w = g
        x = b
        y = r
    else:
        w = g
        x = r
        y = b
else:
    if max(R, G, B) == R:
        w = b
        x = g
        y = r
    else:
        w = b
        x = r
        y = g


ans = 0
z = len(y)
for i in range(len(w)):
    for j in range(len(x)):
        d = abs(w[i] - x[j])
        e = 0
        p = min(w[i], x[j]) - d
        q = max(w[i], x[j]) + d
        if (w[i] + x[j]) % 2 == 0:
            t = int((w[i] + x[j]) / 2)
        else:
            t = 10000
        if p in y:
            e += 1
        if q in y:
            e += 1
        if t in y:
            e += 1

        ans += (z - e)

print(ans)
            

    


Fixed submission:
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
n = int(input())
#n, k = map(int, sys.stdin.readline().split())
s = input()
#a = list(map(int, sys.stdin.readline().split()))

def main():
    r = []
    g = []
    b = []

    for i in range(n):
        if s[i] == "R":
            r.append(i)
        elif s[i] == "G":
            g.append(i)
        else:
            b.append(i)


    ans = 0
    z = len(b)
    for i in range(len(r)):
        for j in range(len(g)):
            d = abs(r[i] - g[j])
            e = 0
            p = min(r[i], g[j]) - d
            q = max(r[i], g[j]) + d
            if (r[i] + g[j]) % 2 == 0:
                t = int((r[i] + g[j]) / 2)
                if p >= 0:
                    if s[p] == "B":
                        e += 1
                if q < n:
                    if s[q] == "B":
                        e += 1
                if s[t] == "B":
                    e += 1
            else:
                if p >= 0:
                    if s[p] == "B":
                        e += 1
                if q < n:
                    if s[q] == "B":
                        e += 1

            ans += (z - e)

    print(ans)
            
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
n = int(input())
#n, k = map(int, sys.stdin.readline().split())
s = input()
#a = list(map(int, sys.stdin.readline().split()))


r = []
g = []
b = []

for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)

ans = 0
for i in range(len(r)):
    for j in range(len(g)):
        d = abs(r[i] - g[j])
        e = 0
        p = min(r[i], g[j]) - d
        q = max(r[i], g[j]) + d
        if (r[i] + g[j]) % 2 == 0:
            t = int((r[i] + g[j]) / 2)
        else:
            t = 10000
        if p in b:
            e += 1
        if q in b:
            e += 1
        if t in b:
            e += 1

        ans += (len(b) - e)

print(ans)
            

    


Fixed submission:
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
n = int(input())
#n, k = map(int, sys.stdin.readline().split())
s = input()
#a = list(map(int, sys.stdin.readline().split()))

def main():
    r = []
    g = []
    b = []

    for i in range(n):
        if s[i] == "R":
            r.append(i)
        elif s[i] == "G":
            g.append(i)
        else:
            b.append(i)


    ans = 0
    z = len(b)
    for i in range(len(r)):
        for j in range(len(g)):
            d = abs(r[i] - g[j])
            e = 0
            p = min(r[i], g[j]) - d
            q = max(r[i], g[j]) + d
            if (r[i] + g[j]) % 2 == 0:
                t = int((r[i] + g[j]) / 2)
                if p >= 0:
                    if s[p] == "B":
                        e += 1
                if q < n:
                    if s[q] == "B":
                        e += 1
                if s[t] == "B":
                    e += 1
            else:
                if p >= 0:
                    if s[p] == "B":
                        e += 1
                if q < n:
                    if s[q] == "B":
                        e += 1

            ans += (z - e)

    print(ans)
            
main()