#include<bits/stdc++.h>
using namespace std;
#define sz(a) int((a).size())
#define int long long
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false) , cin.tie(NULL);
#define pii pair< int , int >
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define bitcnt(n) __builtin_popcountll(n)
#define tr(c) for(auto x : c )cout << x << " ";
#define ol(c , s , e ) for(int pos=s; pos<e ; pos++)cout << c[pos] << " ";
#define PI acos(-1)
const int M = 1000000007;
const int N=3e5 + 7;

#ifdef LOCAL
template<typename... T >
void bug(T... __VA_ARGS__) {
    ((cerr << __VA_ARGS__ << " ") , ...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", bug(__VA_ARGS__)
#endif
signed main()
{
    int tc , i ,j;
    int n;
    cin >> n;
    string s;
    cin >> s;
//    if(n <=3){
//        cout << 0 << endl;
//    }
    int r[4001]={}, g[4001]={} , b[4001]={};
    for(i=0 ;i<n ;i++){
        if(s[i]=='R'){
            r[i] = 1;
        }
        else if(s[i]=='G'){
            g[i] = 1;
        }
        else{
            b[i] = 1;
        }
        if(i!=0){
            r[i] +=r[i-1];
            b[i] +=b[i-1];
            g[i] +=g[i-1];
        }
    }
    int ans = 0;
    for(i=0 ; i<n; i++)
    {
        for(int k=i+1 ; k<n ; k++)
        {
                if(k - i > 1 && s[i]!=s[k]){
                    bool red = false , blue = false , green = false;
                    if(s[i]=='G') green = true;
                    else if(s[i]=='R') red = true;
                    else blue = true;

                    if(s[k]=='G')green = true;
                    else if(s[k]=='R') red = true;
                    else blue = true;

                    char color = '#';
                    if(!blue) color = 'B';
                    else if(!green) color = 'G';
                    else color = 'R';

                    if(color == 'R'){
                        bool risk = false;
                        if((k - i)%2==0){
                            risk = true;
                        }
                        int sum = r[k];
                            sum -=r[i];
                        if(risk){
                            if(s[(i+k)/2]==color){
                                sum--;
                            }
                        }
                        ans+=sum;
                    }
                    else if(color =='G'){
                        bool risk = false;
                        if((k - i)%2==0){
                            risk = true;
                        }
                        int sum = g[k];
                            sum -=g[i];
                        if(risk){
                            if(s[(i+k)/2]==color){
                                sum--;
                            }
                        }
                        ans+=sum;
                    }
                    else{
                        bool risk = false;
                        if((k - i)%2==0){
                            risk = true;
                        }
                        int sum = b[k];
                            sum -=b[i];
                        if(risk){
                            if(s[(i+k)/2]==color){
                                sum--;
                            }
                        }
                        ans+=sum;
                    }
                }
        }
    }
    cout <<ans << endl;
}

Measured memory_bytes: 3736