#include<iostream>
#define int long long

using namespace std;

signed main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    int sum = 0;
    int preR[n]={0},preG[n]={0},preB[n]={0};
    if(s[0]=='R'){
        preR[0]=1;
    }
    if(s[0]=='G'){
        preG[0]=1;
    }
    if(s[0]=='B'){
        preB[0]=1;
    }

    for(int i=1;i<n;i++){
        if(s[i]=='R'){
            preR[i] =preR[i-1]+1;
        }
        else{
            preR[i] =preR[i-1];
        }
        if(s[i]=='G'){
            preG[i] =preG[i-1]+1;
        }
        else{
            preG[i] =preG[i-1];
        }
        if(s[i]=='B'){
            preB[i] =preB[i-1]+1;
        }
        else{
            preB[i] =preB[i-1];
        }
    }

    // for(auto i:preR){
    //     cout<<i<<" ";
    // }
    // cout<<"\n";
    // for(auto i:preG){
    //     cout<<i<<" ";
    // }
    // cout<<"\n";    
    // for(auto i:preB){
    //     cout<<i<<" ";
    // }
    // cout<<"\n";
    
    for(int i=0;i<n;i++){
        for(int k=i+1;k<n;k++){
            // cout<<"i = "<<i<<" k = "<<k<<endl;
            if((i+k)%2==0){
                // cout<<1<<"\n";
                if(s[i]!=s[k] && s[(i+k)/2]!=s[i] && s[(i+k)/2]!=s[k]){
                    // cout<<"Inside 1a\n";
                    if(s[(i+k)/2] == 'R'){
                        sum += preR[k] - preR[i] - 1;
                    }
                    else if(s[(i+k)/2] == 'G'){
                        sum += preG[k] - preG[i] - 1;
                    }
                    else if(s[(i+k)/2] == 'B'){
                        sum += preB[k] - preB[i] - 1;
                    }
                    
                }
                else if(s[i]!=s[k] ){
                    // cout<<"Inside 1b\n";
                    if(s[i] == 'B' && s[k] =='G'){
                        sum += preR[k] - preR[i];
                    }
                    else if(s[i] == 'G' && s[k] =='B'){
                        sum += preR[k] - preR[i];
                    }
                    else if(s[i] == 'B' && s[k] =='R'){
                        sum += preG[k] - preG[i];
                    }
                    else if(s[i] == 'R' && s[k] =='B'){
                        sum += preG[k] - preG[i];
                    }
                    else if(s[i] == 'R' && s[k] =='G'){
                        sum += preB[k] - preB[i];
                    }
                    else if(s[i] == 'G' && s[k] =='R'){
                        sum += preB[k] - preB[i];
                    }
                
                }
                // cout<<"Sum at end of case 1 is "<<sum<<"\n";
            }
            else{
                // cout<<2<<"\n";
                if(s[i]!=s[k]){
                    if(s[i] == 'B' && s[k] =='G'){
                        sum += preR[k] - preR[i];
                    }
                    else if(s[i] == 'G' && s[k] =='B'){
                        sum += preR[k] - preR[i];
                    }
                    else if(s[i] == 'B' && s[k] =='R'){
                        sum += preG[k] - preG[i];
                    }
                    else if(s[i] == 'R' && s[k] =='B'){
                        sum += preG[k] - preG[i];
                    }
                    else if(s[i] == 'R' && s[k] =='G'){
                        sum += preB[k] - preB[i];
                    }
                    else if(s[i] == 'G' && s[k] =='R'){
                        sum += preB[k] - preB[i];
                    }
                }
                // cout<<"Sum at end of case 2 is "<<sum<<"\n";
            }
        }
    }
    
    cout<<sum;
}

Measured memory_bytes: 3740