#include <bits/stdc++.h>
#define rep(a,b,c) for(auto a = (b); a != (c); a++)
#define repD(a,b,c) for(auto a = (b); a != (c); a--)
#define repIn(a, b) for(auto& a : (b))
#define repIn2(a, b, c) for(auto& [a, b] : (c))
constexpr bool dbg = 0;
#define DEBUG if constexpr(dbg)
#define DC DEBUG std::cerr
#define eol std::endl
#define ll long long
#define pb push_back
using namespace std;
constexpr int inf = 1e9 + 7;
map<char, int> xd = { {'R', 0}, {'G', 1}, {'Y', 2} };
int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int n;
    cin >> n;
    string s;
    cin >> s;
    vector<vector<vector<vector<int>>>> dp(2, vector<vector<vector<int>>>(n + 1, vector<vector<int>>(n + 1, vector<int>(3))));
    vector<vector<pair<int, int>>> where(3);
    rep(i, 0, n) where[xd[s[i]]].pb({i, where[(xd[s[i]] ? 0 : 1)].size()});
    rep(i, 0, n + 1) {
        rep(c1, 0, (int)where[0].size() + 1) rep(c2, 0, (int)where[1].size() + 1) rep(last, 0, 3) dp[i & 1][c1][c2][last] = inf;
        rep(c1, 0, min(i + 1, (int)where[0].size() + 1)) rep(c2, 0, min(i - c1 + 1, (int)where[1].size() + 1)) {
            auto c3 = i - c1 - c2;
            if(c3 < 0 || c3 > where[2].size()) continue;
            DC << i << ' ' << c1 << ' ' << c2 << "   ";
            rep(last, 0, 3) {
                auto me = dp[1 ^ (i & 1)][c1][c2][last];
                if(me == inf) { DC << "oo "; }
                else DC << me << ' ';
                if(i == 3 && me == 1) {
                    DC << "";
                }
                rep(last2, 0, 3) {
                    if(i == n || last == last2) continue;
                    if(last2 == 2 && c3 < (int)where[2].size()) {
                        auto [ogPos, b1] = where[2][c3];
                        b1 = min(b1, c1);
                        int b3 = c3 + (i == ogPos);
                        int b2 = min(ogPos - b1 - b3, c2);
                        dp[i & 1][c1][c2][last2] = min(dp[i & 1][c1][c2][last2], me + max(ogPos - b1 - b2 - b3, 0));
                    }
                    if(last2 == 1 && c2 < (int)where[1].size()) {
                        auto [ogPos, b1] = where[1][c2];
                        b1 = min(b1, c1);
                        int b2 = c2 + (i == ogPos);
                        int b3 = min(ogPos - b1 - b2, c3);
                        dp[i & 1][c1][c2 + 1][last2] = min(dp[i & 1][c1][c2 + 1][last2], me + max(ogPos - b1 - b2 - b3, 0)); 
                    }
                    if(last2 == 0 && c1 < (int)where[0].size()) {
                        auto [ogPos, b2] = where[0][c1];
                        b2 = min(b2, c2);
                        int b1 = c1 + (i == ogPos);
                        int b3 = min(ogPos - b1 - b2, c3);
                        dp[i & 1][c1 + 1][c2][last2] = min(dp[i & 1][c1 + 1][c2][last2], me + max(ogPos - b1 - b2 - b3, 0));
                    }
                }
            }
            DC << eol;
        }
    }
    int ans = inf;
    rep(last, 0, 3) ans = min(ans, dp[(1 ^ n) & 1][where[0].size()][where[1].size()][last]);
    if(ans == inf) ans = -1;
    cout << ans << '\n';
}
| # | 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... |