This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <memory.h>
#include <cmath>
#include <array>
using namespace std;
void re(int& x);
void re(char* c);
void pr(int x);
void pr(const char *x);
void ps();
template<class T, class... Ts> void ps(const T& t, const Ts&... ts);
#ifdef FFDBG
#else
#define dbg(x...) dsfdsfsdfasd
#endif
void re(int& x) { scanf("%d", &x); }
void re(char* c) { scanf("%s", c); }
void pr(int x) { printf("%d", x); }
void pr(const char *x) { printf("%s", x); }
void ps() { pr("\n"); }
template<class T, class... Ts> void ps(const T& t, const Ts&... ts) { 
    pr(t); if (sizeof...(ts)) pr(" "); ps(ts...); 
}
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for(int i = a; i < (b); ++i)
int n;
char s[500];
vector<int> locs[3];
int dp[410][410][410][3];
int sn[410][410][410][3];
int rec(int l1, int l2, int l3, int p) {
    //dbg(l1,l2,l3,p);
    if (l1+l2+l3==n) return 0;
    int l[3] = {l1,l2,l3};
    int &ret = dp[l1][l2][l3][p];
    if (!sn[l1][l2][l3][p]) {
        sn[l1][l2][l3][p]=1;
        ret = 1000000000;
        for (int nxt = 0; nxt < 3; nxt++) if ((l1+l2+l3 ==0||nxt != p) && l[nxt] != locs[nxt].size()) {
            int this_pos = locs[nxt][l[nxt]];
            int hm = 0;
            for (int ot = 0; ot < 3; ot++) {
                hm += max(0, (int)(lower_bound(all(locs[ot]), this_pos) - locs[ot].begin()) - l[ot]);
            }
            l[nxt]++;
            //dbg(l1,l2,l3,p,nxt,hm);
            ret = min(ret, hm + rec(l[0],l[1],l[2],nxt));
            l[nxt]--;
        }
    }
    return ret;
}
void solve()
{
    re(n);
    re(s);
    rep(i,0,n) {
        if (s[i]=='R') locs[0].push_back(i);
        else if (s[i]=='G') locs[1].push_back(i);
        else locs[2].push_back(i);
    }
    int ans = rec(0,0,0,0);
    ps(ans == 1000000000 ? -1 : ans);
}
int main() {
    solve();
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int rec(int, int, int, int)':
joi2019_ho_t3.cpp:66:82: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int nxt = 0; nxt < 3; nxt++) if ((l1+l2+l3 ==0||nxt != p) && l[nxt] != locs[nxt].size()) {
                                                                           ~~~~~~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'void re(int&)':
joi2019_ho_t3.cpp:32:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void re(int& x) { scanf("%d", &x); }
                   ~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp: In function 'void re(char*)':
joi2019_ho_t3.cpp:34:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void re(char* c) { scanf("%s", c); }
                    ~~~~~^~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |