macro_rules !read_value {($iter :expr ,($($t :tt ) ,*) ) =>{($(read_value !($iter ,$t ) ) ,*) } ;($iter :expr ,[$t :tt ;$len :expr ] ) =>{(0 ..$len ) .map (|_ |read_value !($iter ,$t ) ) .collect ::<Vec <_ >>() } ;($iter :expr ,{chars :$base :expr } ) =>{read_value !($iter ,String ) .chars () .map (|c |(c as u8 -$base as u8 ) as usize ) .collect ::<Vec <usize >>() } ;($iter :expr ,{char :$base :expr } ) =>{read_value !($iter ,{chars :$base } ) [0 ] } ;($iter :expr ,chars ) =>{read_value !($iter ,String ) .chars () .collect ::<Vec <char >>() } ;($iter :expr ,char ) =>{read_value !($iter ,chars ) [0 ] } ;($iter :expr ,usize1 ) =>{read_value !($iter ,usize ) -1 } ;($iter :expr ,$t :ty ) =>{$iter .next () .unwrap () .parse ::<$t >() .unwrap () } ;}
macro_rules !input_inner {($iter :expr ) =>{} ;($iter :expr ,) =>{} ;($iter :expr ,mut $var :ident :$t :tt $($r :tt ) *) =>{let mut $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,mut $var :ident $($r :tt ) *) =>{input_inner !{$iter ,mut $var :usize $($r ) *} } ;($iter :expr ,$var :ident :$t :tt $($r :tt ) *) =>{let $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,$var :ident $($r :tt ) *) =>{input_inner !{$iter ,$var :usize $($r ) *} } ;}
macro_rules !input {(source =$s :expr ,$($r :tt ) *) =>{let mut iter =$s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;(iter =$iter :ident ,$($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut $iter =s .split_whitespace () ;input_inner !{$iter ,$($r ) *} } ;($($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut iter =s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;}
fn main() {
    input! { n, s: chars };
    let mut r = vec![];
    let mut g = vec![];
    let mut b = std::collections::HashSet::new();
    for i in 0..n {
        if s[i] == 'R' {
            r.push(i);
        }
        if s[i] == 'G' {
            g.push(i);
        }
        if s[i] == 'B' {
            b.insert(i);
        }
    }
    let mut ans = r.len() * g.len() * b.len();
    for i in 0..r.len() {
        for j in 0..g.len() {
            let d = if r[i] > g[j] {
                r[i] - g[j]
            } else {
                g[j] - r[i]
            };
            if r[i] < g[j] {
                if b.contains(&(g[j] + d)) {
                    ans -= 1;
                }
                if r[i] >= d && b.contains(&(r[i] - d)) {
                    ans -= 1;
                }
                if d % 2 == 0 && b.contains(&(r[i] + d / 2)) {
                    ans -= 1;
                }
            } else if g[j] < r[i] {
                if b.contains(&(r[i] + d)) {
                    ans -= 1;
                }
                if g[j] >= d && b.contains(&(g[j] - d)) {
                    ans -= 1;
                }
                if d % 2 == 0 && b.contains(&(g[j] + d / 2)) {
                    ans -= 1;
                }
            } else {
                if b.contains(&r[i]) {
                    ans -= 1;
                }
            }
        }
    }

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


Measured memory_bytes: 2180