Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n=int(input())
  s=tuple(input())
  r=[i for i, x in enumerate(s) if x == 'R']
  g=[i for i, x in enumerate(s) if x == 'G']
  rg=r+g
  b=[i for i in range(n) if not i in rg]
  R,G,B=len(r),len(g),len(b)
  ans=R*G*B
  for i in r:
    for j in g:
      ans+=-1 if (i+j)/2 in b else 0 
      ans+=-1 if ((i+j)+abs(i-j)*3)/2 in b else 0
      ans+=-1 if ((i+j)-abs(i-j)*3)/2 in b else 0
  print(ans)
if __name__=='__main__':
  main()

Fixed submission:
def main():
  n=int(input())
  s=input()
  R,G,B=s.count('R'),s.count('G'),s.count('B')
  ans=R*G*B
  for i in range(n):
    for j in range(i+1,n):
      if 2*j-i>n-1:
        break
      if s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
        ans-=1
  print(ans)
if __name__=='__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n=int(input())
  s=tuple(input())
  r=[i for i, x in enumerate(s) if x == 'R']
  g=[i for i, x in enumerate(s) if x == 'G']
  rg,ans,R,G=r+g,0,len(r),len(g)
  b=[i for i in range(n) if not i in rg]
  x=n-R-G
  for i in r:
    for j in g:
      ans+=x +(-1 if (i+j)/2 in b else 0) 
      ans+=-1 if ((i+j)+abs(i-j)*3)/2 in b else 0
      ans+=-1 if ((i+j)-abs(i-j)*3)/2 in b else 0
  print(ans)
if __name__=='__main__':
  main()

Fixed submission:
def main():
  n=int(input())
  s=input()
  R,G,B=s.count('R'),s.count('G'),s.count('B')
  ans=R*G*B
  for i in range(n):
    for j in range(i+1,n):
      if 2*j-i>n-1:
        break
      if s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
        ans-=1
  print(ans)
if __name__=='__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n=int(input())
  s=tuple(input())
  r=[i for i, x in enumerate(s) if x == 'R']
  g=[i for i, x in enumerate(s) if x == 'G']
  rg,ans=r+g,0
  b=[i for i in range(n) if not i in rg]
  R,G=len(r),len(g)
  for i in r:
    for j in g:
      ans+=n-R-G +(-1 if (i+j)/2 in b else 0) 
      x,y=((i+j)+abs(i-j)*3)/2,((i+j)-abs(i-j)*3)/2
      ans+=-1 if x in b else 0
      ans+=-1 if y in b else 0
  print(ans)
if __name__=='__main__':
  main()

Fixed submission:
def main():
  n=int(input())
  s=input()
  R,G,B=s.count('R'),s.count('G'),s.count('B')
  ans=R*G*B
  for i in range(n):
    for j in range(i+1,n):
      if 2*j-i>n-1:
        break
      if s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
        ans-=1
  print(ans)
if __name__=='__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n=int(input())
  s=tuple(input())
  r=[i for i, x in enumerate(s) if x == 'R']
  g=[i for i, x in enumerate(s) if x == 'G']
  b=[i for i, x in enumerate(s) if x == 'B']
  rg=r+g
  ans=0
  R,G=len(r),len(g)
  for i in r:
    for j in g:
      ans+=n-R-G +(-1 if ((i+j)%2==0  and (i+j)/2 in b) else 0) 
      x,y=((i+j)+abs(i-j)*3)/2,((i+j)-abs(i-j)*3)/2
      ans+=-1 if x in b else 0
      ans+=-1 if y in b else 0
  print(ans)
if __name__=='__main__':
  main()

Fixed submission:
def main():
  n=int(input())
  s=input()
  R,G,B=s.count('R'),s.count('G'),s.count('B')
  ans=R*G*B
  for i in range(n):
    for j in range(i+1,n):
      if 2*j-i>n-1:
        break
      if s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
        ans-=1
  print(ans)
if __name__=='__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=list(input())
r=[i for i, x in enumerate(s) if x == 'R']
g=[i for i, x in enumerate(s) if x == 'G']
b=[i for i, x in enumerate(s) if x == 'B']
R,G,B=len(r),len(g),len(b)
ans=0
for i in range(R):
    for j in range(G):
        for k in range(B):
            l=sorted([r[i],g[j],b[k]])
            ans+=1 if l[1]-l[0]!=l[2]-l[1]  else 0
print(ans)

Fixed submission:
def main():
  n=int(input())
  s=input()
  R,G,B=s.count('R'),s.count('G'),s.count('B')
  ans=R*G*B
  for i in range(n):
    for j in range(i+1,n):
      if 2*j-i>n-1:
        break
      if s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
        ans-=1
  print(ans)
if __name__=='__main__':
  main()