#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 |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
45660 KB |
Output is correct |
19 |
Correct |
3 ms |
37468 KB |
Output is correct |
20 |
Correct |
4 ms |
47708 KB |
Output is correct |
21 |
Correct |
3 ms |
39516 KB |
Output is correct |
22 |
Correct |
4 ms |
47448 KB |
Output is correct |
23 |
Correct |
3 ms |
33372 KB |
Output is correct |
24 |
Correct |
2 ms |
21080 KB |
Output is correct |
25 |
Correct |
6 ms |
72284 KB |
Output is correct |
26 |
Correct |
5 ms |
59996 KB |
Output is correct |
27 |
Correct |
5 ms |
51804 KB |
Output is correct |
28 |
Correct |
4 ms |
41560 KB |
Output is correct |
29 |
Correct |
4 ms |
43612 KB |
Output is correct |
30 |
Correct |
3 ms |
33372 KB |
Output is correct |
31 |
Correct |
3 ms |
29276 KB |
Output is correct |
32 |
Correct |
3 ms |
35420 KB |
Output is correct |
33 |
Correct |
4 ms |
47772 KB |
Output is correct |
34 |
Correct |
4 ms |
43608 KB |
Output is correct |
35 |
Correct |
4 ms |
45660 KB |
Output is correct |
36 |
Correct |
4 ms |
39496 KB |
Output is correct |
37 |
Correct |
3 ms |
25176 KB |
Output is correct |
38 |
Correct |
2 ms |
27228 KB |
Output is correct |
39 |
Correct |
4 ms |
37468 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
3 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
51 ms |
223764 KB |
Output is correct |
3 |
Correct |
56 ms |
224356 KB |
Output is correct |
4 |
Correct |
54 ms |
223572 KB |
Output is correct |
5 |
Correct |
50 ms |
226228 KB |
Output is correct |
6 |
Correct |
49 ms |
227412 KB |
Output is correct |
7 |
Correct |
48 ms |
225364 KB |
Output is correct |
8 |
Correct |
49 ms |
225360 KB |
Output is correct |
9 |
Correct |
52 ms |
226128 KB |
Output is correct |
10 |
Correct |
52 ms |
226128 KB |
Output is correct |
11 |
Correct |
51 ms |
226128 KB |
Output is correct |
12 |
Correct |
17 ms |
130136 KB |
Output is correct |
13 |
Correct |
27 ms |
155368 KB |
Output is correct |
14 |
Correct |
35 ms |
182916 KB |
Output is correct |
15 |
Correct |
51 ms |
225872 KB |
Output is correct |
16 |
Correct |
55 ms |
225872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
45660 KB |
Output is correct |
19 |
Correct |
3 ms |
37468 KB |
Output is correct |
20 |
Correct |
4 ms |
47708 KB |
Output is correct |
21 |
Correct |
3 ms |
39516 KB |
Output is correct |
22 |
Correct |
4 ms |
47448 KB |
Output is correct |
23 |
Correct |
3 ms |
33372 KB |
Output is correct |
24 |
Correct |
2 ms |
21080 KB |
Output is correct |
25 |
Correct |
6 ms |
72284 KB |
Output is correct |
26 |
Correct |
5 ms |
59996 KB |
Output is correct |
27 |
Correct |
5 ms |
51804 KB |
Output is correct |
28 |
Correct |
4 ms |
41560 KB |
Output is correct |
29 |
Correct |
4 ms |
43612 KB |
Output is correct |
30 |
Correct |
3 ms |
33372 KB |
Output is correct |
31 |
Correct |
3 ms |
29276 KB |
Output is correct |
32 |
Correct |
3 ms |
35420 KB |
Output is correct |
33 |
Correct |
4 ms |
47772 KB |
Output is correct |
34 |
Correct |
4 ms |
43608 KB |
Output is correct |
35 |
Correct |
4 ms |
45660 KB |
Output is correct |
36 |
Correct |
4 ms |
39496 KB |
Output is correct |
37 |
Correct |
3 ms |
25176 KB |
Output is correct |
38 |
Correct |
2 ms |
27228 KB |
Output is correct |
39 |
Correct |
4 ms |
37468 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
3 ms |
19036 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
51 ms |
223764 KB |
Output is correct |
44 |
Correct |
56 ms |
224356 KB |
Output is correct |
45 |
Correct |
54 ms |
223572 KB |
Output is correct |
46 |
Correct |
50 ms |
226228 KB |
Output is correct |
47 |
Correct |
49 ms |
227412 KB |
Output is correct |
48 |
Correct |
48 ms |
225364 KB |
Output is correct |
49 |
Correct |
49 ms |
225360 KB |
Output is correct |
50 |
Correct |
52 ms |
226128 KB |
Output is correct |
51 |
Correct |
52 ms |
226128 KB |
Output is correct |
52 |
Correct |
51 ms |
226128 KB |
Output is correct |
53 |
Correct |
17 ms |
130136 KB |
Output is correct |
54 |
Correct |
27 ms |
155368 KB |
Output is correct |
55 |
Correct |
35 ms |
182916 KB |
Output is correct |
56 |
Correct |
51 ms |
225872 KB |
Output is correct |
57 |
Correct |
55 ms |
225872 KB |
Output is correct |
58 |
Correct |
45 ms |
150620 KB |
Output is correct |
59 |
Correct |
52 ms |
167760 KB |
Output is correct |
60 |
Correct |
48 ms |
162196 KB |
Output is correct |
61 |
Correct |
46 ms |
158288 KB |
Output is correct |
62 |
Correct |
56 ms |
227664 KB |
Output is correct |
63 |
Correct |
56 ms |
223152 KB |
Output is correct |
64 |
Correct |
57 ms |
215632 KB |
Output is correct |
65 |
Correct |
56 ms |
198484 KB |
Output is correct |
66 |
Correct |
46 ms |
159572 KB |
Output is correct |
67 |
Correct |
51 ms |
150356 KB |
Output is correct |
68 |
Correct |
46 ms |
161104 KB |
Output is correct |
69 |
Correct |
47 ms |
160848 KB |
Output is correct |
70 |
Correct |
47 ms |
162900 KB |
Output is correct |
71 |
Correct |
49 ms |
161884 KB |
Output is correct |
72 |
Correct |
25 ms |
144220 KB |
Output is correct |
73 |
Correct |
2 ms |
8796 KB |
Output is correct |