#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
const ll mod = 1e9 + 7;
const ll INF = 1e9;
//--------------------------------------------------------------------
ll n;
string T = "RGY";
string s;
const int N = 402;
int f[N][N][N][3];
vector<ll> Pos[4];
ll node = 0;
ll sum[4][N];
ll dp(ll i, ll j, ll k, ll last) {
// if(mark[i][j][k] == 0) {
// mark[i][j][k] = ++node;
// }
if(f[i][j][k][last] != -1) return f[i][j][k][last];
ll pos = i + j + k;
if(pos == n) return 0;
ll res = INF;
int id[3];
id[0] = i;
id[1] = j;
id[2] = k;
FOR(c, 0, 2) {
ll sz = Pos[c].size() - 1;
if(id[c] < sz && c != last) {
ll idx = Pos[c][id[c] + 1];
ll cur = 0;
FOR(non_c, 0, 2) {
if(non_c != c) {
cur += max(0LL, sum[non_c][idx] - id[non_c]);
}
}
res = min(res, dp(i + (c == 0), j + (c == 1), k + (c == 2), c) + cur);
}
}
return f[i][j][k][last] = res;
}
void hbmt() {
cin >> n;
cin >> s;
n = s.size();
s = ' ' + s;
FOR(c, 0, 2) Pos[c].push_back(0);
FOR(i, 1, n) {
ll c = T.find(s[i]);
Pos[c].push_back(i);
FOR(j, 0, 2) {
sum[j][i] = sum[j][i - 1];
}
sum[c][i] += 1;
}
memset(f, -1, sizeof f);
ll val = dp(0, 0, 0, 3);
if(val >= INF) cout << -1;
else cout << val;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
#define NAME "hbmt"
if(fopen(NAME".inp", "r")) {
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
//int t;cin>>t;while(t--)
hbmt();
return 0;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:75:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:76:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(NAME".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |