Problem:
 Score : 400 points    Problem Statement We have a string S of length N consisting of R , G , and B .  Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:   S_i \neq S_j , S_i \neq S_k , and S_j \neq S_k .  j - i \neq k - j .       Constraints  1 \leq N \leq 4000  S is a string of length N consisting of R , G , and B .         Input Input is given from Standard Input in the following format:  N  S       Output Print the number of triplets in question.        Sample Input 1 4
RRGB      Sample Output 1 1  Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.       Sample Input 2 39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB      Sample Output 2 1800

Solution (Python):
n = int(input())
s = input()

"""
for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
"""

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
ans = r*g*b

for i in range(n-2):
    for j in range(i+1, n-1):
        k = 2 * j - i
        if k < n:
            if s[i] == s[j]:
                continue
            if s[j] == s[k]:
                continue
            if s[k] == s[i]:
                continue
            ans -= 1
print(ans)

Solution (Python):

N=int(input())
S=input()


count= S.count("R")*S.count("G")*S.count("B")
count0=0


for i in range(1,N-2 +1):
    for j in range(i+1,(N+i)//2 +1):
        k=2*j-i
        if S[k-1]!=S[i-1] and S[k-1]!=S[j-1] and S[i-1]!=S[j-1]:
            count0 += 1
print(count-count0)

Solution (Python):


N = int(input())
S = input()

R = {i for i, c in enumerate(S) if c == 'R'}
G = {i for i, c in enumerate(S) if c == 'G'}
B = {i for i, c in enumerate(S) if c == 'B'}

r = len(R)
g = len(G)
b = len(B)

all_ = r * g * b


sub = 0

for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue
        else:
            k = 2 * j - i
            if k < N and S[i] != S[k] and S[j] != S[k]:
                sub += 1

print(all_ - sub)

Solution (Python):
n = int(input())
s = list(input())

num_r = s.count('R')
num_g = s.count('G')
num_b = s.count('B')

ans = num_r*num_g*num_b

for i in range(n):
    for j in range(i, n):
        k = j+(j-i)
        if (k < n) and (s[i] != s[j]) and (s[j] != s[k]) and (s[k] != s[i]):
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')
ans = r_cnt * g_cnt * b_cnt

for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n:
            break
        if s[i] != s[j] != s[k] != s[i]:
                ans -= 1

print(ans)

Solution (Python):
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)


inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import deque, defaultdict, Counter
import math
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))



def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a




def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    n = int(input())
    s = 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
    ans = r*g*b
    
    for i in range(n-2):
        for j in range(i, n-1):
            if s[i] != s[j] and 2*j-i < n and s[j] != s[2*j-i] and s[i] != s[2*j-i]:
                ans -= 1
    print(ans)
            
if __name__ == '__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    S = list(input())
    R = set()
    G = set()
    B = set()

    MIN_MAX = lambda a, b: (b, a) if a > b else (a, b)

    for i, s in enumerate(S):
        if s == 'R':
            R.add(i)
        elif s == 'G':
            G.add(i)
        else:
            B.add(i)

    b_len = len(B)
    ans = 0
    for r in R:
        for g in G:
            v1, v2 = MIN_MAX(r, g)
            rg_diff = v2 - v1
            ans += b_len
            if v2 + rg_diff in B:
                ans -= 1
            if v1 - rg_diff in B:
                ans -= 1
            if (r + g) % 2 == 0 and (r + g) // 2 in B:
                ans -= 1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
s = readline().rstrip().decode()
ans = s.count('R') * s.count('G') * s.count('B')
for i, si in enumerate(s):
    for j, sj in enumerate(s[i + 1:]):
        k = 2 * (j + 1) + i
        if k >= n:
            break
        if si != sj and si != s[k] and sj != s[k]:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()

num_r = s.count("R")
num_g = s.count("G")
num_b = s.count("B")


def search(n,s):
    ans = num_r * num_g * num_b

    for i in range(n):
        for j in range(i,n):
            k=2*j-i
            if (k<n) and (s[i]!=s[j]) and (s[i]!=s[k]) and (s[j]!=s[k]):
                ans -= 1

    return ans
