Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = list(input())
L = [[], [], []]
for i in range(N):
  if s[i] == 'R':
    L[0].append(i)
  elif s[i] == 'G':
    L[1].append(i)
  else:
    L[2].append(i)
l = [len(L[i]) for i in range(3)]
ans = l[0] * l[1] * l[2]
bb = []
if l[0] <= l[1] <= l[2]:
    bb = [0, 1, 2]
elif l[0] <= l[2] <= l[1]:
    bb = [0, 2, 1]
elif l[1] <= l[0] <= l[2]:
    bb = [1, 0, 2]
elif l[1] <= l[2] <= l[0]:
    bb = [1, 2, 0]
elif l[2] <= l[0] <= l[1]:
    bb = [2, 0, 1]
elif l[2] <= l[1] <= l[0]:
    bb = [2, 1, 0]
for i in L[bb[0]]:
  for j in L[bb[1]]:
    p = 2 * i - j
    q = 2 * j - i
    r = (i + j) / 2
    for k in L[bb[2]]:
      if k > max(p, q, r):
        break
      if k == p:
        ans = ans - 1
      if k == q:
        ans = ans - 1
      if k == r:
        ans = ans - 1
print(ans)

Fixed submission:
N = int(input())
S = list(input())
R = []
G = []
for i in range(N):
  if S[i] == "R":
    R.append(i)
  elif S[i] == "G":
    G.append(i)
r = len(R)
g = len(G)
ans = r * g * (N - r - g)
for i in R:
  for j in G:
    a = 2 * i - j
    b = 2 * j - i
    c = i + j
    if 0 <= a < N and S[a] == "B":
      ans -= 1
    if 0 <= b < N and S[b] == "B":
      ans -= 1
    if c % 2 == 0 and S[c//2] == "B":
      ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = list(input())
L = [[], [], []]
for i in range(N):
  if s[i] == 'R':
    L[0].append(i)
  elif s[i] == 'G':
    L[1].append(i)
  else:
    L[2].append(i)
l = [len(L[i]) for i in range(3)]
ans = l[0] * l[1] * l[2]
bb = []
if l[0] <= l[1] <= l[2]:
    bb = [0, 1, 2]
elif l[0] <= l[2] <= l[1]:
    bb = [0, 2, 1]
elif l[1] <= l[0] <= l[2]:
    bb = [1, 0, 2]
elif l[1] <= l[2] <= l[0]:
    bb = [1, 2, 0]
elif l[2] <= l[0] <= l[1]:
    bb = [2, 0, 1]
elif l[2] <= l[1] <= l[0]:
    bb = [2, 1, 0]
for i in L[bb[0]]:
  for j in L[bb[1]]:
    if (2 * i - j) in L[bb[2]]:
      ans = ans - 1
    if (2 * j - i) in L[bb[2]]:
      ans = ans - 1
    if (i - j) % 2 == 0 and int((i+j)/2) in L[bb[2]]:
      ans = ans - 1
print(ans)


Fixed submission:
N = int(input())
S = list(input())
R = []
G = []
for i in range(N):
  if S[i] == "R":
    R.append(i)
  elif S[i] == "G":
    G.append(i)
r = len(R)
g = len(G)
ans = r * g * (N - r - g)
for i in R:
  for j in G:
    a = 2 * i - j
    b = 2 * j - i
    c = i + j
    if 0 <= a < N and S[a] == "B":
      ans -= 1
    if 0 <= b < N and S[b] == "B":
      ans -= 1
    if c % 2 == 0 and S[c//2] == "B":
      ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = list(input())
L = [[], [], []]
for i in range(N):
  if s[i] == 'R':
    L[0].append(i)
  elif s[i] == 'G':
    L[1].append(i)
  else:
    L[2].append(i)
ans = len(L[0]) * len(L[1]) * len(L[2])
for i in L[0]:
  for j in L[1]:
    if (2 * i - j) in L[2]:
      ans = ans - 1
    if (2 * j - i) in L[2]:
      ans = ans - 1
    if (i - j) % 2 == 0 and int((i+j)/2) in L[2]:
      ans = ans - 1
print(ans)

Fixed submission:
N = int(input())
S = list(input())
R = []
G = []
for i in range(N):
  if S[i] == "R":
    R.append(i)
  elif S[i] == "G":
    G.append(i)
r = len(R)
g = len(G)
ans = r * g * (N - r - g)
for i in R:
  for j in G:
    a = 2 * i - j
    b = 2 * j - i
    c = i + j
    if 0 <= a < N and S[a] == "B":
      ans -= 1
    if 0 <= b < N and S[b] == "B":
      ans -= 1
    if c % 2 == 0 and S[c//2] == "B":
      ans -= 1
print(ans)