#include<bits/stdc++.h>
#define ll int
#define f first
#define sc second
using namespace std;
ll a,b,c,d,i,e,f,g,n,m,k,l,idx,idx1,idx2,ans;
ll posa[402],posb[402],posc[402],prea[402],preb[402],prec[402],dp[4][402][402][402];
string s;
int main() {
cin>>n>>s;
s="*"+s;
for(ll i=1;i<=n;i++) {
prea[i]=prea[i-1];
preb[i]=preb[i-1];
prec[i]=prec[i-1];
if(s[i]=='R') {
a++;
posa[a]=i;
prea[i]=a;
}
if(s[i]=='G') {
b++;
posb[b]=i;
preb[i]=b;
}
if(s[i]=='Y') {
c++;
posc[c]=i;
prec[i]=c;
}
//cout<<prea[i]<<" "<<preb[i]<<" "<<prec[i]<<endl;
}
for(ll i=0;i<=a;i++) {
for(ll j=0;j<=b;j++) {
for(ll u=0;u<=c;u++) {
for(ll x=0;x<3;x++) {
dp[x][i][j][u]=1e9+10;
}
}
}
}
for(ll i=0;i<3;i++)
dp[i][0][0][0]=0;
for(ll i=0;i<=a;i++) {
for(ll j=0;j<=b;j++) {
for(ll u=0;u<=c;u++) {
for(ll x=0;x<3;x++) {
for(ll y=0;y<3;y++) {
if(x==y) continue;
if(y==0 && i==a) continue;
if(y==1 && j==b) continue;
if(y==2 && u==c) continue;
k=dp[x][i][j][u];
if(y==0) {
idx=posa[i+1];
idx1=posb[j]; idx2=posc[u];
k+=max(f,preb[idx]-preb[idx1]);
k+=max(f,prec[idx]-prec[idx2]);
dp[y][i+1][j][u]=min(dp[y][i+1][j][u],k);
if(dp[y][i+1][j][u]<0) cout<<k<<" ";
}
if(y==1) {
idx=posb[j+1];
idx1=posa[i]; idx2=posc[u];
k+=max(f,prea[idx]-prea[idx1]);
k+=max(f,prec[idx]-prec[idx2]);
dp[y][i][j+1][u]=min(dp[y][i][j+1][u],k);
}
if(y==2) {
idx=posc[u+1];
idx1=posa[i]; idx2=posb[j];
k+=max(f,prea[idx]-prea[idx1]);
k+=max(f,preb[idx]-preb[idx2]);
dp[y][i][j][u+1]=min(dp[y][i][j][u+1],k);
}
}
}
}
}
}
ans=1e9+10;
for(ll i=0;i<3;i++) {
ans=min(ans,dp[i][a][b][c]);
}
if(ans==1e9+10) cout<<-1;
else cout<<ans;
}
/*
5
RRGYY
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
684 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
684 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2124 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
1996 KB |
Output is correct |
23 |
Correct |
2 ms |
2380 KB |
Output is correct |
24 |
Correct |
1 ms |
1740 KB |
Output is correct |
25 |
Correct |
3 ms |
4812 KB |
Output is correct |
26 |
Correct |
3 ms |
4940 KB |
Output is correct |
27 |
Correct |
3 ms |
3660 KB |
Output is correct |
28 |
Correct |
2 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2308 KB |
Output is correct |
32 |
Correct |
2 ms |
2764 KB |
Output is correct |
33 |
Correct |
3 ms |
4616 KB |
Output is correct |
34 |
Correct |
2 ms |
4172 KB |
Output is correct |
35 |
Correct |
2 ms |
3152 KB |
Output is correct |
36 |
Correct |
2 ms |
2124 KB |
Output is correct |
37 |
Correct |
2 ms |
1868 KB |
Output is correct |
38 |
Correct |
2 ms |
2380 KB |
Output is correct |
39 |
Correct |
2 ms |
2636 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
2480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
99 ms |
192804 KB |
Output is correct |
3 |
Correct |
99 ms |
191940 KB |
Output is correct |
4 |
Correct |
97 ms |
192716 KB |
Output is correct |
5 |
Correct |
97 ms |
192816 KB |
Output is correct |
6 |
Correct |
99 ms |
192704 KB |
Output is correct |
7 |
Correct |
94 ms |
191812 KB |
Output is correct |
8 |
Correct |
98 ms |
191868 KB |
Output is correct |
9 |
Correct |
93 ms |
190912 KB |
Output is correct |
10 |
Correct |
94 ms |
192760 KB |
Output is correct |
11 |
Correct |
95 ms |
192724 KB |
Output is correct |
12 |
Correct |
26 ms |
52288 KB |
Output is correct |
13 |
Correct |
45 ms |
91468 KB |
Output is correct |
14 |
Correct |
66 ms |
131928 KB |
Output is correct |
15 |
Correct |
104 ms |
192788 KB |
Output is correct |
16 |
Correct |
95 ms |
192816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
684 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2124 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
1996 KB |
Output is correct |
23 |
Correct |
2 ms |
2380 KB |
Output is correct |
24 |
Correct |
1 ms |
1740 KB |
Output is correct |
25 |
Correct |
3 ms |
4812 KB |
Output is correct |
26 |
Correct |
3 ms |
4940 KB |
Output is correct |
27 |
Correct |
3 ms |
3660 KB |
Output is correct |
28 |
Correct |
2 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2308 KB |
Output is correct |
32 |
Correct |
2 ms |
2764 KB |
Output is correct |
33 |
Correct |
3 ms |
4616 KB |
Output is correct |
34 |
Correct |
2 ms |
4172 KB |
Output is correct |
35 |
Correct |
2 ms |
3152 KB |
Output is correct |
36 |
Correct |
2 ms |
2124 KB |
Output is correct |
37 |
Correct |
2 ms |
1868 KB |
Output is correct |
38 |
Correct |
2 ms |
2380 KB |
Output is correct |
39 |
Correct |
2 ms |
2636 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
2480 KB |
Output is correct |
42 |
Correct |
1 ms |
304 KB |
Output is correct |
43 |
Correct |
99 ms |
192804 KB |
Output is correct |
44 |
Correct |
99 ms |
191940 KB |
Output is correct |
45 |
Correct |
97 ms |
192716 KB |
Output is correct |
46 |
Correct |
97 ms |
192816 KB |
Output is correct |
47 |
Correct |
99 ms |
192704 KB |
Output is correct |
48 |
Correct |
94 ms |
191812 KB |
Output is correct |
49 |
Correct |
98 ms |
191868 KB |
Output is correct |
50 |
Correct |
93 ms |
190912 KB |
Output is correct |
51 |
Correct |
94 ms |
192760 KB |
Output is correct |
52 |
Correct |
95 ms |
192724 KB |
Output is correct |
53 |
Correct |
26 ms |
52288 KB |
Output is correct |
54 |
Correct |
45 ms |
91468 KB |
Output is correct |
55 |
Correct |
66 ms |
131928 KB |
Output is correct |
56 |
Correct |
104 ms |
192788 KB |
Output is correct |
57 |
Correct |
95 ms |
192816 KB |
Output is correct |
58 |
Correct |
120 ms |
76596 KB |
Output is correct |
59 |
Correct |
129 ms |
97008 KB |
Output is correct |
60 |
Correct |
124 ms |
88224 KB |
Output is correct |
61 |
Correct |
131 ms |
85288 KB |
Output is correct |
62 |
Correct |
101 ms |
187904 KB |
Output is correct |
63 |
Correct |
108 ms |
185284 KB |
Output is correct |
64 |
Correct |
120 ms |
161772 KB |
Output is correct |
65 |
Correct |
126 ms |
140760 KB |
Output is correct |
66 |
Correct |
126 ms |
86524 KB |
Output is correct |
67 |
Correct |
121 ms |
83148 KB |
Output is correct |
68 |
Correct |
127 ms |
89028 KB |
Output is correct |
69 |
Correct |
127 ms |
85908 KB |
Output is correct |
70 |
Correct |
128 ms |
93076 KB |
Output is correct |
71 |
Correct |
122 ms |
84556 KB |
Output is correct |
72 |
Correct |
43 ms |
43700 KB |
Output is correct |
73 |
Correct |
7 ms |
2892 KB |
Output is correct |