Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N = int(input())
S = np.array(list(input()))

R = (S=='R').sum()
G = (S=='G').sum()
B = N - R - G

ans = R * G * B

for i in range(0, N-2):
  for j in range(i+1,N-1):
    k = 2 * j - i
    if k >= N:
      break
    else:
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        ans -= 1

print(ans)

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

R = S.count('R')
G = S.count('G')
B = N - R - G

ans = R * G * B

for i in range(0, N-2):
  for j in range(i+1,N-1):
    k = 2 * j - i
    if k >= N:
      break
    else:
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        ans -= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N = int(input())
S = input()

after_cumsum_RGB = {}
R, G, B = np.zeros(N+1), np.zeros(N+1), np.zeros(N+1)

def rgb_hash(a):
  if S[i] == 'R':
    return 0
  elif S[i] == 'G':
    return 1
  elif S[i] == 'B':
    return 2
  
def ci_hash(a):
  if 'R' not in a:
    return 0
  elif 'G' not in a:
    return 1
  elif 'B' not in a:
    return 2

def c_hash(a):
  if 'R' not in a:
    return 'R'
  elif 'G' not in a:
    return 'G'
  elif 'B' not in a:
    return 'B'
  
for i in range(N):
  if S[i] == 'R':
    R[i] = 1
  if S[i] == 'G':
    G[i] = 1
  if S[i] == 'B':
    B[i] = 1
after_cumsum_RGB = {'R': R[::-1].cumsum()[::-1], 'G': G[::-1].cumsum()[::-1], 'B': B[::-1].cumsum()[::-1]}

ans = 0
for i in range(0, N-2):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      c = c_hash([S[i], S[j]])
      ans += after_cumsum_RGB[c][j+1]
      if 2*j-i <= N-1:
        if S[2*j-i] == c:
          ans -= 1
print(int(ans))
      
    

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

R = S.count('R')
G = S.count('G')
B = N - R - G

ans = R * G * B

for i in range(0, N-2):
  for j in range(i+1,N-1):
    k = 2 * j - i
    if k >= N:
      break
    else:
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        ans -= 1

print(ans)

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

after_cumsum_RGB = [[0]*(N+1), [0]*(N+1), [0]*(N+1)]

def rgb_hash(a):
  if S[i] == 'R':
    return 0
  elif S[i] == 'G':
    return 1
  elif S[i] == 'B':
    return 2
  
def ci_hash(a):
  if 'R' not in a:
    return 0
  elif 'G' not in a:
    return 1
  elif 'B' not in a:
    return 2

def c_hash(a):
  if 'R' not in a:
    return 'R'
  elif 'G' not in a:
    return 'G'
  elif 'B' not in a:
    return 'B'
  
for i in range(N-1, -1, -1):
  if S[i] == 'R':
    after_cumsum_RGB[0][i] = 1
  if S[i] == 'G':
    after_cumsum_RGB[1][i] = 1
  if S[i] == 'B':
    after_cumsum_RGB[2][i] = 1
  for j in range(3):
    after_cumsum_RGB[j][i] += after_cumsum_RGB[j][i+1]

ans = 0
for i in range(0, N-2):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      target = ci_hash([S[i], S[j]])
      ans += after_cumsum_RGB[target][j+1]
      if 2*j-i <= N-1:
        if S[2*j-i] == c_hash([S[i], S[j]]):
          ans -= 1
print(ans)
      
    

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

R = S.count('R')
G = S.count('G')
B = N - R - G

ans = R * G * B

for i in range(0, N-2):
  for j in range(i+1,N-1):
    k = 2 * j - i
    if k >= N:
      break
    else:
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        ans -= 1

print(ans)