#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> pos[3];
int pre[3][405];
int dp[3][405][405][405];
int sz[3];
int cst(int p, int t, int x1, int x2, int x3){
int sum = 0;
sum += max(0, pre[0][pos[t][p]] - pre[0][pos[0][x1]]);
sum += max(0, pre[1][pos[t][p]] - pre[1][pos[1][x2]]);
sum += max(0, pre[2][pos[t][p]] - pre[2][pos[2][x3]]);
//cout << sum << "\n";
return sum;
}
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
pos[0].emplace_back(0), pos[1].emplace_back(0), pos[2].emplace_back(0);
for(int i = 1; i<=N; i++){
char c;
cin >> c;
if(c == 'R'){
sz[0]++;
pos[0].emplace_back(i);
}
else if(c == 'G'){
sz[1]++;
pos[1].emplace_back(i);
}
else{
sz[2]++;
pos[2].emplace_back(i);
}
}
if(max({sz[0], sz[1], sz[2]}) > (N+1)/2){
cout << -1 << "\n";
return 0;
}
for(int c = 0; c<3; c++){
for(int n : pos[c]){
pre[c][n]++;
}
partial_sum(pre[c], pre[c]+N+1, pre[c]);
}
for(int r = 0; r<=sz[0]; r++){
for(int g = 0; g<=sz[1]; g++){
for(int y = 0; y<=sz[2]; y++){
dp[0][r][g][y] = dp[1][r][g][y] = dp[2][r][g][y] = 1000000;
}
}
}
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
for(int r = 0; r<=sz[0]; r++){
for(int g = 0; g<=sz[1]; g++){
for(int y = 0; y<=sz[2]; y++){
if(r){
dp[0][r][g][y] = min(dp[1][r-1][g][y], dp[2][r-1][g][y]) + cst(r, 0, r, g, y);
}
if(g){
dp[1][r][g][y] = min(dp[0][r][g-1][y], dp[2][r][g-1][y]) + cst(g, 1, r, g, y);
}
if(y){
dp[2][r][g][y] = min(dp[0][r][g][y-1], dp[1][r][g][y-1]) + cst(y, 2, r, g, y);
}
//cout << dp[0][r][g][y] << " " << dp[1][r][g][y] << " " << dp[2][r][g][y] << "\n";
}
}
}
cout << min({dp[0][sz[0]][sz[1]][sz[2]], dp[1][sz[0]][sz[1]][sz[2]], dp[2][sz[0]][sz[1]][sz[2]]});
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2304 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2944 KB |
Output is correct |
22 |
Correct |
5 ms |
2048 KB |
Output is correct |
23 |
Correct |
6 ms |
2560 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
3712 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
5 ms |
416 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
6 ms |
3072 KB |
Output is correct |
36 |
Correct |
5 ms |
2304 KB |
Output is correct |
37 |
Correct |
5 ms |
2048 KB |
Output is correct |
38 |
Correct |
6 ms |
2560 KB |
Output is correct |
39 |
Correct |
6 ms |
2688 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
93 ms |
194296 KB |
Output is correct |
3 |
Correct |
94 ms |
193308 KB |
Output is correct |
4 |
Correct |
92 ms |
194296 KB |
Output is correct |
5 |
Correct |
93 ms |
194296 KB |
Output is correct |
6 |
Correct |
92 ms |
194296 KB |
Output is correct |
7 |
Correct |
94 ms |
193400 KB |
Output is correct |
8 |
Correct |
96 ms |
193272 KB |
Output is correct |
9 |
Correct |
92 ms |
192376 KB |
Output is correct |
10 |
Correct |
92 ms |
194296 KB |
Output is correct |
11 |
Correct |
92 ms |
194296 KB |
Output is correct |
12 |
Correct |
29 ms |
52864 KB |
Output is correct |
13 |
Correct |
46 ms |
92280 KB |
Output is correct |
14 |
Correct |
67 ms |
132996 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2304 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2944 KB |
Output is correct |
22 |
Correct |
5 ms |
2048 KB |
Output is correct |
23 |
Correct |
6 ms |
2560 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
3712 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
5 ms |
416 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
6 ms |
3072 KB |
Output is correct |
36 |
Correct |
5 ms |
2304 KB |
Output is correct |
37 |
Correct |
5 ms |
2048 KB |
Output is correct |
38 |
Correct |
6 ms |
2560 KB |
Output is correct |
39 |
Correct |
6 ms |
2688 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
4 ms |
384 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
93 ms |
194296 KB |
Output is correct |
44 |
Correct |
94 ms |
193308 KB |
Output is correct |
45 |
Correct |
92 ms |
194296 KB |
Output is correct |
46 |
Correct |
93 ms |
194296 KB |
Output is correct |
47 |
Correct |
92 ms |
194296 KB |
Output is correct |
48 |
Correct |
94 ms |
193400 KB |
Output is correct |
49 |
Correct |
96 ms |
193272 KB |
Output is correct |
50 |
Correct |
92 ms |
192376 KB |
Output is correct |
51 |
Correct |
92 ms |
194296 KB |
Output is correct |
52 |
Correct |
92 ms |
194296 KB |
Output is correct |
53 |
Correct |
29 ms |
52864 KB |
Output is correct |
54 |
Correct |
46 ms |
92280 KB |
Output is correct |
55 |
Correct |
67 ms |
132996 KB |
Output is correct |
56 |
Correct |
5 ms |
384 KB |
Output is correct |
57 |
Correct |
4 ms |
384 KB |
Output is correct |
58 |
Correct |
78 ms |
77304 KB |
Output is correct |
59 |
Correct |
93 ms |
97892 KB |
Output is correct |
60 |
Correct |
84 ms |
88960 KB |
Output is correct |
61 |
Correct |
86 ms |
86008 KB |
Output is correct |
62 |
Correct |
5 ms |
384 KB |
Output is correct |
63 |
Correct |
95 ms |
186744 KB |
Output is correct |
64 |
Correct |
103 ms |
163064 KB |
Output is correct |
65 |
Correct |
101 ms |
142020 KB |
Output is correct |
66 |
Correct |
84 ms |
87160 KB |
Output is correct |
67 |
Correct |
86 ms |
83832 KB |
Output is correct |
68 |
Correct |
86 ms |
89848 KB |
Output is correct |
69 |
Correct |
91 ms |
86648 KB |
Output is correct |
70 |
Correct |
87 ms |
93688 KB |
Output is correct |
71 |
Correct |
86 ms |
85240 KB |
Output is correct |
72 |
Correct |
4 ms |
384 KB |
Output is correct |
73 |
Correct |
4 ms |
384 KB |
Output is correct |