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 in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>

int main(void)
{
	int i,x,y,N,r[4001],g[4001],b[4001],rc=0,gc=0,bc=0,rp[4001],gp[4001],bp[4001];
	char S[4001];
	long long total=0;
	scanf("%d",&N);
	scanf("%s",S);
	for(i=0;i<N;i++){
		if(S[i]=='R'){
			rp[rc]=i;
			rc++;
		}else if(S[i]=='G'){
			gp[gc]=i;
			gc++;
		}else if(S[i]=='B'){
			bp[bc]=i;
			bc++;
		}
		r[i]=rc;
		g[i]=gc;
		b[i]=bc;
	}
	if(rc==0||gc==0||bc==0){}
	else{
	for(x=0;x<rc;x++){
		for(y=0;y<gc;y++){
			if(rp[x]<gp[y]){
				if(S[gp[y]*2-rp[x]]=='B')total+=(long long)bc-b[gp[y]]-1;
				else total+=(long long)bc-b[gp[y]];
			}
		}
	}
	for(x=0;x<gc;x++){
		for(y=0;y<bc;y++){
			if(gp[x]<bp[y]){
				if(S[bp[y]*2-gp[x]]=='R')total+=(long long)rc-r[bp[y]]-1;
				else total+=(long long)rc-r[bp[y]];
			}
		}
	}
	for(x=0;x<bc;x++){
		for(y=0;y<rc;y++){
			if(bp[x]<rp[y]){
				if(S[rp[y]*2-bp[x]]=='G')total+=(long long)gc-g[rp[y]]-1;
				else total+=(long long)gc-g[rp[y]];
			}
		}
	}
	for(x=0;x<rc;x++){
		for(y=0;y<bc;y++){
			if(rp[x]<bp[y]){
				if(S[bp[y]*2-rp[x]]=='G')total+=(long long)gc-g[bp[y]]-1;
				else total+=(long long)gc-g[bp[y]];
			}
		}
	}
	for(x=0;x<gc;x++){
		for(y=0;y<rc;y++){
			if(gp[x]<rp[y]){
				if(S[rp[y]*2-gp[x]]=='B')total+=(long long)bc-b[rp[y]]-1;
				else total+=(long long)bc-b[rp[y]];
			}
		}
	}
	for(x=0;x<bc;x++){
		for(y=0;y<gc;y++){
			if(bp[x]<gp[y]){
				if(S[gp[y]*2-bp[x]]=='R')total+=(long long)rc-r[gp[y]]-1;
				else total+=(long long)rc-r[gp[y]];
			}
		}
	}
	}
	printf("%lld",total);
	return 0;
}