Buggy python submission (verdict: Time Limit Exceeded):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    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 r in R:
        for g in G:
            for b in B:
                rg = abs(r-g)
                gb = abs(g-b)
                br = abs(b-r)
                if rg != gb and gb != br and br != rg:
                    ans += 1
                # i = min(r, g, b)
                # k = max(r, g, b)
                # a = k-i
                # if a != 2*r and a != 2*g and a != 2*b:
                #     ans += 1
    # for n in R:
    #     # x = bisect_left(G, n)
    #     for i in G:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(B, a)
    #         ans += len(B[x:])
    #         if 2*a-b in B:
    #             ans -= 1
    # for n in R:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(G, a)
    #         ans += len(G[x:])
    #         if 2*a-b in G:
    #             ans -= 1
    # # for n in G:
    # #     x = bisect_left(R, n)
    # #     for i in R[x:]:
    # #         x = bisect_left(B, i)
    # #         ans += len(B[x:])
    # #         if 2*i-n in B:
    # #             ans -= 1
    # for n in G:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(R, a)
    #         ans += len(R[x:])
    #         if 2*a-b in R:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Fixed submission:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    R = []
    G = []
    B = []
    for i in range(N):
        if S[i] == 'R':
            R.append(i+1)
        elif S[i] == 'G':
            G.append(i+1)
        else:
            B.append(i+1)
    ans = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in B:
                    ans -= 1
    for r in B:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in R:
                    ans -= 1
    for r in R:
        for g in B:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in G:
                    ans -= 1
            # for b in B:
            #     rg = abs(r-g)
            #     gb = abs(g-b)
            #     br = abs(b-r)
            #     if rg != gb and gb != br and br != rg:
            #         ans += 1
                # i = min(r, g, b)
                # k = max(r, g, b)
                # a = k-i
                # if a != 2*r and a != 2*g and a != 2*b:
                #     ans += 1
    # for n in R:
    #     # x = bisect_left(G, n)
    #     for i in G:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(B, a)
    #         ans += len(B[x:])
    #         if 2*a-b in B:
    #             ans -= 1
    # for n in R:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(G, a)
    #         ans += len(G[x:])
    #         if 2*a-b in G:
    #             ans -= 1
    # # for n in G:
    # #     x = bisect_left(R, n)
    # #     for i in R[x:]:
    # #         x = bisect_left(B, i)
    # #         ans += len(B[x:])
    # #         if 2*i-n in B:
    # #             ans -= 1
    # for n in G:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(R, a)
    #         ans += len(R[x:])
    #         if 2*a-b in R:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    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 n in R:
        # x = bisect_left(G, n)
        for i in G:
            a = max(n, i)
            b = min(n, i)
            x = bisect_left(B, a)
            ans += len(B[x:])
            if 2*a-b in B:
                ans -= 1
    for n in R:
        # x = bisect_left(B, n)
        for i in B:
            a = max(n, i)
            b = min(n, i)
            x = bisect_left(G, a)
            ans += len(G[x:])
            if 2*a-b in G:
                ans -= 1
    # for n in G:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(B, i)
    #         ans += len(B[x:])
    #         if 2*i-n in B:
    #             ans -= 1
    for n in G:
        # x = bisect_left(B, n)
        for i in B:
            a = max(n, i)
            b = min(n, i)
            x = bisect_left(R, a)
            ans += len(R[x:])
            if 2*a-b in R:
                ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Fixed submission:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    R = []
    G = []
    B = []
    for i in range(N):
        if S[i] == 'R':
            R.append(i+1)
        elif S[i] == 'G':
            G.append(i+1)
        else:
            B.append(i+1)
    ans = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in B:
                    ans -= 1
    for r in B:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in R:
                    ans -= 1
    for r in R:
        for g in B:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in G:
                    ans -= 1
            # for b in B:
            #     rg = abs(r-g)
            #     gb = abs(g-b)
            #     br = abs(b-r)
            #     if rg != gb and gb != br and br != rg:
            #         ans += 1
                # i = min(r, g, b)
                # k = max(r, g, b)
                # a = k-i
                # if a != 2*r and a != 2*g and a != 2*b:
                #     ans += 1
    # for n in R:
    #     # x = bisect_left(G, n)
    #     for i in G:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(B, a)
    #         ans += len(B[x:])
    #         if 2*a-b in B:
    #             ans -= 1
    # for n in R:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(G, a)
    #         ans += len(G[x:])
    #         if 2*a-b in G:
    #             ans -= 1
    # # for n in G:
    # #     x = bisect_left(R, n)
    # #     for i in R[x:]:
    # #         x = bisect_left(B, i)
    # #         ans += len(B[x:])
    # #         if 2*i-n in B:
    # #             ans -= 1
    # for n in G:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(R, a)
    #         ans += len(R[x:])
    #         if 2*a-b in R:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    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 n in R:
        for i in G:
            if i < n:
                continue
            x = bisect_left(B, i)
            ans += len(B[x:])
            if 2*i-n in B:
                ans -= 1
    for n in R:
        x = bisect_left(B, n)
        for i in B:
            if i < n:
                continue
            x = bisect_left(G, i)
            ans += len(G[x:])
            if 2*i-n in G:
                ans -= 1
    for n in G:
        x = bisect_left(R, n)
        for i in R:
            if i < n:
                continue
            x = bisect_left(B, i)
            ans += len(B[x:])
            if 2*i-n in B:
                ans -= 1
    for n in G:
        x = bisect_left(B, n)
        for i in B:
            if i < n:
                continue
            x = bisect_left(R, i)
            ans += len(R[x:])
            if 2*i-n in R:
                ans -= 1
    for n in B:
        x = bisect_left(R, n)
        for i in R:
            if i < n:
                continue
            x = bisect_left(G, i)
            ans += len(G[x:])
            if 2*i-n in G:
                ans -= 1
    for n in B:
        x = bisect_left(G, n)
        for i in G:
            if i < n:
                continue
            x = bisect_left(R, i)
            ans += len(R[x:])
            if 2*i-n in R:
                ans -= 1
    print(ans)
