Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n = int(input())
s = input()
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)
B = tuple(B)
if len(R)==0 or len(G)==0 or len(B)==0:
    print(0)
    exit()
cnt = 0
for r in R:
    for g in G:
        a,b = min(r,g), max(r,g)
        if 2*a-b in B:
            cnt += 1
        if 2*b-a in B:
            cnt += 1
        if (b-a)%2==0 and a+(b-a)//2 in B:
            cnt += 1
print(len(R)*len(G)*len(B)-cnt)

Fixed submission:
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
if ans==0:
    print(ans)
else:
    for i in range(n):
        for j in range(i+1, n):
            k=2*j-i
            if k<n:
                if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                    ans -= 1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n = int(input())
s = input()
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)
if len(R)==0 or len(G)==0 or len(B)==0:
    print(0)
    exit()
cnt = 0
for r in R:
    for g in G:
        a,b = min(r,g), max(r,g)
        if 2*a-b in B:
            cnt += 1
        if 2*b-a in B:
            cnt += 1
        if (b-a)%2==0 and a+(b-a)//2 in B:
            cnt += 1
print(len(R)*len(G)*len(B)-cnt)

Fixed submission:
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
if ans==0:
    print(ans)
else:
    for i in range(n):
        for j in range(i+1, n):
            k=2*j-i
            if k<n:
                if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                    ans -= 1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n = int(input())
s = input()
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)
cnt = 0
for r in R:
    for g in G:
        a,b = min(r,g), max(r,g)
        if 2*a-b in B:
            cnt += 1
        if 2*b-a in B:
            cnt += 1
        if (b-a)%2==0 and a+(b-a)//2 in B:
            cnt += 1
print(len(R)*len(G)*len(B)-cnt)

Fixed submission:
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
if ans==0:
    print(ans)
else:
    for i in range(n):
        for j in range(i+1, n):
            k=2*j-i
            if k<n:
                if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                    ans -= 1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
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 = len(R)*len(G)*len(B)
cnt = 0
for r in R:
    for g in G:
        if 2*r - g in B:
            cnt += 1
        if 2*g - r in B:
            cnt += 1
        if g>r:
            if (g-r)%2==0 and r+(g-r)//2 in B:  
                cnt += 1
        if r>g:
            if (r-g)%2==0 and g+(r-g)//2 in B:
                cnt += 1
print(ans - cnt)

Fixed submission:
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
if ans==0:
    print(ans)
else:
    for i in range(n):
        for j in range(i+1, n):
            k=2*j-i
            if k<n:
                if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                    ans -= 1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
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 = len(R)*len(G)*len(B)
cnt = 0
for r in R:
    for g in G:
        if 2*r - g in B:
            cnt += 1
        if 2*g - r in B:
            cnt += 1
        if g>r:
            if (g-r)%2==0 and r+(g-r)//2 in B:  
                cnt += 1
        if r>g:
            if (r-g)%2==0 and g+(r-g)//2 in B:
                cnt += 1
print(ans - cnt)

Fixed submission:
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
if ans==0:
    print(ans)
else:
    for i in range(n):
        for j in range(i+1, n):
            k=2*j-i
            if k<n:
                if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                    ans -= 1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n = int(input())
s = input()
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(n):
    for j in range(i+1, n):
        ci, cj = s[i], s[j]
        if ci==cj:
            continue
        ng = 2*j-i
        if (ci=='R' and cj=='B') or (ci=='B' and cj=='R'):
            ans += len(G)-bisect.bisect_left(G, j)
            if ng in G:
                ans -= 1
        elif (ci=='R' and cj=='G') or (ci=='G' and cj=='R'):
            ans += len(B)-bisect.bisect_left(B, j)
            if ng in B:
                ans -= 1
        else:
            ans += len(R)-bisect.bisect_left(R, j)
            if ng in R:
                ans -= 1
print(ans)

Fixed submission:
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
if ans==0:
    print(ans)
else:
    for i in range(n):
        for j in range(i+1, n):
            k=2*j-i
            if k<n:
                if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                    ans -= 1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n = int(input())
s = input()
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(n):
    for j in range(i+1, n):
        ci, cj = s[i], s[j]
        if ci==cj:
            continue
        ng = 2*j-i

        if (ci=='R' and cj=='B') or (ci=='B' and cj=='R'):
            ans += len(G)-bisect.bisect_left(G, j)
            if ng in G:
                ans -= 1
        elif (ci=='R' and cj=='G') or (ci=='G' and cj=='R'):
            ans += len(B)-bisect.bisect_left(B, j)
            if ng in B:
                ans -= 1
        else:
            ans += len(R)-bisect.bisect_left(R, j)
            if ng in R:
                ans -= 1
print(ans)

Fixed submission:
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
if ans==0:
    print(ans)
else:
    for i in range(n):
        for j in range(i+1, n):
            k=2*j-i
            if k<n:
                if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                    ans -= 1
    print(ans)