print(search(n,s))

Solution (Python):
n = int(input())
s = list(input())

max_interval = (n-3)//2+1

exception = 0

for interval in range(1,max_interval+1):
    for i in range(n-interval*2):
        if s[i]!=s[i+interval] and s[i+interval]!=s[i+2*interval] and s[i+2*interval]!=s[i]:
            exception += 1

allc = s.count("R")*s.count("G")*s.count("B")

print(allc-exception)

Solution (Python):
n = int(input())
s = list(input())

num_r = s.count('R')
num_g = s.count('G')
num_b = s.count('B')

ans = num_r*num_g*num_b

for i in range(n):
    for j in range(i, n):
        k = j+(j-i)
        if (k < n) and (s[i] != s[j]) and (s[j] != s[k]) and (s[k] != s[i]):
            ans -= 1
print(ans)

Solution (Python):
n = int(input())

s = list(input())

ans = s.count("R")*s.count("G")*s.count("B")

for i in range(n):
    for j in range(i+1,n):
        k = 2*j-i
        if k < n :
            if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                ans -= 1

print(ans)

Solution (Python):
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-2):
    for d in range(1,N//2+1):
        j = i + d
        k = j + d
        if k >= N:break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)

Solution (Python):
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-2):
    for d in range(N//2+1):
        j = i + d
        k = j + d
        if k >= N:break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1
print(ans)

Solution (Python):
def main():
    n = int(input())
    s = input()
    r = s.count("R")
    g = s.count("G")
    b = s.count("B")
    ans = r*g*b

    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i] != s[j]:
                if 2*j-i < n:
                    if s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
                        ans-=1

    
    print(ans)

main()

Solution (Python):
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


















def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    
    n = int(input().strip())
    S = input().strip()
    R = []
    G = []
    B = set()
    for i in range(n):
        if S[i] == 'R':
            R.append(i)
        elif S[i] == 'G':
            G.append(i)
        elif S[i] == 'B':
            B.add(i)
    R.sort()
    G.sort()

    
    n_all = len(R)*len(G)*len(B)
    cnt = 0
    for i in R:
        for j in G:
            high = 2*i - j
            low = 2*j - i
            if (i+j) % 2 == 0:
                mid = (i+j) // 2
            else:
                mid = -1
            cnt += (high in B) + (low in B) + (mid in B)
    eprint('n_all, cnt ',end=':\n')
    eprint(n_all, cnt)
    print(n_all - cnt)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = list(input())

r, g, b = 0, 0, 0
ans = 0
for i in range(N):
    if S[i] == 'R':
        r += 1
    elif S[i] == 'G':
        g += 1
    else:
        b += 1

ans += r*g*b

for i in range(N):
    for j in range(i+1, N):
        if 2*j-i < N:
            if (S[i] != S[j]) and (S[j] != S[2*j-i]) and (S[i] != S[2*j-i]):
                ans -= 1

print(ans)

Solution (Python):
n=int(input())
s=input()
r,g,b=0,0,0
for i in range(n):
    if s[i]=="R":
        r+=1
    elif s[i]=="G":
        g+=1
    else:
        b+=1
m=0
for i in range(n):
    for j in range(n):
        if i-j<0 or i+j>=n:
            break
        if s[i-j]!=s[i] and s[i]!=s[i+j] and s[i-j]!=s[i+j]:
            m+=1
print(r*g*b-m)

