#include<bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define fr first
#define sc second
#define endl '\n'
using namespace std;
int n;
int arr[423];
vector<int>app[3],rel[3][3];
int dp[423][423][423][3];
int main(){
ios_base::sync_with_stdio(23^23);cin.tie(NULL);
cin>>n;
for(int i=1;i<=n;i++){
char c;cin>>c;
if(c=='R')arr[i]=1;
if(c=='Y')arr[i]=2;
app[arr[i]].pb(i);
}
for(int i=1;i<=n;i++){
for(int j=0;j<=n;j++){
for(int l=0;l<=n;l++){
for(int r=0;r<3;r++){
dp[i][j][l][r]=1e9;
}
}
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
for(int l=0;l<app[i].size();l++){
rel[i][j].pb(0);
for(int r=0;r<app[j].size();r++){
if(app[j][r]<app[i][l]){
rel[i][j].back()++;
continue;
}
else break;
}
}
}
}
if(app[0].size())dp[1][1][0][0]=app[0][0]-1;
if(app[1].size())dp[1][0][1][1]=app[1][0]-1;
if(app[2].size())dp[1][0][0][2]=app[2][0]-1;
for(int i=1;i<n;i++){
for(int a=0;a<=i;a++){
for(int b=0;b<=i-a;b++){
for(int l=0;l<3;l++){
if(dp[i][a][b][l]==1e9)continue;
int c=i-a-b;
if(l!=0&&a<app[0].size()){
int sum=max(rel[0][0][a]-a,0)+max(rel[0][1][a]-b,0)+max(rel[0][2][a]-c,0);
dp[i+1][a+1][b][0]=min(dp[i+1][a+1][b][0],dp[i][a][b][l]+sum);
}
if(l!=1&&b<app[1].size()){
int sum=max(rel[1][0][b]-a,0)+max(rel[1][1][b]-b,0)+max(rel[1][2][b]-c,0);
dp[i+1][a][b+1][1]=min(dp[i+1][a][b+1][1],dp[i][a][b][l]+sum);
}
if(l!=2&&c<app[2].size()){
int sum=max(rel[2][0][c]-a,0)+max(rel[2][1][c]-b,0)+max(rel[2][2][c]-c,0);
dp[i+1][a][b][2]=min(dp[i+1][a][b][2],dp[i][a][b][l]+sum);
}
}
}
}
}
int ans=min(min(dp[n][app[0].size()][app[1].size()][0],dp[n][app[0].size()][app[1].size()][1]),dp[n][app[0].size()][app[1].size()][2]);
if(ans==1e9)ans=-1;
cout<<ans;
}
# | 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... |