Problem p02714 (C++)

Buggy submission (status: Runtime Error):
/*
ID: wangjun30
LANG: C++11
TASK:
*/
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include<fstream>
#define int long long
using namespace std;
// ifstream cin(".in");
// ofstream cout(".out");
int Rf[4010];
int Gf[4010];
int Bf[4010];
int Rb[4010];
int Gb[4010];
int Bb[4010];
char ch[4010];
int ans = 0;
int Subans = 0;
int n;
int init() {
  cin >> n;
  int R = 0, G = 0, B = 0;
  for (int i = 1; i <= n; i++) {
    cin >> ch[i];
    Rf[i] = R;
    Gf[i] = G;
    Bf[i] = B;
    switch (ch[i]) {
    case 'R': {
      R++;
      break;
    }
    case 'G': {
      G++;
      break;
    }
    case 'B': {
      B++;
      break;
    }
    }
  }
  R = 0;
  G = 0;
  B = 0;
  for (int i = n; i >= 1; i--) {
    Rb[i] = R;
    Gb[i] = G;
    Bb[i] = B;
    switch (ch[i]) {
    case 'R': {
      R++;
      break;
    }
    case 'G': {
      G++;
      break;
    }
    case 'B': {
      B++;
      break;
    }
    }
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  init();
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      int Length = j - i;
      int PlaceA = i - Length;
      int PlaceB = i;
      int PlaceC = i + Length;
      if (PlaceC <= n && PlaceA >= 1 && ch[PlaceA] != ch[PlaceB] &&
          ch[PlaceA] != ch[PlaceC] && ch[PlaceB] != ch[PlaceC])
        Subans++;
    }
  }
  for (int i = 1; i <= n; i++) {
    switch (ch[i]) {
    case 'R': {
      ans += Gf[i] * Bb[i] + Gb[i] * Bf[i];
      break;
    }
    case 'G': {
      ans += Rf[i] * Bb[i] + Rb[i] * Bf[i];
      break;
    }
    case 'B': {
      ans += Rf[i] * Gb[i] + Rb[i] * Gf[i];
      break;
    }
    }
  }
  cout << ans - Subans;
  return 0;
}

Fixed submission:
/*
ID: wangjun30
LANG: C++11
TASK:
*/
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include<fstream>
#define int long long
using namespace std;
// ifstream cin(".in");
// ofstream cout(".out");
int Rf[4010];
int Gf[4010];
int Bf[4010];
int Rb[4010];
int Gb[4010];
int Bb[4010];
char ch[4010];
int ans = 0;
int Subans = 0;
int n;
void init() {
  cin >> n;
  int R = 0, G = 0, B = 0;
  for (int i = 1; i <= n; i++) {
    cin >> ch[i];
    Rf[i] = R;
    Gf[i] = G;
    Bf[i] = B;
    switch (ch[i]) {
    case 'R': {
      R++;
      break;
    }
    case 'G': {
      G++;
      break;
    }
    case 'B': {
      B++;
      break;
    }
    }
  }
  R = 0;
  G = 0;
  B = 0;
  for (int i = n; i >= 1; i--) {
    Rb[i] = R;
    Gb[i] = G;
    Bb[i] = B;
    switch (ch[i]) {
    case 'R': {
      R++;
      break;
    }
    case 'G': {
      G++;
      break;
    }
    case 'B': {
      B++;
      break;
    }
    }
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  init();
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      int Length = j - i;
      int PlaceA = i - Length;
      int PlaceB = i;
      int PlaceC = i + Length;
      if (PlaceC <= n && PlaceA >= 1 && ch[PlaceA] != ch[PlaceB] &&
          ch[PlaceA] != ch[PlaceC] && ch[PlaceB] != ch[PlaceC])
        Subans++;
    }
  }
  for (int i = 1; i <= n; i++) {
    switch (ch[i]) {
    case 'R': {
      ans += Gf[i] * Bb[i] + Gb[i] * Bf[i];
      break;
    }
    case 'G': {
      ans += Rf[i] * Bb[i] + Rb[i] * Bf[i];
      break;
    }
    case 'B': {
      ans += Rf[i] * Gb[i] + Rb[i] * Gf[i];
      break;
    }
    }
  }
  cout << ans - Subans;
  return 0;
}