Problem p02714 (C++)

Buggy submission (status: Runtime Error):
/***
        ***Fresher***
    Md Mahfujur Rahman Khan
    Dept. of CSE
    CSE-28 Batch
    JU-48 Batch
    Jahangirnagar University

***/
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define V_ vector
#define Q_ queue
#define S_ set
#define ST_ stack
#define M_ map
#define DQ_ deque
/***PAIR***/
#define pii pair<ll, ll>
#define pci pair<char, ll>
#define psi pair<string, ll>
#define pis pair<ll, string>
#define pic pair<ll, char>
/***MAP***/
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll, string>
#define mib map<ll, bool>
#define msb map<string, bool>
#define mci map<char, ll>
/***VECTOR***/
#define vi vector<ll>
#define vs vector<string>
/***ITERASTORS***/
#define ivi vector<ll>::iterator
#define ivs vector<string>::iterator
#define imii map<ll, ll>::iterator
#define imsi map<string, ll>::iterator
#define imis map<ll, string>::iterator
#define imib map<ll, bool>::iterator
#define imsb map<string, bool>::iterator
#define imci map<char, ll>::iterator
/*** FAST ***/
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
/***FUNCTIONS AND MACROS***/
#define F first
#define S second
#define M_P(x, y) make_pair(x, y)
#define pi 2 * acos(0.0)
#define PB(x) push_back(x)
#define PP(x) push(x)
#define RSZ(n) resize(n)
#define POB pop_back()
#define P_ pop()
#define CL_ clear()
#define LEN_ length()
#define SZ_ size()
#define CNT(x) count(x)
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define B_ begin()
#define E_ end()
#define F_ front()
#define t_ top()
#define IN_ insert
#define ER_ erase
#define GT(x) getline(cin, x)
#define NL_ "\n"
#define SP_ " "
#define R_0 return 0
#define R_ return

#define ul unsigned long long int
#define ll long long int
/***
//prime fact
#define most_prime_loop 1001
#define most_prime_number 500000*2+1
#define prime_MAX_arr 500010
ll PRIME[prime_MAX_arr];
void seive()
{
    for(auto &x: PRIME )
        x = 1;
    for(ll i= 3; i <= most_prime_loop; i+=2)
    {
        for(ll j = i*i; j <= most_prime_number; j+=2*i)
            PRIME[j/2] = 0;
    }
}
//
***/
#define fr(i, n) for (ll i = 0; i < n; i++)
#define fe(x, n) for (ll i = x; i <= n; i++)
#define fa(a) for (auto &x : a)

#define SR(x) sqrt(x)
#define IC (ll)

const ll inf = 1000000000;
ll i, T, N, tc = 0, t = 0;
ll arr[100100];
ll n, k;
pii temp[100100];
ll pay;
vector<pii> vl;
pii ch(ll i, ll val, ll us) {
  if (temp[i].F != -1) {
    //        cout <<temp[i].F <<"\n\n";
    return temp[i];
  }
  if (val >= pay) {
    //        cout << val << " " << us <<"\n";
    //        vl.PB(M_P(val, us));
    return M_P(val, us);
  }
  if (i == n)
    return M_P(LLONG_MAX, LLONG_MAX);

  pii temp2 = ch(i + 1, val + arr[i], us + 1);
  pii temp1 = ch(i + 1, val, us);
  //    cout << temp1.F <<SP_ << temp2.F <<NL_;
  if (temp1.F < temp2.F) {
    temp[i] = temp1;
  } else
    temp[i] = temp2;

  return temp[i];
}

ll ar[17];

bool ch(ll i, ll j) {
  if (ar[i] > ar[j])
    R_ 0;
  while (i < j - 1) {
    if (ar[i] > ar[i + 1])
      R_ 0;
    i++;
  }
  R_ 1;
}

