Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = list(input())

rl = []
gl = []
bl = []
rld = [0]*(n)
gld = [0]*(n)
bld = [0]*(n)

for i in range(len(s)):
  if s[i] == 'R':
    rl += [i]
    rld[i] = 1 
  elif s[i] == 'G':
    gl += [i]
    gld[i] = 1 
  else:
    bl += [i]
    bld[i] = 1

ans = 0 
for i in rl:
  for j in gl:
    #print(i, j, j+j-i, bl[j+1:])
    #print(bl[j+1:])
    if i >= j:
      continue
    ans += sum(bld[j+1:])
    if j+j-i < n:
      ans -= bld[j+j-i]

for i in rl:
  for j in bl:
    if i >= j:
      continue
    ans += sum(gld[j+1:])
    if j+j-i < n:
      ans -= gld[j+j-i]

for i in gl:
  for j in bl:
    if i >= j:
      continue
    ans += sum(rld[j+1:])
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in gl:
  for j in rl:
    if i >= j:
      continue
    ans += sum(bld[j+1:])
    if j+j-i < n:
      ans -= bld[j+j-i]
for i in bl:
  for j in gl:
    if i >= j:
      continue
    ans += sum(rld[j+1:])
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in bl:
  for j in rl:
    if i >= j:
      continue
    ans += sum(gld[j+1:])
    if j+j-i < n:
      ans -= gld[j+j-i]
    
print(ans)


Fixed submission:
n = int(input())
s = list(input())

rl = []
gl = []
bl = []
rld = [0]*(n)
gld = [0]*(n)
bld = [0]*(n)

for i in range(len(s)):
  if s[i] == 'R':
    rl += [i]
    rld[i] = 1 
  elif s[i] == 'G':
    gl += [i]
    gld[i] = 1 
  else:
    bl += [i]
    bld[i] = 1

rlds = rld[:]
glds = gld[:]
blds = bld[:]

for i in range(n-1, 0, -1):
  blds[i-1] += blds[i]
  rlds[i-1] += rlds[i] 
  glds[i-1] += glds[i] 

ans = 0 
for i in rl:
  for j in gl:
    if i >= j or j+1 >= n:
      continue
    ans += blds[j+1]
    if j+j-i < n:
      ans -= bld[j+j-i]

for i in rl:
  for j in bl:
    if i >= j or j+1 >= n:
      continue
    ans += glds[j+1]
    if j+j-i < n:
      ans -= gld[j+j-i]

for i in gl:
  for j in bl:
    if i >= j or j+1 >= n:
      continue
    ans += rlds[j+1]
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in gl:
  for j in rl:
    if i >= j or j+1 >= n:
      continue
    ans += blds[j+1]
    if j+j-i < n:
      ans -= bld[j+j-i]
for i in bl:
  for j in gl:
    if i >= j or j+1 >= n:
      continue
    ans += rlds[j+1]
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in bl:
  for j in rl:
    if i >= j or j+1 >= n:
      continue
    ans += glds[j+1]
    if j+j-i < n:
      ans -= gld[j+j-i]
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = list(input())

rl = []
gl = []
bl = []
rld = [0]*(n)
gld = [0]*(n)
bld = [0]*(n)

for i in range(len(s)):
  if s[i] == 'R':
    rl += [i]
    rld[i] = 1 
  elif s[i] == 'G':
    gl += [i]
    gld[i] = 1 
  else:
    bl += [i]
    bld[i] = 1

ans = 0 
for i in rl:
  for j in gl:
    #print(i, j, j+j-i, bl[j+1:])
    #print(bl[j+1:])
    if i >= j:
      continue
    ans += sum(bld[j+1:])
    if j+j-i < n:
      ans -= bld[j+j-i]

for i in rl:
  for j in bl:
    if i >= j:
      continue
    ans += sum(gld[j+1:])
    if j+j-i < n:
      ans -= gld[j+j-i]

for i in gl:
  for j in bl:
    if i >= j:
      continue
    ans += sum(rld[j+1:])
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in gl:
  for j in rl:
    if i >= j:
      continue
    ans += sum(bld[j+1:])
    if j+j-i < n:
      ans -= bld[j+j-i]
for i in bl:
  for j in gl:
    if i >= j:
      continue
    ans += sum(rld[j+1:])
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in bl:
  for j in rl:
    if i >= j:
      continue
    ans += sum(gld[j+1:])
    if j+j-i < n:
      ans -= gld[j+j-i]
    
print(ans)


Fixed submission:
n = int(input())
s = list(input())

rl = []
gl = []
bl = []
rld = [0]*(n)
gld = [0]*(n)
bld = [0]*(n)

for i in range(len(s)):
  if s[i] == 'R':
    rl += [i]
    rld[i] = 1 
  elif s[i] == 'G':
    gl += [i]
    gld[i] = 1 
  else:
    bl += [i]
    bld[i] = 1

rlds = rld[:]
glds = gld[:]
blds = bld[:]

for i in range(n-1, 0, -1):
  blds[i-1] += blds[i]
  rlds[i-1] += rlds[i] 
  glds[i-1] += glds[i] 

ans = 0 
for i in rl:
  for j in gl:
    if i >= j or j+1 >= n:
      continue
    ans += blds[j+1]
    if j+j-i < n:
      ans -= bld[j+j-i]

for i in rl:
  for j in bl:
    if i >= j or j+1 >= n:
      continue
    ans += glds[j+1]
    if j+j-i < n:
      ans -= gld[j+j-i]

for i in gl:
  for j in bl:
    if i >= j or j+1 >= n:
      continue
    ans += rlds[j+1]
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in gl:
  for j in rl:
    if i >= j or j+1 >= n:
      continue
    ans += blds[j+1]
    if j+j-i < n:
      ans -= bld[j+j-i]
for i in bl:
  for j in gl:
    if i >= j or j+1 >= n:
      continue
    ans += rlds[j+1]
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in bl:
  for j in rl:
    if i >= j or j+1 >= n:
      continue
    ans += glds[j+1]
    if j+j-i < n:
      ans -= gld[j+j-i]
    
print(ans)