resolve()

Fixed submission:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    R = []
    G = []
    B = []
    for i in range(N):
        if S[i] == 'R':
            R.append(i+1)
        elif S[i] == 'G':
            G.append(i+1)
        else:
            B.append(i+1)
    ans = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in B:
                    ans -= 1
    for r in B:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in R:
                    ans -= 1
    for r in R:
        for g in B:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in G:
                    ans -= 1
            # for b in B:
            #     rg = abs(r-g)
            #     gb = abs(g-b)
            #     br = abs(b-r)
            #     if rg != gb and gb != br and br != rg:
            #         ans += 1
                # i = min(r, g, b)
                # k = max(r, g, b)
                # a = k-i
                # if a != 2*r and a != 2*g and a != 2*b:
                #     ans += 1
    # for n in R:
    #     # x = bisect_left(G, n)
    #     for i in G:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(B, a)
    #         ans += len(B[x:])
    #         if 2*a-b in B:
    #             ans -= 1
    # for n in R:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(G, a)
    #         ans += len(G[x:])
    #         if 2*a-b in G:
    #             ans -= 1
    # # for n in G:
    # #     x = bisect_left(R, n)
    # #     for i in R[x:]:
    # #         x = bisect_left(B, i)
    # #         ans += len(B[x:])
    # #         if 2*i-n in B:
    # #             ans -= 1
    # for n in G:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(R, a)
    #         ans += len(R[x:])
    #         if 2*a-b in R:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
from bisect import bisect_left
def resolve():
    N = ir()
    nS = sr()
    q = deque([])
    S = []
    R = []
    G = []
    B = []
    for i in range(N):
        if nS[i] == 'R':
            S.append(2)
            R.append(i)
        elif nS[i] == 'G':
            S.append(3)
            G.append(i)
        else:
            S.append(5)
            B.append(i)
        q.append([S[i], i])
    ans = 0
    for n in R:
        x = bisect_left(G, n)
        for i in G[x:]:
            x = bisect_left(B, i)
            ans += len(B[x:])
            if 2*i-n in B:
                ans -= 1
    for n in R:
        x = bisect_left(B, n)
        for i in B[x:]:
            x = bisect_left(G, i)
            ans += len(G[x:])
            if 2*i-n in G:
                ans -= 1
    for n in G:
        x = bisect_left(R, n)
        for i in R[x:]:
            x = bisect_left(B, i)
            ans += len(B[x:])
            if 2*i-n in B:
                ans -= 1
    for n in G:
        x = bisect_left(B, n)
        for i in B[x:]:
            x = bisect_left(R, i)
            ans += len(R[x:])
            if 2*i-n in R:
                ans -= 1
    for n in B:
        x = bisect_left(R, n)
        for i in R[x:]:
            x = bisect_left(G, i)
            ans += len(G[x:])
            if 2*i-n in G:
                ans -= 1
    for n in B:
        x = bisect_left(G, n)
        for i in G[x:]:
            x = bisect_left(R, i)
            ans += len(R[x:])
            if 2*i-n in R:
                ans -= 1
    print(ans)