Solution (Python):
n = int(input())
s = [1 if i=="R" else 2 if i=="G" else 4 for i in input()]
r = s.count(1)
g = s.count(2)
b = s.count(4)
ans = r*g*b
for i in range(n-1):
    for j in range(1, (n-i-1)//2+1):
        if s[i] + s[i+j] + s[i+j+j] == 7:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
ans = s.count('R') * s.count('G') * s.count('B')
for i in range(n):
  for j in range(i, n-1):
    k = 2*j - i
    if k >= n:
      continue
    if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
      ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')

for i in range(n-2):
    for j in range(i+1, n-1):
        k = j + (j - i)
        if k > n-1:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = n - r - g

ans = r*g*b

for i in range(n-2):
  for j in range(i+1, n-1):
    k = j + (j - i)
    if k > n-1:
    	break
    if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
      ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')

ans = r*g*b

for i in range(n-2):
  for j in range(i+1, n-1):
    k = j + (j - i)
    if k > n-1:
    	break
    if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
      ans -= 1
print(ans)

Solution (Python):
def check(a, b, c, s):
    if((s[a]!=s[b])and(s[a]!=s[c])and(s[b]!=s[c])):
        return True
    else:
        return False

def main():
    n = int(input())
    s = input()

    n_r = 0
    n_g = 0
    n_b = 0

    for i in range(n):
        if(s[i]=='R'):
            n_r = n_r + 1
        elif(s[i]=='G'):
            n_g = n_g + 1
        elif(s[i]=='B'):
            n_b = n_b + 1

    ans = n_r * n_g * n_b

    for i in range(n-2):
        for j in range(i,n-1):
            k = 2*j - i
            if((j<k<n)and(check(i,j,k,s))):
                ans = ans - 1

    print(ans)

main()

Solution (Python):
n = int(input())
s = input()

r = s.count("R")
g = s.count("G")
b = s.count("B")
ans = r*g*b

for i in range(n-1):
  for j in range(i, n):
    k = j + (j - i)
    if k >= n:
      break
    else:
      if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
        ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()

ans = s.count('R')*s.count('G')*s.count('B')
for i in range(n):
    for j in range(i,n-1):
        k = 2*j - i
        if k >= n:
            continue
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ans -= 1


print(ans)

Solution (Python):
N = int(input()) 
S = input() 
r = S.count('R')
g = S.count('G')
b = N - g - r
count = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        k = 2*j-i
        if k < N:
            if not (S[i] == S[j] or S[i] == S[k] or S[j] == S[k]):
                count = count + 1
        else:
            break
print(r*g*b - count)

Solution (Python):
import itertools
N = int(input())
S = input()

out = S.count('R') * S.count('G') * S.count('B')

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))

for i in range(1, N):
  for j in range(N-i*2):
    
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from numba import njit
 
MOD = 10**9 + 7
 
N = int(readline())
S = np.array(list(read().rstrip()), np.int8)
 
R = np.sum(S == ord('R'))
B = np.sum(S == ord('B'))
G = np.sum(S == ord('G'))
 
@njit
def f(S):
    N = len(S)
    ret = 0
    for i in range(N):
        for j in range(i + 1, N):
            k = j + j - i
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ret += 1
    return ret
 
answer = R * B * G - f(S)
print(answer)

Solution (Python):
if __name__ == '__main__':
	N = int(input())
	S = input()

	listS = list(S)

	r = listS.count('R')
	g = listS.count('G')
	b = listS.count('B')

	ans = r*g*b

	for i in range(N-1):
		si = S[i]
		for j in range(i,N):
			sj = S[j]
			if si != sj:
				k = 2 * j - i
				if j < k and k < N:
					sk = S[k]
					if sk != si and sk != sj:
						ans -= 1

	print(ans)

Solution (Python):
N = int(input())
S = input()
r = 0
g = 0
b = 0
for a in range(N):
    if S[a] == "R":
        r += 1
    elif S[a] == "G":
        g += 1
    elif S[a] == "B":
        b += 1
all = r*g*b
count = 0
for i in range(0, N-2):
    for j in range(i+1, N-1):
        k = 2*j-i
        if k < N:
            if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
                count += 1
print(all-count)

Solution (Python):
N = int(input())
S = input()
Sr = Sg = Sb = 0
for c in S:
    if c == 'R':
        Sr += 1
    elif c == 'G':
        Sg += 1
    else:
        Sb += 1
A = Sr * Sg * Sb
for sep in range(1,(N+1)//2):
    for i in range(N-2*sep):
        if S[i] != S[i+sep] and S[i+sep] != S[i+2*sep] and S[i] != S[i+2*sep]:
            A -= 1
print(A)

Solution (Python):
n=int(input())
s=input().rstrip()
r=s.count("R")
g=s.count("G")
b=s.count("B")
count=0
for i in range(n-2):
    for j in range(i+1,n-1):
        if j+j-i<n:
            if s[i]!=s[j] and s[i]!=s[j+j-i] and s[j]!=s[j+j-i]:
                count+=1
print(r*g*b-count)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]


