#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
const int INF = 1e9 + 10;
int dp[401][401][401][3];
int a[401];
int cnt[401][3];
vector<int> q[3];
void solve(){
int n;
cin>>n;
int cnt0 = 0, cnt1 = 0, cnt2 = 0;
for(int i=1; i<=n; i++){
char c;
cin>>c;
if(c == 'R') a[i] = 0, cnt0 ++;
else if(c == 'G') a[i] = 1, cnt1 ++;
else a[i] = 2, cnt2 ++;
}
for(int i=1; i<=n; i++){
q[a[i]].pb(i);
for(int d=0; d<3; d++){
cnt[i][d] = cnt[i-1][d] + (a[i] == d);
}
}
for(int i=0; i<=cnt0; i++){
for(int j=0; j<=cnt1; j++){
for(int k=0; k<=cnt2; k++){
for(int d=0; d<=2; d++){
dp[i][j][k][d] = INF;
}
}
}
}
for(int d=0; d<3; d++) dp[0][0][0][d] = 0;
for(int i=0; i<n; i++){
for(int x=0; x<=min(i, cnt0); x++){
for(int y=0; y<=min(i-x, cnt1); y++){
int z = i - x - y;
if(z > cnt2) continue;
if(x < cnt0)dp[x+1][y][z][0] = min(dp[x+1][y][z][0], min(dp[x][y][z][1], dp[x][y][z][2])+\
max(0, cnt[q[0][x]][1] - y) + max(0, cnt[q[0][x]][2] - z));
if(y < cnt1)dp[x][y+1][z][1] = min(dp[x][y+1][z][1], min(dp[x][y][z][0], dp[x][y][z][2])+\
max(0, cnt[q[1][y]][0] - x) + max(0, cnt[q[1][y]][2] - z));
if(z < cnt2)dp[x][y][z+1][2] = min(dp[x][y][z+1][2], min(dp[x][y][z][1], dp[x][y][z][0])+\
max(0, cnt[q[2][z]][1] - y) + max(0, cnt[q[2][z]][0] - x));
}
}
}
int ans = INF;
for(int i=0; i<3; i++){
ans = min(ans, dp[cnt0][cnt1][cnt2][i]);
}
if(ans < INF) cout<<ans;
else cout<<-1;
}
signed main(){
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2496 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 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 |
2652 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2496 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 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 |
2652 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
448 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
1628 KB |
Output is correct |
23 |
Correct |
1 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
3676 KB |
Output is correct |
25 |
Correct |
2 ms |
4092 KB |
Output is correct |
26 |
Correct |
2 ms |
4188 KB |
Output is correct |
27 |
Correct |
1 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
4188 KB |
Output is correct |
30 |
Correct |
2 ms |
2240 KB |
Output is correct |
31 |
Correct |
1 ms |
3932 KB |
Output is correct |
32 |
Correct |
1 ms |
2452 KB |
Output is correct |
33 |
Correct |
2 ms |
3944 KB |
Output is correct |
34 |
Correct |
1 ms |
3688 KB |
Output is correct |
35 |
Correct |
1 ms |
4712 KB |
Output is correct |
36 |
Correct |
1 ms |
1896 KB |
Output is correct |
37 |
Correct |
1 ms |
1640 KB |
Output is correct |
38 |
Correct |
1 ms |
2152 KB |
Output is correct |
39 |
Correct |
1 ms |
2408 KB |
Output is correct |
40 |
Correct |
0 ms |
360 KB |
Output is correct |
41 |
Correct |
1 ms |
2152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
360 KB |
Output is correct |
2 |
Correct |
68 ms |
162888 KB |
Output is correct |
3 |
Correct |
65 ms |
162056 KB |
Output is correct |
4 |
Correct |
65 ms |
164008 KB |
Output is correct |
5 |
Correct |
65 ms |
164136 KB |
Output is correct |
6 |
Correct |
66 ms |
164092 KB |
Output is correct |
7 |
Correct |
62 ms |
163344 KB |
Output is correct |
8 |
Correct |
64 ms |
162144 KB |
Output is correct |
9 |
Correct |
61 ms |
161324 KB |
Output is correct |
10 |
Correct |
67 ms |
164032 KB |
Output is correct |
11 |
Correct |
62 ms |
163092 KB |
Output is correct |
12 |
Correct |
16 ms |
44124 KB |
Output is correct |
13 |
Correct |
31 ms |
78616 KB |
Output is correct |
14 |
Correct |
41 ms |
111336 KB |
Output is correct |
15 |
Correct |
60 ms |
162900 KB |
Output is correct |
16 |
Correct |
63 ms |
164104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
2496 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
444 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 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 |
2652 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
448 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
1628 KB |
Output is correct |
23 |
Correct |
1 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
3676 KB |
Output is correct |
25 |
Correct |
2 ms |
4092 KB |
Output is correct |
26 |
Correct |
2 ms |
4188 KB |
Output is correct |
27 |
Correct |
1 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
4188 KB |
Output is correct |
30 |
Correct |
2 ms |
2240 KB |
Output is correct |
31 |
Correct |
1 ms |
3932 KB |
Output is correct |
32 |
Correct |
1 ms |
2452 KB |
Output is correct |
33 |
Correct |
2 ms |
3944 KB |
Output is correct |
34 |
Correct |
1 ms |
3688 KB |
Output is correct |
35 |
Correct |
1 ms |
4712 KB |
Output is correct |
36 |
Correct |
1 ms |
1896 KB |
Output is correct |
37 |
Correct |
1 ms |
1640 KB |
Output is correct |
38 |
Correct |
1 ms |
2152 KB |
Output is correct |
39 |
Correct |
1 ms |
2408 KB |
Output is correct |
40 |
Correct |
0 ms |
360 KB |
Output is correct |
41 |
Correct |
1 ms |
2152 KB |
Output is correct |
42 |
Correct |
0 ms |
360 KB |
Output is correct |
43 |
Correct |
68 ms |
162888 KB |
Output is correct |
44 |
Correct |
65 ms |
162056 KB |
Output is correct |
45 |
Correct |
65 ms |
164008 KB |
Output is correct |
46 |
Correct |
65 ms |
164136 KB |
Output is correct |
47 |
Correct |
66 ms |
164092 KB |
Output is correct |
48 |
Correct |
62 ms |
163344 KB |
Output is correct |
49 |
Correct |
64 ms |
162144 KB |
Output is correct |
50 |
Correct |
61 ms |
161324 KB |
Output is correct |
51 |
Correct |
67 ms |
164032 KB |
Output is correct |
52 |
Correct |
62 ms |
163092 KB |
Output is correct |
53 |
Correct |
16 ms |
44124 KB |
Output is correct |
54 |
Correct |
31 ms |
78616 KB |
Output is correct |
55 |
Correct |
41 ms |
111336 KB |
Output is correct |
56 |
Correct |
60 ms |
162900 KB |
Output is correct |
57 |
Correct |
63 ms |
164104 KB |
Output is correct |
58 |
Correct |
52 ms |
75604 KB |
Output is correct |
59 |
Correct |
61 ms |
95712 KB |
Output is correct |
60 |
Correct |
60 ms |
88156 KB |
Output is correct |
61 |
Correct |
59 ms |
83984 KB |
Output is correct |
62 |
Correct |
60 ms |
161876 KB |
Output is correct |
63 |
Correct |
69 ms |
161484 KB |
Output is correct |
64 |
Correct |
86 ms |
150096 KB |
Output is correct |
65 |
Correct |
69 ms |
139600 KB |
Output is correct |
66 |
Correct |
58 ms |
86608 KB |
Output is correct |
67 |
Correct |
59 ms |
83160 KB |
Output is correct |
68 |
Correct |
59 ms |
89060 KB |
Output is correct |
69 |
Correct |
79 ms |
85844 KB |
Output is correct |
70 |
Correct |
60 ms |
93008 KB |
Output is correct |
71 |
Correct |
61 ms |
83028 KB |
Output is correct |
72 |
Correct |
22 ms |
43100 KB |
Output is correct |
73 |
Correct |
2 ms |
4188 KB |
Output is correct |