resolve()

Fixed submission:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    R = []
    G = []
    B = []
    for i in range(N):
        if S[i] == 'R':
            R.append(i+1)
        elif S[i] == 'G':
            G.append(i+1)
        else:
            B.append(i+1)
    ans = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in B:
                    ans -= 1
    for r in B:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in R:
                    ans -= 1
    for r in R:
        for g in B:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in G:
                    ans -= 1
            # for b in B:
            #     rg = abs(r-g)
            #     gb = abs(g-b)
            #     br = abs(b-r)
            #     if rg != gb and gb != br and br != rg:
            #         ans += 1
                # i = min(r, g, b)
                # k = max(r, g, b)
                # a = k-i
                # if a != 2*r and a != 2*g and a != 2*b:
                #     ans += 1
    # for n in R:
    #     # x = bisect_left(G, n)
    #     for i in G:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(B, a)
    #         ans += len(B[x:])
    #         if 2*a-b in B:
    #             ans -= 1
    # for n in R:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(G, a)
    #         ans += len(G[x:])
    #         if 2*a-b in G:
    #             ans -= 1
    # # for n in G:
    # #     x = bisect_left(R, n)
    # #     for i in R[x:]:
    # #         x = bisect_left(B, i)
    # #         ans += len(B[x:])
    # #         if 2*i-n in B:
    # #             ans -= 1
    # for n in G:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(R, a)
    #         ans += len(R[x:])
    #         if 2*a-b in R:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
from bisect import bisect_left
def resolve():
    N = ir()
    nS = sr()
    q = deque([])
    S = []
    R = []
    G = []
    B = []
    for i in range(N):
        if nS[i] == 'R':
            S.append(2)
            R.append(i)
        elif nS[i] == 'G':
            S.append(3)
            G.append(i)
        else:
            S.append(5)
            B.append(i)
        q.append([S[i], i])
    ans = 0
    for j in range(N):
        u, n = S[j], j
        for i in range(n+1, N):
            if u != S[i]:
                if u*S[i] == 6:
                    x = bisect_left(B, i)
                    ans += len(B[x:])
                    if 2*i-n in B:
                        ans -= 1
                elif u*S[i] == 10:
                    x = bisect_left(G, i)
                    ans += len(G[x:])
                    if 2*i-n in G:
                        ans -= 1
                else:
                    x = bisect_left(R, i)
                    ans += len(R[x:])
                    if 2*i-n in R:
                        ans -= 1   
    print(ans)
resolve()

Fixed submission:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    R = []
    G = []
    B = []
    for i in range(N):
        if S[i] == 'R':
            R.append(i+1)
        elif S[i] == 'G':
            G.append(i+1)
        else:
            B.append(i+1)
    ans = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in B:
                    ans -= 1
    for r in B:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in R:
                    ans -= 1
    for r in R:
        for g in B:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in G:
                    ans -= 1
            # for b in B:
            #     rg = abs(r-g)
            #     gb = abs(g-b)
            #     br = abs(b-r)
            #     if rg != gb and gb != br and br != rg:
            #         ans += 1
                # i = min(r, g, b)
                # k = max(r, g, b)
                # a = k-i
                # if a != 2*r and a != 2*g and a != 2*b:
                #     ans += 1
    # for n in R:
    #     # x = bisect_left(G, n)
    #     for i in G:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(B, a)
    #         ans += len(B[x:])
    #         if 2*a-b in B:
    #             ans -= 1
    # for n in R:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(G, a)
    #         ans += len(G[x:])
    #         if 2*a-b in G:
    #             ans -= 1
    # # for n in G:
    # #     x = bisect_left(R, n)
    # #     for i in R[x:]:
    # #         x = bisect_left(B, i)
    # #         ans += len(B[x:])
    # #         if 2*i-n in B:
    # #             ans -= 1
    # for n in G:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(R, a)
    #         ans += len(R[x:])
    #         if 2*a-b in R:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
    N = ir()
    nS = sr()
    q = deque([])
    S = []
    for i in range(N):
        if nS[i] == 'R':
            S.append(2)
        elif nS[i] == 'G':
            S.append(3)
        else:
            S.append(5)
        q.append([S[i], S[i], i, -1])
    ans = 0
    while len(q) > 0:
        u, v, n, d = q.popleft()
        if d == -1:
            for i in range(n+1, N):
                if u != S[i]:
                    q.append([u, S[i], i, i-n])
        else:
            for i in range(n+1, N):
                if i == n+d:
                    continue
                if u*v*S[i] == 30:
                    ans += 1
    print(ans)
