Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
R, G, B = [0]*N, [0]*N, [0]*N
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    R[i] += R[i-1] + (1 if S[i] == "R" else 0)
    G[i] += G[i-1] + (1 if S[i] == "G" else 0)
    B[i] += B[i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i] - (1 if S[j] == "G" else 0)
                else:
                    ans += R[k] - R[i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i]
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i]
                else:
                    ans += R[k] - R[i]
print(ans)


Fixed submission:
N = int(input())
S = list(input())
R, G, B = [0]*N, [0]*N, [0]*N
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    R[i] += R[i-1] + (1 if S[i] == "R" else 0)
    G[i] += G[i-1] + (1 if S[i] == "G" else 0)
    B[i] += B[i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i] - (1 if S[j] == "G" else 0)
                else:
                    ans += R[k] - R[i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i]
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i]
                else:
                    ans += R[k] - R[i]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
R, G, B = [0]*N, [0]*N, [0]*N
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    R[i] += R[i-1] + (1 if S[i] == "R" else 0)
    G[i] += G[i-1] + (1 if S[i] == "G" else 0)
    B[i] += B[i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i] - (1 if S[j] == "G" else 0)
                else:
                    ans += R[k] - R[i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i]
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i]
                else:
                    ans += R[k] - R[i]
print(ans)


Fixed submission:
N = int(input())
S = list(input())
R, G, B = [0]*N, [0]*N, [0]*N
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    R[i] += R[i-1] + (1 if S[i] == "R" else 0)
    G[i] += G[i-1] + (1 if S[i] == "G" else 0)
    B[i] += B[i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i] - (1 if S[j] == "G" else 0)
                else:
                    ans += R[k] - R[i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i]
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i]
                else:
                    ans += R[k] - R[i]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
rgb = {"R": [0]*N, "B": [0]*N, "G": [0]*N}
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    rgb["R"][i] += rgb["R"][i-1] + (1 if S[i] == "R" else 0)
    rgb["G"][i] += rgb["G"][i-1] + (1 if S[i] == "G" else 0)
    rgb["B"][i] += rgb["B"][i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += rgb["B"][k] - rgb["B"][i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += rgb["G"][k] - rgb["G"][i] - (1 if S[j] == "G" else 0)
                else:
                    ans += rgb["R"][k] - rgb["R"][i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += rgb["B"][k] - rgb["B"][i]
                elif (S[i], S[k]) in rb_set:
                    ans += rgb["G"][k] - rgb["G"][i]
                else:
                    ans += rgb["R"][k] - rgb["R"][i]
print(ans)


Fixed submission:
N = int(input())
S = list(input())
R, G, B = [0]*N, [0]*N, [0]*N
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    R[i] += R[i-1] + (1 if S[i] == "R" else 0)
    G[i] += G[i-1] + (1 if S[i] == "G" else 0)
    B[i] += B[i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i] - (1 if S[j] == "G" else 0)
                else:
                    ans += R[k] - R[i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i]
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i]
                else:
                    ans += R[k] - R[i]
print(ans)