Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
#define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#define LOG(x) 0
#endif
#define pb push_back
#define mod 1000000007
#define endl '\n'
typedef int integer;
#define int long long
using namespace std;

int powmod(int a, int b) {
  int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

const integer NN = 200005;
int ar[NN];
int n, m, k;
int sum[1000][3];

integer main() {
  ios_base::sync_with_stdio(false);

  int t = 1;
  //    cin >> t;
  while (t--) {
    cin >> n;
    string s;
    cin >> s;
    int g = 0;
    int b = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'R') {
        sum[i][0] = 1;
        s[i] = 0;
      } else if (s[i] == 'B') {
        sum[i][1] = 1;
        s[i] = 1;
      } else {
        sum[i][2] = 1;
        s[i] = 2;
      }
      if (i) {
        sum[i][0] += sum[i - 1][0];
        sum[i][1] += sum[i - 1][1];
        sum[i][2] += sum[i - 1][2];
      }
    }
    int ans = 0;
    for (int i = 1; i < n - 1; i++) {
      if (s[i] == 2) {
        ans += sum[i][0] * (sum[n - 1][1] - sum[i][1]);
        ans += sum[i][1] * (sum[n - 1][0] - sum[i][0]);
      } else if (s[i] == 1) {
        ans += sum[i][0] * (sum[n - 1][2] - sum[i][2]);
        ans += sum[i][2] * (sum[n - 1][0] - sum[i][0]);
      } else {
        ans += sum[i][2] * (sum[n - 1][1] - sum[i][1]);
        ans += sum[i][1] * (sum[n - 1][2] - sum[i][2]);
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] != 0)
        continue;
      int st = i - 1, en = i + 1;
      while (st >= 0 && en < n) {
        if (s[st] == 1 && s[en] == 2)
          ans--;
        else if (s[st] == 2 && s[en] == 1)
          ans--;
        st--;
        en++;
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] != 1)
        continue;
      int st = i - 1, en = i + 1;
      while (st >= 0 && en < n) {
        if (s[st] == 0 && s[en] == 2)
          ans--;
        else if (s[st] == 2 && s[en] == 0)
          ans--;
        st--;
        en++;
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] != 2)
        continue;
      int st = i - 1, en = i + 1;
      while (st >= 0 && en < n) {
        if (s[st] == 1 && s[en] == 0)
          ans--;
        else if (s[st] == 0 && s[en] == 1)
          ans--;
        st--;
        en++;
      }
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
#define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#define LOG(x) 0
#endif
#define pb push_back
#define mod 1000000007
#define endl '\n'
typedef int integer;
#define int long long
using namespace std;

int powmod(int a, int b) {
  int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

const integer NN = 200005;
int ar[NN];
int n, m, k;
int sum[5000][3];

integer main() {
  ios_base::sync_with_stdio(false);

  int t = 1;
  //    cin >> t;
  while (t--) {
    cin >> n;
    string s;
    cin >> s;
    int g = 0;
    int b = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'R') {
        sum[i][0] = 1;
        s[i] = 0;
      } else if (s[i] == 'B') {
        sum[i][1] = 1;
        s[i] = 1;
      } else {
        sum[i][2] = 1;
        s[i] = 2;
      }
      if (i) {
        sum[i][0] += sum[i - 1][0];
        sum[i][1] += sum[i - 1][1];
        sum[i][2] += sum[i - 1][2];
      }
    }
    int ans = 0;
    for (int i = 1; i < n - 1; i++) {
      if (s[i] == 2) {
        ans += sum[i][0] * (sum[n - 1][1] - sum[i][1]);
        ans += sum[i][1] * (sum[n - 1][0] - sum[i][0]);
      } else if (s[i] == 1) {
        ans += sum[i][0] * (sum[n - 1][2] - sum[i][2]);
        ans += sum[i][2] * (sum[n - 1][0] - sum[i][0]);
      } else {
        ans += sum[i][2] * (sum[n - 1][1] - sum[i][1]);
        ans += sum[i][1] * (sum[n - 1][2] - sum[i][2]);
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] != 0)
        continue;
      int st = i - 1, en = i + 1;
      while (st >= 0 && en < n) {
        if (s[st] == 1 && s[en] == 2)
          ans--;
        else if (s[st] == 2 && s[en] == 1)
          ans--;
        st--;
        en++;
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] != 1)
        continue;
      int st = i - 1, en = i + 1;
      while (st >= 0 && en < n) {
        if (s[st] == 0 && s[en] == 2)
          ans--;
        else if (s[st] == 2 && s[en] == 0)
          ans--;
        st--;
        en++;
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] != 2)
        continue;
      int st = i - 1, en = i + 1;
      while (st >= 0 && en < n) {
        if (s[st] == 1 && s[en] == 0)
          ans--;
        else if (s[st] == 0 && s[en] == 1)
          ans--;
        st--;
        en++;
      }
    }
    cout << ans << endl;
  }
}