Problem p02714 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map<int, int>
#define mll map<ll, ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vd vector<double>
#define vll vector<ll>
#define fi first
#define se second
#define si set<int>
#define sll set<ll>
#define spii set<pii>
#define vs vector<string>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long, long long>>
#define vvi vector<vector<int>>
#define vvpii vector<vector<pii>>
#define vvll vector<vll>
#define vsi vector<si>
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 400 + 100;
const ll INF = 1e9;
const ll mod1 = 1e9 + 7;
const ll mod2 = 2e9 + 11;
int R[MAXN], B[MAXN], G[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("a.in", "r", stdin);
#endif
  int n;
  cin >> n;
  string s;
  cin >> s;
  int r = 0, b = 0, g = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r++;
    if (s[i] == 'B')
      b++;
    if (s[i] == 'G')
      g++;
    R[i + 1] = r;
    B[i + 1] = b;
    G[i + 1] = g;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (s[i] == s[j])
        continue;
      if (s[i] != 'B' && s[j] != 'B') {
        ans += B[n] - B[j];
        if (j + j - i < n && s[2 * j - i] == 'B')
          ans--;
      }
      if (s[i] != 'G' && s[j] != 'G') {
        ans += G[n] - G[j];
        if (j + j - i < n && s[2 * j - i] == 'G')
          ans--;
      }
      if (s[i] != 'R' && s[j] != 'R') {
        ans += R[n] - R[j];
        if (j + j - i < n && s[2 * j - i] == 'R')
          ans--;
      }
    }
  }
  cout << ans;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mii map<int, int>
#define mll map<ll, ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vd vector<double>
#define vll vector<ll>
#define fi first
#define se second
#define si set<int>
#define sll set<ll>
#define spii set<pii>
#define vs vector<string>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long, long long>>
#define vvi vector<vector<int>>
#define vvpii vector<vector<pii>>
#define vvll vector<vll>
#define vsi vector<si>
#define forn(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
const ll INFLL = 1e18;
const int MAXN = 4000 + 100;
const ll INF = 1e9;
const ll mod1 = 1e9 + 7;
const ll mod2 = 2e9 + 11;
int R[MAXN], B[MAXN], G[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("a.in", "r", stdin);
#endif
  int n;
  cin >> n;
  string s;
  cin >> s;
  int r = 0, b = 0, g = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r++;
    if (s[i] == 'B')
      b++;
    if (s[i] == 'G')
      g++;
    R[i + 1] = r;
    B[i + 1] = b;
    G[i + 1] = g;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (s[i] == s[j])
        continue;
      if (s[i] != 'B' && s[j] != 'B') {
        ans += B[n] - B[j];
        if (j + j - i < n && s[2 * j - i] == 'B')
          ans--;
      }
      if (s[i] != 'G' && s[j] != 'G') {
        ans += G[n] - G[j];
        if (j + j - i < n && s[2 * j - i] == 'G')
          ans--;
      }
      if (s[i] != 'R' && s[j] != 'R') {
        ans += R[n] - R[j];
        if (j + j - i < n && s[2 * j - i] == 'R')
          ans--;
      }
    }
  }
  cout << ans;
}