r_list= []
g_list = []
b_list = []
def length(t):
    if t == "R":
        return len(g_list), len(b_list)
    elif t == "G":
        return len(b_list), len(r_list)
    else:
        return len(r_list), len(g_list)

def rgb_list(t):
    if t == "R":
        return g_list, b_list
    elif t == "G":
        return b_list, r_list
    else:
        return r_list, g_list
n = i()
s = s()

for i, t in enumerate(s):
    if t == "R":
        r_list.append(i)
    elif t == "G":
        g_list.append(i)
    else:
        b_list.append(i)

rgb = ["R", "G", "B"]
s_start = 0
t_start = 0
r_start = 0
g_start = 0
b_start = 0

for i, first in enumerate(s):
    a1 = 0
    a2 = 0
    if first == "R":
        for g_i in range(g_start, len(g_list)):
            if i < g_list[g_i]:
                g_start = g_i
                a1 = 1
                break
        for b_i in range(b_start, len(b_list)):
            if i < b_list[b_i]:
                b_start = b_i
                a2 = 1
                break
        if a1 == 1 and a2 == 1:
            ans += (len(g_list) - g_start) * (len(b_list) - b_start)

    elif first == "G":
        for r_i in range(r_start, len(r_list)):
            if i < r_list[r_i]:
                r_start = r_i
                a1 = 1
                break
        for b_i in range(b_start, len(b_list)):
            if i < b_list[b_i]:
                b_start = b_i
                a2 = 1
                break
        if a1 == 1 and a2 == 1:
            ans += (len(r_list) - r_start) * (len(b_list) - b_start)

    else:
        for g_i in range(g_start, len(g_list)):
            if i < g_list[g_i]:
                g_start = g_i
                a1 = 1
                break
        for r_i in range(r_start, len(r_list)):
            if i < r_list[r_i]:
                r_start = r_i
                a2 = 1
                break
        if a1 == 1 and a2 == 1:
            ans += (len(g_list) - g_start) * (len(r_list) - r_start)

for i, first in enumerate(s):
    interval = 1
    while i + 2 * interval < len(s):
        if first != s[i + interval] and s[i + interval] != s[i + 2 * interval] and s[i + 2 * interval] != first:
            ans -= 1
        interval += 1
print(ans)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]


r_list= []
g_list = []
b_list = []
def length(t):
    if t == "R":
        return len(g_list), len(b_list)
    elif t == "G":
        return len(b_list), len(r_list)
    else:
        return len(r_list), len(g_list)

def rgb_list(t):
    if t == "R":
        return g_list, b_list
    elif t == "G":
        return b_list, r_list
    else:
        return r_list, g_list
n = i()
s = s()

for i, t in enumerate(s):
    if t == "R":
        r_list.append(i)
    elif t == "G":
        g_list.append(i)
    else:
        b_list.append(i)

rgb = ["R", "G", "B"]
s_start = 0
t_start = 0

a = [i for i in range(n)]

def isOK(mid, point):
    if mid >= point:
        return True
    else:
        return False



