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 <bits/stdc++.h>
//qwerty47924692
using namespace std;
using ll = long long;
#define f first
#define s second
const ll N=3e5+29;
ll cur1,cur2,cnt[3],n,ans=1e18;
vector<ll>pos[3];
string s;
void f(ll x,ll cntr,ll cnty,ll cntg,ll sum,char last){
if(x==n-1){
ans=min(ans,sum);
return;
}
x++;
if(last!='R'&&cntr+1<=cnt[0]){
f(x,cntr+1,cnty,cntg,sum+abs(x-pos[0][cntr]),'R');
}if(last!='Y'&&cnty+1<=cnt[1]){
f(x,cntr,cnty+1,cntg,sum+abs(x-pos[1][cnty]),'Y');
}if(last!='G'&&cntg+1<=cnt[2]){
f(x,cntr,cnty,cntg+1,sum+abs(x-pos[2][cntg]),'G');
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>s;
ll x=0;
for(char i:s){
if(i=='R'){
cnt[0]++;
pos[0].push_back(x++);
}
if(i=='Y'){
cnt[1]++;
pos[1].push_back(x++);
}
if(i=='G'){
cnt[2]++;
pos[2].push_back(x++);
}
}
f(-1,0,0,0,0,'J');
cout<<(ans==1e18 ? -1 : ans/2);
}
# | 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... |