resolve()

Fixed submission:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    R = []
    G = []
    B = []
    for i in range(N):
        if S[i] == 'R':
            R.append(i+1)
        elif S[i] == 'G':
            G.append(i+1)
        else:
            B.append(i+1)
    ans = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in B:
                    ans -= 1
    for r in B:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in R:
                    ans -= 1
    for r in R:
        for g in B:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in G:
                    ans -= 1
            # for b in B:
            #     rg = abs(r-g)
            #     gb = abs(g-b)
            #     br = abs(b-r)
            #     if rg != gb and gb != br and br != rg:
            #         ans += 1
                # i = min(r, g, b)
                # k = max(r, g, b)
                # a = k-i
                # if a != 2*r and a != 2*g and a != 2*b:
                #     ans += 1
    # for n in R:
    #     # x = bisect_left(G, n)
    #     for i in G:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(B, a)
    #         ans += len(B[x:])
    #         if 2*a-b in B:
    #             ans -= 1
    # for n in R:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(G, a)
    #         ans += len(G[x:])
    #         if 2*a-b in G:
    #             ans -= 1
    # # for n in G:
    # #     x = bisect_left(R, n)
    # #     for i in R[x:]:
    # #         x = bisect_left(B, i)
    # #         ans += len(B[x:])
    # #         if 2*i-n in B:
    # #             ans -= 1
    # for n in G:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(R, a)
    #         ans += len(R[x:])
    #         if 2*a-b in R:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
    N = ir()
    S = [s for s in sr()]
    q = deque([])
    for i in range(N):
        q.append([S[i], S[i], i, -1])
    ans = 0
    while len(q) > 0:
        u, v, n, d = q.popleft()
        if d == -1:
            for i in range(n+1, N):
                if u != S[i]:
                    q.append([u, S[i], i, i-n])
        else:
            for i in range(n+1, N):
                if i == n+d:
                    continue
                if u != S[i] and v != S[i]:
                    ans += 1
    print(ans)
resolve()

Fixed submission:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    R = []
    G = []
    B = []
    for i in range(N):
        if S[i] == 'R':
            R.append(i+1)
        elif S[i] == 'G':
            G.append(i+1)
        else:
            B.append(i+1)
    ans = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in B:
                    ans -= 1
    for r in B:
        for g in G:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in R:
                    ans -= 1
    for r in R:
        for g in B:
            if abs(r+g)%2 == 0:
                if abs(r+g)//2 in G:
                    ans -= 1
            # for b in B:
            #     rg = abs(r-g)
            #     gb = abs(g-b)
            #     br = abs(b-r)
            #     if rg != gb and gb != br and br != rg:
            #         ans += 1
                # i = min(r, g, b)
                # k = max(r, g, b)
                # a = k-i
                # if a != 2*r and a != 2*g and a != 2*b:
                #     ans += 1
    # for n in R:
    #     # x = bisect_left(G, n)
    #     for i in G:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(B, a)
    #         ans += len(B[x:])
    #         if 2*a-b in B:
    #             ans -= 1
    # for n in R:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(G, a)
    #         ans += len(G[x:])
    #         if 2*a-b in G:
    #             ans -= 1
    # # for n in G:
    # #     x = bisect_left(R, n)
    # #     for i in R[x:]:
    # #         x = bisect_left(B, i)
    # #         ans += len(B[x:])
    # #         if 2*i-n in B:
    # #             ans -= 1
    # for n in G:
    #     # x = bisect_left(B, n)
    #     for i in B:
    #         a = max(n, i)
    #         b = min(n, i)
    #         x = bisect_left(R, a)
    #         ans += len(R[x:])
    #         if 2*a-b in R:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()