def binary_search(the_list, point):  
    ng = -1 
    ok = len(the_list) 

    
    while abs(ok - ng) > 1:
        mid = the_list[(ok + ng) // 2]
        if isOK(mid, point):
            ok = (ok + ng) // 2
        else:
            ng = (ok + ng) // 2
    if ok < len(the_list):
        return the_list[ok], ok
    else:
        return "", ""

def go(a):
    if a == "":
        return False
    return True

for i, first in enumerate(s):
    s_len, t_len = length(first)
    s_list, t_list = rgb_list(first)

    s_i, s_num = binary_search(s_list, i)
    if go(s_i):
        t_i, t_num = binary_search(t_list, i)
        if go(t_num):
            ans += (len(s_list) - s_num) * (len(t_list) - t_num)

for i, first in enumerate(s):
    interval = 1
    while i + 2 * interval < len(s):
        if first != s[i + interval] and s[i + interval] != s[i + 2 * interval] and s[i + 2 * interval] != first:
            ans -= 1
        interval += 1
print(ans)

Solution (Python):
n = int(input())
s = str(input())

count = [0] * 3

for i in range(n):
    if s[i] == 'R':
        count[0] += 1
    if s[i] == 'G':
        count[1] += 1
    if s[i] == 'B':
        count[2] += 1

ans = count[0] * count[1] * count[2]

for i in range(n):
    k = 1
    while i + 2 * k < n:
        l = []
        l.append(s[i])
        l.append(s[i+k])
        l.append(s[i+2*k])
        if l.count('R') == 1 and l.count('G') == 1 and l.count('B') == 1:
            ans -= 1
        
        k += 1

print(ans)

Solution (Python):
n=int(input())
s=[str(x) for x in input()]

error=0
for i in range(n-2):
    for j in range(i+1,(n+i+1)//2):
        k=2*j-i
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            error += 1
r=s.count('R')
g=s.count('G')
b=s.count('B')
print(r*g*b-error)

Solution (Python):
N = int(input())
S = input()




r = g = b = 0
for s in range(N):
    if S[s] == 'R':
        r += 1
    elif S[s] == 'G':
        g += 1
    else:
        b += 1

total = r * g * b


for gap in range(1, N//2+1):
    for i in range(N - 2 * gap):
        j = i + gap
        k = j + gap
        if k >= N:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            total -= 1

print(total)

Solution (Python):
N = int(input())
S = input()


total = S.count('R') * S.count('G') * S.count('B')


for gap in range(1, N//2+1):
    for i in range(N - 2 * gap):
        j = i + gap
        k = j + gap
        if k >= N:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            total -= 1

print(total)

Solution (Python):
N=int(input())
S=input()

ans=S.count("R")*S.count("G")*S.count("B")



for i in range(N):
  for j in range(i+1,N):
    if 2*j-i>=N:
      break
      
    if S[i]!=S[j] and S[i]!=S[2*j-i] and S[j]!=S[2*j-i]:
      ans -=1
        
print(ans)

Solution (Python):
N=int(input())
S=input()

ans=S.count("R")*S.count("G")*S.count("B")



for i in range(N):
  for j in range(i+1,N):
    if 2*j-i<N:
      if S[i]!=S[j] and S[i]!=S[2*j-i] and S[j]!=S[2*j-i]:
        ans -=1
        
print(ans)

Solution (Python):
N = int(input())
S = str(input())

ans = S.count('R')*S.count('G')*S.count('B')

for i in range(N):
    for j in range(i+1,N):
        k = 2*j - i
        if k>=N:
            break
        if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
            ans -=1

print(ans)

Solution (Python):
n=int(input())
s=list(input())
ans=s.count('R')*s.count('G')*s.count('B')
for i in range(n):
  if s[i]=='R':
    s[i]=1
  elif s[i]=='G':
    s[i]=2
  elif s[i]=='B':
    s[i]=4
for i in range(n):
   for j in range(i+1,n):
      k=2*j-i
      if k>=n:
        continue
      if s[i]+s[j]+s[k]==7:
        ans-=1
print(ans)

Solution (Python):
N=int(input())
S=input()
count=0
count=S.count('R')*S.count('G')*S.count('B')
for i in range(N-2):
    for j in range(1,(N+i+1)//2-i):

        if(S[i]+S[i+j]+S[i+2*j] in ['RGB','RBG','GRB','GBR','BGR','BRG']):

            count=count-1
print(count)

Solution (Python):
import sys


def main():
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    A = S.count('R')*S.count('G')*S.count('B')
    assert 1 <= N <= 4000
    assert N == len(S)
    total = 0
    for j in range(1, N-1):
        for i in range(j):
            k = 2*j-i
            if k < len(S):
                if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    total += 1
    print(A - total)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


def main():
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    A = S.count('R')*S.count('G')*S.count('B')
    assert 1 <= N <= 4000
    total = 0
    for j in range(1, N-1):
        for i in range(j):
            k = 2*j-i
            if k < len(S):
                if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    total += 1
    print(A - total)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import collections


def main():
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    A = S.count('R')*S.count('G')*S.count('B')
    total = 0
    for j in range(1, N-1):
        for i in range(j):
            k = 2*j-i
            if k < len(S):
                if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    total += 1
    print(A - total)


def main2():
    N = int(input())
    S = input()
    A = S.count('R')*S.count('G')*S.count('B')
    order = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            order += 1
            if 2*j-i < N:
                if S[i] != S[j] and S[i] != S[2*j-i] and S[j] != S[2*j-i]:
                    A -= 1
    print(A)
    print(order)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(input())
ans = A.count("R")*A.count("G")*A.count("B")
for i in range(N-2):
  for j in range(i+1, N-1):
    k = 2*j-i
    if k<N and A[k] != A[i] and A[k] != A[j] and A[i] != A[j]:
      ans -= 1
print(ans)

Solution (Python):
N=int(input())
S=input()
A=S.count('R')*S.count('G')*S.count('B')
for i in range(N-2):
    for j in range(i+1,N-1):
        if 2*j-i < N:
            if S[i] != S[j] and S[i] != S[2*j-i] and S[j] != S[2*j-i]:
                A-=1
print(A)

Solution (Python):
n = int(input())
s = input()
r,g,b =0,0,0
ls = len(s)
for i in range(ls):
    if s[i]=='R':
        r+=1
    elif s[i]=='G':
        g+=1
    elif s[i]=='B':
        b+=1
cnt =0
for j in range(1,ls-1):
    for i in range(j):
        k = 2*j-i
        if k<ls:
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                cnt+=1
print(r*g*b-cnt)

Solution (Python):
N = int(input())
S = 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
         
a = 0         
         
for i in range(N):
  for j in range(i+1,N):
    k = 2*j - i
    if k < N:
      if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
        a += 1
      
print(r*g*b - a)

Solution (Python):
N=int(input())
S=input()
r,g,b=0,0,0
for i in range(N):
    if S[i]=="R":
        r+=1
    elif S[i]=="G":
        g+=1
    elif S[i]=="B":
        b+=1

tmp=0
for i in range(N):
    for j in range(i+1,N):
        k=2*j-i
        if k<N and k>=0:
            if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
                tmp+=1


print(r*g*b-tmp)

Solution (Python):
n = int(input())
s = input()
r,g,b =0,0,0
ls = len(s)
for i in range(ls):
    if s[i]=='R':
        r+=1
    elif s[i]=='G':
        g+=1
    elif s[i]=='B':
        b+=1
cnt =0
for j in range(1,ls-1):
    for i in range(j):
        k = 2*j-i
        if k<ls:
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                cnt+=1
print(r*g*b-cnt)

Solution (Python):
N = int(input())
S = list(input())
count = S.count("R")*S.count("B")*S.count("G")
for i in range(N):
    for j in range(i+1,N):
        if 2*j-i < N:
            if S[i]!=S[j] and S[2*j-i]!=S[i] and S[2*j-i]!=S[j]:
                count-=1
print(count)

Solution (Python):
N = int(input())
S = list(input())
count = S.count("R")*S.count("B")*S.count("G")
for i in range(N):
    for j in range(i+1,N):
        if 2*j-i < N:
            if S[i]!=S[j] and S[2*j-i]!=S[i] and S[2*j-i]!=S[j]:
                count-=1
print(count)

Solution (Python):
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
count = r * g * b
for i in range(N):
    for j in range(i+1,N-1):
        if 2*j - i <= N-1:
            l = set([S[i],S[j],S[2*j-i]])
            if len(l) == 3:
                count -= 1
print(count)

Solution (Python):
import itertools
N = int(input())
S = input()
out = S.count('R') * S.count('G') * S.count('B')

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))

for i in range(1, N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)

Solution (Python):
import itertools
N = int(input())
S = input()
out = S.count('R') * S.count('G') * S.count('B')

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))

for i in range(1, N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)