/*
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░▒▒░░▄░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░█░▀░░░░░▌░░░░░░░░░░░░░░▄░░░░░░░░░░░░░░░░░░█░░░░░▌░░░░█░░░░░░░░░░░░░░░░
░░░░░░░░░▌░░░░░░░▐░░░░░░░░▌░░░░██░▀▌░░░░░░░░▐▌░░░░░██░░░░▐░░░░░█░░░░░░░░░░░░░░░░
░░░░░░░░█░░░░░░░░█░░░░░░░█▌░░░░█░░░▌░░░░░░░░█░░░░░█░▐░░░░▐░░░░░▌░░░░░░░░░░░░░░░░
░░░░░░░░█░░░░░░░░█░░░░░░█░▌░░░░█░░░▄░░░░░░░▐▄░███▀░░▐░░░░█░░░░██░░░░░░░░░░░░░░░░
░░░░░░░░░█░░░░░░▐█░░░░░█░▐░░░░▐▌░░░▐░░░░░░█▀░▌░░▀▌░░▐░░░░░█▄░█░░▌░░░░░░░░░░░░░░░
░░░░░░░░░░█▄░░░█░░█░░░█░░█░░░▄█▌░░░░█▄░░▄█░░░█░░░▌░░░█░▄█░░░▀░░░█▄░░░░░░░░░░░░░░
░░░░░░░░░░░░▀▀▀░░░░▀█▀░░░▌▄▄█░█░░░░░░░▀▀░░░░░░██▀░░░░░▀░░░░░░░░░░░▀▀░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░▄██▀░░░░█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░▄▄█▀░█░░░░░▐█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░▌░░░░█░░░░░░▐▌░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░█░░▄█░░░░░░░▐▌░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░▀▀░░░░░░░░░▐▌░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
#include<bits/stdc++.h>
// #include "stress.cpp"
#define IOS ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie()
#define ll long long
#define ull unsigned long long
#define pb push_back
// #define endl "\n"
#define int ll
#define F first
#define S second
#define db long double
#define ld long double
#define short unsigned short
#define pii pair<int,int>
#define all(x) x.begin(),x.enad();
using namespace std;
const ll inf = 1e18,MOD=1e9+7,N=400+10,MN=1e9+7,lim=100;
const db Pie=acos(-1);
int binpow(int a,int p);
int lcgayletiar(int a,int b);
//...and justice for all
int cnt[3];
int pr[3][N];
int in[N][3];
int dp[N][N][N][3];
const void solve(){
int n;
cin>>n;
string c;
cin>>c;
for(int i=0;i<n;i++){
if(i!=0){
pr[0][i]=pr[0][i-1];
pr[1][i]=pr[1][i-1];
pr[2][i]=pr[2][i-1];
}
if(c[i]=='R'){
pr[0][i]++;
cnt[0]++;
in[cnt[0]][0]=i;
}
if(c[i]=='G'){
pr[1][i]++;
cnt[1]++;
in[cnt[1]][1]=i;
}
if(c[i]=='Y'){
pr[2][i]++;
cnt[2]++;
in[cnt[2]][2]=i;
}
}
for(int i=0;i<=cnt[0];i++){
for(int j=0;j<=cnt[1];j++){
for(int k=0;k<=cnt[2];k++){
for(int l=0;l<3;l++){
dp[i][j][k][l]=inf;
}
}
}
}
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for(int i=0;i<=cnt[0];i++){
for(int j=0;j<=cnt[1];j++){
for(int k=0;k<=cnt[2];k++){
if(i){
int cur=in[i][0]+1-(i+j+k);
cur+=max(j-pr[1][in[i][0]],0ll);
cur+=max(k-pr[2][in[i][0]],0ll);
dp[i][j][k][0]=min({dp[i][j][k][0],dp[i-1][j][k][1]+cur,dp[i-1][j][k][2]+cur});
// cout<<i<<" "<<j<<" "<<k<<" "<<cur<<" "<<pr[1][in[i][0]]<<endl;
}
if(j){
int cur=in[j][1]+1-(i+j+k);
cur+=max(i-pr[0][in[j][1]],0ll);
cur+=max(k-pr[2][in[j][1]],0ll);
dp[i][j][k][1]=min({dp[i][j][k][1],dp[i][j-1][k][0]+cur,dp[i][j-1][k][2]+cur});
}
if(k){
int cur=in[k][2]+1-(i+j+k);
cur+=max(i-pr[0][in[k][2]],0ll);
cur+=max(j-pr[1][in[k][2]],0ll);
dp[i][j][k][2]=min({dp[i][j][k][2],dp[i][j][k-1][0]+cur,dp[i][j][k-1][1]+cur});
}
}
// cout<<endl;
}
// cout<<endl;
}
int ans=min({dp[cnt[0]][cnt[1]][cnt[2]][0],dp[cnt[0]][cnt[1]][cnt[2]][1],dp[cnt[0]][cnt[1]][cnt[2]][2]});
if(ans==inf){
cout<<-1<<endl;
}
else{
cout<<ans<<endl;
}
}
signed main() {
srand(time(NULL));
IOS;
// freopen("sequence.in", "r", stdin);
// freopen("sequence.out", "w", stdout);
int pantyandstocking=1;
// cin>>pantyandstocking;
for(int i=1;i<=pantyandstocking;i++) {
// cout<<"Test "<<i<<": "<<endl;
solve();
// cout<<endl;
}
// cout<<fixed<<setprecision(10);
// cerr<<"Time:"<<1000*((double)clock())/(double) CLOCKS_PER_SEC<<"ms\n";
}
int lcgayletiar(int a,int b){return (a*b)/(int)__gcd(a,b);}
int binpow(int a,int p){if(p==0)return 1;if(p%2){return ((binpow(a,p-1)*a)%MOD);}int res=binpow(a,p/2)%MOD; return (res*res)%MOD;}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
1744 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
3932 KB |
Output is correct |
26 |
Correct |
1 ms |
4188 KB |
Output is correct |
27 |
Correct |
1 ms |
3168 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
3 ms |
2140 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
1 ms |
3932 KB |
Output is correct |
34 |
Correct |
1 ms |
3672 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
36 |
Correct |
1 ms |
2140 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
56 ms |
164168 KB |
Output is correct |
3 |
Correct |
54 ms |
163360 KB |
Output is correct |
4 |
Correct |
54 ms |
164176 KB |
Output is correct |
5 |
Correct |
53 ms |
164176 KB |
Output is correct |
6 |
Correct |
53 ms |
164192 KB |
Output is correct |
7 |
Correct |
54 ms |
163408 KB |
Output is correct |
8 |
Correct |
54 ms |
163412 KB |
Output is correct |
9 |
Correct |
54 ms |
162536 KB |
Output is correct |
10 |
Correct |
54 ms |
163992 KB |
Output is correct |
11 |
Correct |
54 ms |
164180 KB |
Output is correct |
12 |
Correct |
17 ms |
44376 KB |
Output is correct |
13 |
Correct |
25 ms |
77664 KB |
Output is correct |
14 |
Correct |
39 ms |
112216 KB |
Output is correct |
15 |
Correct |
54 ms |
164176 KB |
Output is correct |
16 |
Correct |
61 ms |
164220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
1744 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
3932 KB |
Output is correct |
26 |
Correct |
1 ms |
4188 KB |
Output is correct |
27 |
Correct |
1 ms |
3168 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
3 ms |
2140 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
1 ms |
3932 KB |
Output is correct |
34 |
Correct |
1 ms |
3672 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
36 |
Correct |
1 ms |
2140 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
56 ms |
164168 KB |
Output is correct |
44 |
Correct |
54 ms |
163360 KB |
Output is correct |
45 |
Correct |
54 ms |
164176 KB |
Output is correct |
46 |
Correct |
53 ms |
164176 KB |
Output is correct |
47 |
Correct |
53 ms |
164192 KB |
Output is correct |
48 |
Correct |
54 ms |
163408 KB |
Output is correct |
49 |
Correct |
54 ms |
163412 KB |
Output is correct |
50 |
Correct |
54 ms |
162536 KB |
Output is correct |
51 |
Correct |
54 ms |
163992 KB |
Output is correct |
52 |
Correct |
54 ms |
164180 KB |
Output is correct |
53 |
Correct |
17 ms |
44376 KB |
Output is correct |
54 |
Correct |
25 ms |
77664 KB |
Output is correct |
55 |
Correct |
39 ms |
112216 KB |
Output is correct |
56 |
Correct |
54 ms |
164176 KB |
Output is correct |
57 |
Correct |
61 ms |
164220 KB |
Output is correct |
58 |
Correct |
60 ms |
120728 KB |
Output is correct |
59 |
Correct |
67 ms |
138068 KB |
Output is correct |
60 |
Correct |
62 ms |
129876 KB |
Output is correct |
61 |
Correct |
70 ms |
128152 KB |
Output is correct |
62 |
Correct |
60 ms |
163668 KB |
Output is correct |
63 |
Correct |
64 ms |
164948 KB |
Output is correct |
64 |
Correct |
72 ms |
164692 KB |
Output is correct |
65 |
Correct |
76 ms |
158804 KB |
Output is correct |
66 |
Correct |
61 ms |
128336 KB |
Output is correct |
67 |
Correct |
61 ms |
125520 KB |
Output is correct |
68 |
Correct |
75 ms |
131152 KB |
Output is correct |
69 |
Correct |
66 ms |
128964 KB |
Output is correct |
70 |
Correct |
69 ms |
135016 KB |
Output is correct |
71 |
Correct |
68 ms |
126288 KB |
Output is correct |
72 |
Correct |
25 ms |
49816 KB |
Output is correct |
73 |
Correct |
3 ms |
5468 KB |
Output is correct |