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 Rust (1.42.0) (judge verdict: Wrong Answer)

#![allow(unused_imports)]
#![allow(clippy::many_single_char_names)]
#![allow(clippy::suspicious_arithmetic_impl)]

// using Rust(1.42.0)
// My Library Repo is at "https://github.com/Loptall/prcn_lib"
// check available crates on AtCoder at "https://atcoder.jp/contests/language-test-202001"

/*
    AtCoder Language Update 2020204 お疲れ様でした! 有難いです
*/

// Dep crates are...
use std::cmp::{max, min, Ordering};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::mem::swap;
use std::ops::*;
use std::{i32, i64, isize, u32, u64, usize};

use itertools::*;
use itertools_num::*;
use lazy_static::lazy_static;
use maplit::{btreemap, btreeset, hashmap, hashset};
use num_bigint::{BigInt, BigUint};
use num_integer::{binomial, gcd, lcm, Integer};
use permutohedron::Heap;
use proconio::{fastout, input, is_stdin_empty, marker::*};
use rand::random;

// code...
#[fastout]
fn main() {
    input! {
        n: usize,
        s: Chars
    }

    let mut r = vec![0; n+1];
    let mut g = vec![0; n+1];
    let mut b = vec![0; n+1];


    for (i, c) in s.iter().enumerate() {
        r[i+1] = r[i] + if *c == 'R' { 1 } else { 0 };
        g[i+1] = g[i] + if *c == 'G' { 1 } else { 0 };
        b[i+1] = b[i] + if *c == 'B' { 1 } else { 0 };
    }

    println!("{:?}", r);
    println!("{:?}", g);
    println!("{:?}", b);
    let mut rr = vec![0; n-1];
    let mut gg = vec![0; n-1];
    let mut bb = vec![0; n-1];

    for i in 0..n {
        for j in 0..n {
            if (i + j) % 2 == 0 {
                match (s[i], s[j]) {
                    ('R', 'G') | ('G', 'R') => bb[((i+j)/2)-1] += 1,
                    ('B', 'G') | ('G', 'B') => rr[((i+j)/2)-1] += 1,
                    ('R', 'B') | ('B', 'R') => gg[((i+j)/2)-1] += 1,
                    _ => {}
                }
            }
        }
    }

    println!("{:?}", rr);
    println!("{:?}", gg);
    println!("{:?}", bb);

    let mut ans = 0;

    for (i, mid) in s.iter().enumerate().skip(1).take(n-2) {
        ans += match mid {
            'R' => {
                (g[i] * (b[n]-b[i+1])) + (b[i] * (g[n]-g[i+1])) - rr[i-1] / 2
            }
            'G' => {
                (r[i] * (b[n]-b[i+1])) + (b[i] * (r[n]-r[i+1])) - gg[i-1] / 2
            }
            'B' => {
                (g[i] * (r[n]-r[i+1])) + (r[i] * (g[n]-g[i+1])) - bb[i-1] / 2
            }
            _ => { unreachable!() }
        };
    }


    println!("{}", ans);
}