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

R = []
G = []
B = []
result = 0

for i in range(N):
    if S[i] == 'R':
        R.append(1)
        G.append(0)
        B.append(0)
    elif S[i] == 'G':
        R.append(0)
        G.append(1)
        B.append(0)
    if S[i] == 'B':
        R.append(0)
        G.append(0)
        B.append(1)
for i in range(N):
    R.append(0)
    G.append(0)
    B.append(0)
R_total = sum(R)
G_total = sum(G)
B_total = sum(B)
R_count = []
G_count = []
B_count = []
for i in range(N):
    if S[i] == 'R':
        R_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    elif S[i] == 'G':
        G_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    if S[i] == 'B':
        B_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == 'R':
            if S[j] == 'G':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'B':
            if S[j] == 'R':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'G':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
print(result)

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

R = []
G = []
B = []
result = 0

for i in range(N):
    if S[i] == 'R':
        R.append(1)
        G.append(0)
        B.append(0)
    elif S[i] == 'G':
        R.append(0)
        G.append(1)
        B.append(0)
    if S[i] == 'B':
        R.append(0)
        G.append(0)
        B.append(1)
for i in range(N):
    R.append(0)
    G.append(0)
    B.append(0)
R_total = sum(R)
G_total = sum(G)
B_total = sum(B)
R_count = []
G_count = []
B_count = []
for i in range(N):
    if S[i] == 'R':
        R_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    elif S[i] == 'G':
        G_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    if S[i] == 'B':
        B_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == 'R':
            if S[j] == 'G':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'B':
            if S[j] == 'R':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'G':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
print(result)

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

R = []
G = []
B = []
result = 0

for i in range(N):
    if S[i] == 'R':
        R.append(1)
        G.append(0)
        B.append(0)
    elif S[i] == 'G':
        R.append(0)
        G.append(1)
        B.append(0)
    if S[i] == 'B':
        R.append(0)
        G.append(0)
        B.append(1)
for i in range(N):
    R.append(0)
    G.append(0)
    B.append(0)

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == 'R':
            if S[j] == 'G':
                tmp_list = B[j + 1:]
                result += sum(tmp_list)
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                tmp_list = G[j + 1:]
                result += sum(tmp_list)
                if G[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                tmp_list = B[j + 1:]
                result += sum(tmp_list)
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                tmp_list = R[j + 1:]
                result += sum(tmp_list)
                if R[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'B':
            if S[j] == 'R':
                tmp_list = G[j + 1:]
                result += sum(tmp_list)
                if G[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'G':
                tmp_list = R[j + 1:]
                result += sum(tmp_list)
                if R[j * 2 - i] == 1:
                    result -= 1
print(result)

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

R = []
G = []
B = []
result = 0

for i in range(N):
    if S[i] == 'R':
        R.append(1)
        G.append(0)
        B.append(0)
    elif S[i] == 'G':
        R.append(0)
        G.append(1)
        B.append(0)
    if S[i] == 'B':
        R.append(0)
        G.append(0)
        B.append(1)
for i in range(N):
    R.append(0)
    G.append(0)
    B.append(0)
R_total = sum(R)
G_total = sum(G)
B_total = sum(B)
R_count = []
G_count = []
B_count = []
for i in range(N):
    if S[i] == 'R':
        R_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    elif S[i] == 'G':
        G_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    if S[i] == 'B':
        B_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == 'R':
            if S[j] == 'G':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'B':
            if S[j] == 'R':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'G':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
print(result)

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

R = []
G = []
B = []
result = 0

for i in range(N):
    if S[i] == 'R':
        R.append(1)
        G.append(0)
        B.append(0)
    elif S[i] == 'G':
        R.append(0)
        G.append(1)
        B.append(0)
    if S[i] == 'B':
        R.append(0)
        G.append(0)
        B.append(1)
for i in range(N):
    R.append(0)
    G.append(0)
    B.append(0)

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == 'R':
            if S[j] == 'G':
                tmp_list = B[j + 1:]
                result += sum(tmp_list)
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                tmp_list = G[j + 1:]
                result += sum(tmp_list)
                if G[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                tmp_list = B[j + 1:]
                result += sum(tmp_list)
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                tmp_list = R[j + 1:]
                result += sum(tmp_list)
                if R[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'B':
            if S[j] == 'R':
                tmp_list = G[j + 1:]
                result += sum(tmp_list)
                if G[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'G':
                tmp_list = R[j + 1:]
                result += sum(tmp_list)
                if R[j * 2 - i] == 1:
                    result -= 1
print(result)

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

R = []
G = []
B = []
result = 0

for i in range(N):
    if S[i] == 'R':
        R.append(1)
        G.append(0)
        B.append(0)
    elif S[i] == 'G':
        R.append(0)
        G.append(1)
        B.append(0)
    if S[i] == 'B':
        R.append(0)
        G.append(0)
        B.append(1)
for i in range(N):
    R.append(0)
    G.append(0)
    B.append(0)
R_total = sum(R)
G_total = sum(G)
B_total = sum(B)
R_count = []
G_count = []
B_count = []
for i in range(N):
    if S[i] == 'R':
        R_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    elif S[i] == 'G':
        G_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)
    if S[i] == 'B':
        B_total -= 1
        R_count.append(R_total)
        G_count.append(G_total)
        B_count.append(B_total)

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == 'R':
            if S[j] == 'G':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                result += B_count[j]
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'B':
            if S[j] == 'R':
                result += G_count[j]
                if G[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'G':
                result += R_count[j]
                if R[j * 2 - i] == 1:
                    result -= 1
print(result)