Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=input()
r=[]
g=[]
b=[]
cnt=0
for i in range(n):
  if s[i]=="R":
    r.append(i)
  elif s[i]=="G":
    g.append(i)
  else:
    b.append(i)
for i in r:
  for j in range(i,n):
    if s[j]=="G":
      if 2*j-i<=n-1:
        if s[2*j-i]=="B":
          cnt+=(sum(x>j for x in b)-1)
        elif s[2*j-i]!="B":
          cnt+=sum(x>j for x in b)
      else:
        cnt+=sum(x>j for x in b)
    elif s[j]=="B":
      if 2*j-i<=n-1:
        if s[2*j-i]=="G":
          cnt+=(sum(x>j for x in g)-1)
        elif s[2*j-i]!="G":
          cnt+=sum(x>j for x in g)
      else:
        cnt+=sum(x>j for x in g)

for i in g:
  for j in range(i,n):
    if s[j]=="R":
      if 2*j-i<=n-1:
        if s[2*j-i]=="B":
          cnt+=(sum(x>j for x in b)-1)
        elif s[2*j-i]!="B":
          cnt+=sum(x>j for x in b)
      else:
        cnt+=sum(x>j for x in b)
    elif s[j]=="B":
      if 2*j-i<=n-1:
        if s[2*j-i]=="R":
          cnt+=(sum(x>j for x in r)-1)
        elif s[2*j-i]!="R":
          cnt+=sum(x>j for x in r)
      else:
        cnt+=sum(x>j for x in r)

for i in b:
  for j in range(i,n):
    if s[j]=="G":
      if 2*j-i<=n-1:
        if s[2*j-i]=="R":
          cnt+=(sum(x>j for x in r)-1)
        elif s[2*j-i]!="R":
          cnt+=sum(x>j for x in r)
      else:
        cnt+=sum(x>j for x in r)
    elif s[j]=="R":
      if 2*j-i<=n-1:
        if s[2*j-i]=="G":
          cnt+=(sum(x>j for x in g)-1)
        elif s[2*j-i]!="G":
          cnt+=sum(x>j for x in g)
      else:
        cnt+=sum(x>j for x in g)
print(cnt)

Fixed submission:
n=int(input())
s=input()
t=s.count("R")*s.count("G")*s.count("B")
cnt=0
for i in range(n):
  for j in range(i,n):
    if 2*j-i>=n:
      break
    else:
      if s[i]!=s[j]!=s[2*j-i]!=s[i]:
        cnt+=1
print(t-cnt)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=input()
r=[]
g=[]
b=[]
cnt=0
for i in range(n):
  if s[i]=="R":
    r.append(i)
  elif s[i]=="G":
    g.append(i)
  else:
    b.append(i)
for i in r:
  for j in range(i,n):
    if s[j]=="G":
      if 2*j-i<=n-1:
        if s[2*j-i]=="B":
          cnt+=(sum(x>j for x in b)-1)
        elif s[2*j-i]!="B":
          cnt+=sum(x>j for x in b)
      else:
        cnt+=sum(x>j for x in b)
    elif s[j]=="B":
      if 2*j-i<=n-1:
        if s[2*j-i]=="G":
          cnt+=(sum(x>j for x in g)-1)
        elif s[2*j-i]!="G":
          cnt+=sum(x>j for x in g)
      else:
        cnt+=sum(x>j for x in g)

for i in g:
  for j in range(i,n):
    if s[j]=="R":
      if 2*j-i<=n-1:
        if s[2*j-i]=="B":
          cnt+=(sum(x>j for x in b)-1)
        elif s[2*j-i]!="B":
          cnt+=sum(x>j for x in b)
      else:
        cnt+=sum(x>j for x in b)
    elif s[j]=="B":
      if 2*j-i<=n-1:
        if s[2*j-i]=="R":
          cnt+=(sum(x>j for x in r)-1)
        elif s[2*j-i]!="R":
          cnt+=sum(x>j for x in r)
      else:
        cnt+=sum(x>j for x in r)

for i in b:
  for j in range(i,n):
    if s[j]=="G":
      if 2*j-i<=n-1:
        if s[2*j-i]=="R":
          cnt+=(sum(x>j for x in r)-1)
        elif s[2*j-i]!="R":
          cnt+=sum(x>j for x in r)
      else:
        cnt+=sum(x>j for x in r)
    elif s[j]=="R":
      if 2*j-i<=n-1:
        if s[2*j-i]=="G":
          cnt+=(sum(x>j for x in g)-1)
        elif s[2*j-i]!="G":
          cnt+=sum(x>j for x in g)
      else:
        cnt+=sum(x>j for x in g)
print(cnt)

Fixed submission:
n=int(input())
s=input()
t=s.count("R")*s.count("G")*s.count("B")
cnt=0
for i in range(n):
  for j in range(i,n):
    if 2*j-i>=n:
      break
    else:
      if s[i]!=s[j]!=s[2*j-i]!=s[i]:
        cnt+=1
print(t-cnt)