int main() {
  ll g[402], r[402], b[402];
  fa(g) x = 0;
  fa(r) x = 0;
  fa(b) x = 0;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll rr = 0, bb = 0, gg = 0;
  //    cout <<"fgjg";
  for (ul i = s.SZ_; i > 0; i--) {

    if (s[i - 1] == 'G') {
      gg++;
    } else if (s[i - 1] == 'R') {
      rr++;
    } else
      bb++;
    g[i - 1] = gg;
    r[i - 1] = rr;
    b[i - 1] = bb;
  }
  //    for(ll i= 0; i < s.SZ_; i++)
  //    {
  //        cout << g[i] <<" " << r[i] <<" "  << b[i] <<"\n";
  //    }

  ll sum = 0;
  for (ll i = 0; i < n - 2; i++) {
    for (ll j = i + 1; j < n - 1; j++) {
      if ((s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R')) {

        sum += b[j + 1];
        ll k = j - i + j;
        if (k < n && s[k] == 'B')
          sum--;
      } else if ((s[i] == 'B' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'B')) {
        sum += r[j + 1];
        ll k = j - i + j;
        if (k < n && s[k] == 'R')
          sum--;
      } else if ((s[i] == 'B' && s[j] == 'R') || (s[i] == 'R' && s[j] == 'B')) {
        sum += g[j + 1];
        ll k = j - i + j;
        if (k < n && s[k] == 'G')
          sum--;
      }
    }
  }

  cout << sum;

  R_0;
}


Fixed submission:
/***
        ***Fresher***
    Md Mahfujur Rahman Khan
    Dept. of CSE
    CSE-28 Batch
    JU-48 Batch
    Jahangirnagar University

***/
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define V_ vector
#define Q_ queue
#define S_ set
#define ST_ stack
#define M_ map
#define DQ_ deque
/***PAIR***/
#define pii pair<ll, ll>
#define pci pair<char, ll>
#define psi pair<string, ll>
#define pis pair<ll, string>
#define pic pair<ll, char>
/***MAP***/
#define mii map<ll, ll>
#define msi map<string, ll>
#define mis map<ll, string>
#define mib map<ll, bool>
#define msb map<string, bool>
#define mci map<char, ll>
/***VECTOR***/
#define vi vector<ll>
#define vs vector<string>
/***ITERASTORS***/
#define ivi vector<ll>::iterator
#define ivs vector<string>::iterator
#define imii map<ll, ll>::iterator
#define imsi map<string, ll>::iterator
#define imis map<ll, string>::iterator
#define imib map<ll, bool>::iterator
#define imsb map<string, bool>::iterator
#define imci map<char, ll>::iterator
/*** FAST ***/
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
/***FUNCTIONS AND MACROS***/
#define F first
#define S second
#define M_P(x, y) make_pair(x, y)
#define pi 2 * acos(0.0)
#define PB(x) push_back(x)
#define PP(x) push(x)
#define RSZ(n) resize(n)
#define POB pop_back()
#define P_ pop()
#define CL_ clear()
#define LEN_ length()
#define SZ_ size()
#define CNT(x) count(x)
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define B_ begin()
#define E_ end()
#define F_ front()
#define t_ top()
#define IN_ insert
#define ER_ erase
#define GT(x) getline(cin, x)
#define NL_ "\n"
#define SP_ " "
#define R_0 return 0
#define R_ return

#define ul unsigned long long int
#define ll long long int
/***
//prime fact
#define most_prime_loop 1001
#define most_prime_number 500000*2+1
#define prime_MAX_arr 500010
ll PRIME[prime_MAX_arr];
void seive()
{
    for(auto &x: PRIME )
        x = 1;
    for(ll i= 3; i <= most_prime_loop; i+=2)
    {
        for(ll j = i*i; j <= most_prime_number; j+=2*i)
            PRIME[j/2] = 0;
    }
}
//
***/
#define fr(i, n) for (ll i = 0; i < n; i++)
#define fe(x, n) for (ll i = x; i <= n; i++)
#define fa(a) for (auto &x : a)

#define SR(x) sqrt(x)
#define IC (ll)

const ll inf = 1000000000;
ll i, T, N, tc = 0, t = 0;
ll arr[100100];
ll n, k;
pii temp[100100];
ll pay;
vector<pii> vl;
pii ch(ll i, ll val, ll us) {
  if (temp[i].F != -1) {
    //        cout <<temp[i].F <<"\n\n";
    return temp[i];
  }
  if (val >= pay) {
    //        cout << val << " " << us <<"\n";
    //        vl.PB(M_P(val, us));
    return M_P(val, us);
  }
  if (i == n)
    return M_P(LLONG_MAX, LLONG_MAX);

  pii temp2 = ch(i + 1, val + arr[i], us + 1);
  pii temp1 = ch(i + 1, val, us);
  //    cout << temp1.F <<SP_ << temp2.F <<NL_;
  if (temp1.F < temp2.F) {
    temp[i] = temp1;
  } else
    temp[i] = temp2;

  return temp[i];
}

ll ar[17];

bool ch(ll i, ll j) {
  if (ar[i] > ar[j])
    R_ 0;
  while (i < j - 1) {
    if (ar[i] > ar[i + 1])
      R_ 0;
    i++;
  }
  R_ 1;
}

int main() {
  ll g[4002], r[4002], b[4002];
  fa(g) x = 0;
  fa(r) x = 0;
  fa(b) x = 0;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll rr = 0, bb = 0, gg = 0;
  //    cout <<"fgjg";
  for (ul i = s.SZ_; i > 0; i--) {

    if (s[i - 1] == 'G') {
      gg++;
    } else if (s[i - 1] == 'R') {
      rr++;
    } else
      bb++;
    g[i - 1] = gg;
    r[i - 1] = rr;
    b[i - 1] = bb;
  }
  //    for(ll i= 0; i < s.SZ_; i++)
  //    {
  //        cout << g[i] <<" " << r[i] <<" "  << b[i] <<"\n";
  //    }

  ll sum = 0;
  for (ll i = 0; i < n - 2; i++) {
    for (ll j = i + 1; j < n - 1; j++) {
      if ((s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R')) {

        sum += b[j + 1];
        ll k = j - i + j;
        if (k < n && s[k] == 'B')
          sum--;
      } else if ((s[i] == 'B' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'B')) {
        sum += r[j + 1];
        ll k = j - i + j;
        if (k < n && s[k] == 'R')
          sum--;
      } else if ((s[i] == 'B' && s[j] == 'R') || (s[i] == 'R' && s[j] == 'B')) {
        sum += g[j + 1];
        ll k = j - i + j;
        if (k < n && s[k] == 'G')
          sum--;
      }
    }
  }

  cout << sum;

  R_0;
}