//For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstring>
    #include <chrono>
    #include <complex>
    #define ll long long
    #define ld long double
    #define vi vector<int>
    #define vll vector<ll>
    #define vvi vector < vi >
    #define pii pair<int,int>
    #define pll pair<long long, long long>
    #define mod 1000000007
    #define inf 1000000000000000001;
    #define all(c) c.begin(),c.end()
    #define mp(x,y) make_pair(x,y)
    #define mem(a,val) memset(a,val,sizeof(a))
    #define eb emplace_back
    #define pb push_back
    #define f first
    #define s second
    
    using namespace std;

    map<int,int> mp,mp2,mp3;
    vector<int> v,v2,v3;

    int main()
    {
        std::ios::sync_with_stdio(false);
        int T;
        T=1;
        // cin.ignore(); must be there when using getline(cin, s)
        while(T--)
        {
            string s;
            int n,i,j;
            cin>>n;
            cin>>s;
            int r=0,g=0,b=0;
            for(i=0;i<n;++i)
            {
                if(s[i]=='R')
                {
                    mp[i]=1;
                    ++r;
                    v.pb(i);
                }
                else if(s[i]=='G')
                {
                    mp2[i]=1;
                    ++g;
                    v2.pb(i);
                }
                else
                {
                    mp3[i]=1;
                    ++b;
                    v3.pb(i);
                }
            }
            ll ans=0;
            int pos;
            for(i=0;i<n;++i)
            {
                for(j=i+1;j<n;++j)
                {
                    if(s[i]==s[j])
                        continue;

                    if((s[i]=='R' && s[j]=='G') || (s[i]=='G' && s[j]=='R'))
                    {
                        pos=lower_bound(v3.begin(),v3.end(),j)-v3.begin();
                        ans=ans+v3.size()-pos;
                        if(mp3[2*j-i]==1)
                        {
                            --ans;
                        }
                    }
                    else if((s[i]=='R' && s[j]=='B') || (s[i]=='B' && s[j]=='R'))
                    {
                        pos=lower_bound(v2.begin(),v2.end(),j)-v2.begin();
                        ans=ans+v2.size()-pos;
                        if(mp2[2*j-i]==1)
                        {
                            --ans;
                        }
                    }
                    else
                    {
                        pos=lower_bound(v.begin(),v.end(),j)-v.begin();
                        ans=ans+v.size()-pos;
                        if(mp[2*j-i]==1)
                        {
                            --ans;
                        }
                    }
                }
                if(s[i]=='R')
                {
                    mp[i]=0;
                    --r;
                }
                else if(s[i]=='G')
                {
                    mp2[i]=0;
                    --g;
                }
                else
                {
                    mp3[i]=0;
                    --b;
                }
            }

            cout<<ans<<endl;
        }
        return 0;
    }


Measured memory_bytes: 4784