Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using namespace std;
// LLONG_MAX = 90^18
#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No"); }
void Yes() { cout << "Yes"; }
void No() { cout << "No"; }

int N;
char S[4010];

int r_cnt[4010];
int g_cnt[4010];
int b_cnt[4010];

signed main() {
  cin >> N;
  cin >> S;

  int r = 0;
  int g = 0;
  int b = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == 'R')
      r++;
    if (S[i] == 'G')
      g++;
    if (S[i] == 'B')
      b++;
    r_cnt[i] = r;
    g_cnt[i] = g;
    b_cnt[i] = b;
  }

  int sum = 0;

  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      int sub = j - i;
      int k = j + sub;
      if ((S[i] == 'R' && S[j] == 'G') || (S[i] == 'G' && S[j] == 'R')) {
        if (S[k] == 'B') {
          sum--;
        }
        sum += b_cnt[j + 1];
      }
      if ((S[i] == 'R' && S[j] == 'B') || (S[i] == 'B' && S[j] == 'R')) {
        if (S[k] == 'G') {
          sum--;
        }
        sum += g_cnt[j + 1];
      }
      if ((S[i] == 'B' && S[j] == 'G') || (S[i] == 'G' && S[j] == 'B')) {
        if (S[k] == 'R') {
          sum--;
        }
        sum += r_cnt[j + 1];
      }
    }
  }

  cout << sum << endl;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using namespace std;
// LLONG_MAX = 90^18
#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No"); }
void Yes() { cout << "Yes"; }
void No() { cout << "No"; }

int N;
char S[4010];

int r_cnt[4010];
int g_cnt[4010];
int b_cnt[4010];

signed main() {
  cin >> N;
  cin >> S;

  int r = 0;
  int g = 0;
  int b = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (S[i] == 'R')
      r++;
    if (S[i] == 'G')
      g++;
    if (S[i] == 'B')
      b++;
    r_cnt[i] = r;
    g_cnt[i] = g;
    b_cnt[i] = b;
  }

  int sum = 0;

  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      int sub = j - i;
      int k = j + sub;
      if (k >= N)
        k = 4000;
      if ((S[i] == 'R' && S[j] == 'G') || (S[i] == 'G' && S[j] == 'R')) {
        if (S[k] == 'B') {
          sum--;
        }
        sum += b_cnt[j + 1];
      }
      if ((S[i] == 'R' && S[j] == 'B') || (S[i] == 'B' && S[j] == 'R')) {
        if (S[k] == 'G') {
          sum--;
        }
        sum += g_cnt[j + 1];
      }
      if ((S[i] == 'B' && S[j] == 'G') || (S[i] == 'G' && S[j] == 'B')) {
        if (S[k] == 'R') {
          sum--;
        }
        sum += r_cnt[j + 1];
      }
    }
  }

  cout << sum << endl;
}