#include <bits/stdc++.h>
using namespace std;
#define int long long
using pii = pair<int, int>;
vector<int> treating;
int n;
map<int, int> situations;
int pow(int a, int b){
    if (b==0)return 1LL;
    return a*pow(a, b-1);
}
int superhash(vector<int>& treating, int lc){
    int meganumber = pow(2, 56)-1;
    int supernumber = 1;
    int res=0;
    res+=(lc*supernumber)%meganumber;
        supernumber=(6*supernumber)%meganumber;
    for(auto thing:treating){
        res+=(thing*supernumber)%meganumber;
        supernumber=(6*supernumber)%meganumber;
    }
    return res;
}
int dpf(vector<int>& treating, int lc){
    int megahash=superhash(treating, lc);
    if(situations[megahash]!=0)return situations[megahash]-1;
    bitset<3> hs;
    int res = 1000000000;
    int ri=0;
    vector<int> indexe(3);
    int nbt=0;
    for(auto thing:treating){
        if(thing>=3){
            nbt++;
        }
    }
    int tnt=0;
    if(nbt==n)return 0;
    for(int i = 0;i<n;i++){
        int thing = treating[i];
        if (thing<3 && !hs[thing] && thing!=lc){
            hs[thing]=1;
            treating[i]+=3;
            res=min(res, dpf(treating, thing)+tnt);
            nbt++;
            treating[i]-=3;
        }
        if(thing<3)tnt++;
    }
    situations[megahash]=res+1;
    return res;
}
//ok no I put in treating a big 4 to say it has been treated and order kepts
signed main(){
    cin>>n;
    for(int i = 0;i<n;i++){
        char x;cin>>x;
        if(x=='R'){
            treating.push_back(0);
        }
        if(x=='G'){
            treating.push_back(1);
        }
        if(x=='Y'){
            treating.push_back(2);
        }
    }
    int x = dpf(treating, -1);
    cout << (x==1000000000? -1 : x);
}
| # | 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... |