Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
S=list(input())
 
def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'
 
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            ans += S[j+1:].count(thi)
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[0]*N+[0]
g=[0]*N+[0]
b=[0]*N+[0]

for i in reversed(range(N)):
    if S[i]=='R':
        r[i]=r[i+1]+1
        g[i]=g[i+1]
        b[i]=b[i+1]
    if S[i]=='G':
        r[i]=r[i+1]
        g[i]=g[i+1]+1
        b[i]=b[i+1]
    if S[i]=='B':
        r[i]=r[i+1]
        g[i]=g[i+1]
        b[i]=b[i+1]+1 
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[0]*N+[0]
g=[0]*N+[0]
b=[0]*N+[0]

for i in reversed(range(N)):
    if S[i]=='R':
        r[i]=r[i+1]+1
        g[i]=g[i+1]
        b[i]=b[i+1]
    if S[i]=='G':
        r[i]=r[i+1]
        g[i]=g[i+1]+1
        b[i]=b[i+1]
    if S[i]=='B':
        r[i]=r[i+1]
        g[i]=g[i+1]
        b[i]=b[i+1]+1 
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[0]*N+[0]
g=[0]*N+[0]
b=[0]*N+[0]

for i in reversed(range(N)):
    if S[i]=='R':
        r[i]=r[i+1]+1
        g[i]=g[i+1]
        b[i]=b[i+1]
    if S[i]=='G':
        r[i]=r[i+1]
        g[i]=g[i+1]+1
        b[i]=b[i+1]
    if S[i]=='B':
        r[i]=r[i+1]
        g[i]=g[i+1]
        b[i]=b[i+1]+1 
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[]
g=[]
b=[]
for i in range(N):
    r.append(S[i:].count('R'))
    g.append(S[i:].count('G'))
    b.append(S[i:].count('B'))
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[0]*N+[0]
g=[0]*N+[0]
b=[0]*N+[0]

for i in reversed(range(N)):
    if S[i]=='R':
        r[i]=r[i+1]+1
        g[i]=g[i+1]
        b[i]=b[i+1]
    if S[i]=='G':
        r[i]=r[i+1]
        g[i]=g[i+1]+1
        b[i]=b[i+1]
    if S[i]=='B':
        r[i]=r[i+1]
        g[i]=g[i+1]
        b[i]=b[i+1]+1 
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            ans += S[j+1:].count(thi)
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[0]*N+[0]
g=[0]*N+[0]
b=[0]*N+[0]

for i in reversed(range(N)):
    if S[i]=='R':
        r[i]=r[i+1]+1
        g[i]=g[i+1]
        b[i]=b[i+1]
    if S[i]=='G':
        r[i]=r[i+1]
        g[i]=g[i+1]+1
        b[i]=b[i+1]
    if S[i]=='B':
        r[i]=r[i+1]
        g[i]=g[i+1]
        b[i]=b[i+1]+1 
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

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

ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            for k in range(j+1,N):
                if S[k] == now or S[k] == sec:
                    continue
                else:
                    if j-i != k-j:
                        ans+=1
print(ans)

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

def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'

r=[0]*N+[0]
g=[0]*N+[0]
b=[0]*N+[0]

for i in reversed(range(N)):
    if S[i]=='R':
        r[i]=r[i+1]+1
        g[i]=g[i+1]
        b[i]=b[i+1]
    if S[i]=='G':
        r[i]=r[i+1]
        g[i]=g[i+1]+1
        b[i]=b[i+1]
    if S[i]=='B':
        r[i]=r[i+1]
        g[i]=g[i+1]
        b[i]=b[i+1]+1 
    
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            if thi=='R':
                ans += r[j+1]
            if thi=='G':
                ans += g[j+1]
            if thi=='B':
                ans += b[j+1]
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)