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

n_total = 0
S_ik, S_jk = [], []
for i in range(0,N-2):
  tmp = []
  for k in range(i+2,N):
    if S[i] != S[k]:
      tmp.append(k)
  S_ik.append(tmp)
for j in range(1,N-1):
  tmp = []
  for k in range(j+1,N):
    if S[j] != S[k]:
      tmp.append(k)
  S_jk.append(tmp)
for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      group1 = list(set(S_ik[i])&set(S_jk[j-1]))
      group2 = list(set([j-i+j])&set(group1))
      if len(group2) == 0:
        n_total += len(group1)
      else:
        n_total += len(group1)-1
print(n_total)

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

n_R, n_G, n_B, n_total = 0, 0, 0, 0
for i in range(N):
  if S[i] == 'R':
    n_R+=1
  elif S[i] == 'G':
    n_G+=1
  else:
    n_B+=1
n_total += (n_R*n_G*n_B)
for j in range(1,N-1):
  i = j-1
  k = j+1
  while 1:
    if 0 <= i and k < N:
      if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
        #print(i,j,k, S[i], S[j], S[k])
        n_total-=1
    else:
      break
    i -= 1
    k += 1
print(n_total)

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

n_total = 0
S_ik, S_jk = [], []
for i in range(0,N-2):
  tmp = []
  for k in range(i+2,N):
    if S[i] != S[k]:
      tmp.append(k)
  S_ik.append(tmp)
for j in range(1,N-1):
  tmp = []
  for k in range(j+1,N):
    if S[j] != S[k]:
      tmp.append(k)
  S_jk.append(tmp)
for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      group = list(set(S_ik[i])&set(S_jk[j-1]))
      for k in range(len(group)):
        if (j-i) != (group[k]-j):
          n_total+=1
print(n_total)

"""
for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      for k in range(j+1,N):
        if S[i] != S[k] and S[j] != S[k] and (j-i) != (k-j): 
          n_total+=1
print(n_total)
"""

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

n_R, n_G, n_B, n_total = 0, 0, 0, 0
for i in range(N):
  if S[i] == 'R':
    n_R+=1
  elif S[i] == 'G':
    n_G+=1
  else:
    n_B+=1
n_total += (n_R*n_G*n_B)
for j in range(1,N-1):
  i = j-1
  k = j+1
  while 1:
    if 0 <= i and k < N:
      if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
        #print(i,j,k, S[i], S[j], S[k])
        n_total-=1
    else:
      break
    i -= 1
    k += 1
print(n_total)

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

n_total = 0

S_jk = []
for j in range(1,N-1):
  tmp = []
  for k in range(j+1,N):
    if S[j] != S[k]:
      tmp.append(k)
  S_jk.append(tmp)
for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      for k in range(len(S_jk[j-1])):
        if S[i] != S[S_jk[j-1][k]] and (j-i) != (S_jk[j-1][k]-j):
          n_total+=1
print(n_total)

"""
for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      for k in range(j+1,N):
        if S[i] != S[k] and S[j] != S[k] and (j-i) != (k-j): 
          n_total+=1
print(n_total)
"""

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

n_R, n_G, n_B, n_total = 0, 0, 0, 0
for i in range(N):
  if S[i] == 'R':
    n_R+=1
  elif S[i] == 'G':
    n_G+=1
  else:
    n_B+=1
n_total += (n_R*n_G*n_B)
for j in range(1,N-1):
  i = j-1
  k = j+1
  while 1:
    if 0 <= i and k < N:
      if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
        #print(i,j,k, S[i], S[j], S[k])
        n_total-=1
    else:
      break
    i -= 1
    k += 1
print(n_total)

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

n_total = 0
for i in range(0,N-2):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      for k in range(j+1,N):
        if S[i] != S[k] and S[j] != S[k] and (j-i) != (k-j): 
          n_total+=1
print(n_total)

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

n_R, n_G, n_B, n_total = 0, 0, 0, 0
for i in range(N):
  if S[i] == 'R':
    n_R+=1
  elif S[i] == 'G':
    n_G+=1
  else:
    n_B+=1
n_total += (n_R*n_G*n_B)
for j in range(1,N-1):
  i = j-1
  k = j+1
  while 1:
    if 0 <= i and k < N:
      if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
        #print(i,j,k, S[i], S[j], S[k])
        n_total-=1
    else:
      break
    i -= 1
    k += 1
print(n_total)