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: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use competitive::prelude::*;

fn main() {
    input! {
        n: usize,
        s: Chars,
    }
    let acc_char = |ch: char| -> Vec<usize> {
        s.iter().map(|&c| if c == ch { 1 } else { 0 }).scan(0, |acc, x| {
            *acc = *acc + x;
            Some(*acc)
        }).collect()
    };
    let acc_r = acc_char('R');
    let acc_g = acc_char('G');
    let acc_b = acc_char('B');
    let rgb = vec!['R', 'G', 'B'];
    let acc_rgb = vec![acc_r, acc_g, acc_b];
    let mut ans = 0;
//    println!("{:?}", acc_rgb);
    for i in 0..n {
        for j in i + 1..n {
            if (i >= n) | (j >= n) | (s[i] == s[j]) { continue; }
            for m in 0..=2 {
                let ch = rgb[m];
                if (s[i] != ch) & (s[j] != ch) {
                    // j + 1 ~ n - 1のchの個数
                    ans += acc_rgb[m][n - 1] - acc_rgb[m][j];
                    if (2 * j - i < n) && (s[2 * j - i] == ch) { ans -= 1; }
                }
            }
        }
    }
    println!("{}", ans);
}
*/

fn main() {
    let exe = "/tmp/binA0EFAA60";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";