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

R=[]
G=[]
B=[]

# リストを作成
for i in range(0,N):
  if S[i]=="R":
    R += [i]
  elif S[i]=="G":
    G += [i]
  else:
    B += [i]

def search(ind, col):
  n=0
  useable_col = ["R","G","B"]
  useable_col.remove(col)
  for i,j in [[0,1],[1,0]]:
    l_col=useable_col[i]
    r_col=useable_col[j]
    if l_col=="R":
      l_col=R
    elif l_col=="G":
      l_col=G
    else:
      l_col=B
    if r_col=="R":
      r_col=R
    elif r_col=="G":
      r_col=G
    else:
      r_col=B
    
    l_useable = [] 
    r_useable = [] 
    for ii in l_col:
      if ii<ind:
        l_useable += [ii]
      else:
        break
    if l_useable == []:
      continue
    for ii in reversed(r_col):
      if ii>ind:
        r_useable += [ii]
      else:
        break
    if r_useable == []:
      continue

    # print(l_useable)
    # print(r_useable)
    l = min(ind, N-1-ind)
    for m in range(1,l+1):
      if ind-m in l_useable and ind+m in r_useable:
        n -= 1
    n += len(l_useable) * len(r_useable)
    # print(len(l_useable))
    # print(len(r_useable))
  return n

n=0
for pivot in range(1,N-1):
  if S[pivot] =="R":
    n += search(pivot, "R")
  elif S[pivot] =="G":
    n += search(pivot, "G")
  else:
    n += search(pivot, "B")

print(n)


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

r=0
g=0
b=0

for i in range(N):
  if S[i]=="R":
    r+=1
  elif S[i]=="G":
    g+=1
  else:
    b+=1
    
n=r*g*b

for i in range(N):
  for j in range(i+1,N):
    if S[i]==S[j]:
      continue
    k = 2*j-i
    if k >= N:
      break
    if S[i]==S[k] or S[j]==S[k]:
      continue
    n -= 1
      
print(n)

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

R=[]
G=[]
B=[]

# リストを作成
for i in range(0,N):
  if S[i]=="R":
    R += [i]
  elif S[i]=="G":
    G += [i]
  else:
    B += [i]

def search(ind, col):
  n=0
  useable_col = ["R","G","B"]
  useable_col.remove(col)
  for i,j in [[0,1],[1,0]]:
    l_col=useable_col[i]
    r_col=useable_col[j]
    if l_col=="R":
      l_col=R
    elif l_col=="G":
      l_col=G
    else:
      l_col=B
    if r_col=="R":
      r_col=R
    elif r_col=="G":
      r_col=G
    else:
      r_col=B
        
    l_useable = [ii for ii in l_col if ii<ind]
    r_useable = [ii for ii in r_col if ii>ind]
    if (l_useable == []) or (r_useable == []):
      continue

    # print(l_useable)
    # print(r_useable)
    l = min(ind, N-1-ind)
    for m in range(1,l+1):
      if ind-m in l_useable and ind+m in r_useable:
        n -= 1
    n += len(l_useable) * len(r_useable)
    # print(len(l_useable))
    # print(len(r_useable))
  return n

n=0
for pivot in range(1,N-1):
  if S[pivot] =="R":
    n += search(pivot, "R")
  elif S[pivot] =="G":
    n += search(pivot, "G")
  else:
    n += search(pivot, "B")

print(n)


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

r=0
g=0
b=0

for i in range(N):
  if S[i]=="R":
    r+=1
  elif S[i]=="G":
    g+=1
  else:
    b+=1
    
n=r*g*b

for i in range(N):
  for j in range(i+1,N):
    if S[i]==S[j]:
      continue
    k = 2*j-i
    if k >= N:
      break
    if S[i]==S[k] or S[j]==S[k]:
      continue
    n -= 1
      
print(n)

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

R=[]
G=[]
B=[]

# リストを作成
for i in range(0,N):
  if S[i]=="R":
    R += [i]
  elif S[i]=="G":
    G += [i]
  else:
    B += [i]

def search(ind, col):
  n=0
  useable_col = ["R","G","B"]
  useable_col.remove(col)
  for i,j in [[0,1],[1,0]]:
    l_col=useable_col[i]
    r_col=useable_col[j]
    if l_col=="R":
      l_col=R
    elif l_col=="G":
      l_col=G
    else:
      l_col=B
    if r_col=="R":
      r_col=R
    elif r_col=="G":
      r_col=G
    else:
      r_col=B
        
    l_useable = [ii for ii in l_col if ii<ind]
    r_useable = [ii for ii in r_col if ii>ind]
    # print(l_useable)
    # print(r_useable)
    l = min(ind, N-1-ind)
    for m in range(1,l+1):
      if ind-m in l_useable and ind+m in r_useable:
        n -= 1
    n += len(l_useable) * len(r_useable)
  return n

n=0
for pivot in range(1,N-1):
  if S[pivot] =="R":
    n += search(pivot, "R")
  elif S[pivot] =="G":
    n += search(pivot, "G")
  else:
    n += search(pivot, "B")

print(n)


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

r=0
g=0
b=0

for i in range(N):
  if S[i]=="R":
    r+=1
  elif S[i]=="G":
    g+=1
  else:
    b+=1
    
n=r*g*b

for i in range(N):
  for j in range(i+1,N):
    if S[i]==S[j]:
      continue
    k = 2*j-i
    if k >= N:
      break
    if S[i]==S[k] or S[j]==S[k]:
      continue
    n -= 1
      
print(n)