#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
// #pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
#define ckmin(a, b) a = min(a, b)
#define ckmax(a, b) a = max(a, b)
const int maxn = 400 + 5;
const int INF = 1e9;
int dp[3][maxn][maxn][maxn];
int cnt[3][maxn];
int main(void){
fastio;
int n;
cin>>n;
string s;
cin>>s;
vector<vector<int>> pos(3);
for(int i = 0; i < n; i++){
if(i) for(int j = 0; j < 3; j++) cnt[j][i] = cnt[j][i - 1];
if(s[i] == 'R') pos[0].pb(i), cnt[0][i] ++;
else if(s[i] == 'Y') pos[1].pb(i), cnt[1][i] ++;
else pos[2].pb(i), cnt[2][i] ++;
}
for(int i = 0; i < 3; i++){
if(pos[i].size() > (n + 1) / 2){
cout<<-1<<"\n";
return 0;
}
}
for(int i = 0; i < 3; i++) for(int j = 0; j <= pos[0].size(); j++) for(int k = 0; k <= pos[1].size(); k++) for(int l = 0; l <= pos[2].size(); l++) dp[i][j][k][l] = INF;
if(pos[0].size()) dp[0][1][0][0] = pos[0][0];
if(pos[1].size()) dp[1][0][1][0] = pos[1][0];
if(pos[2].size()) dp[2][0][0][1] = pos[2][0];
for(int i = 2; i <= n; i++){
for(int a = 0; a <= pos[0].size(); a++){
for(int b = 0; b <= pos[1].size(); b++){
int c = i - a - b;
if(c > pos[2].size()) continue;
for(int lst = 0; lst < 3; lst++) dp[lst][a][b][c] = INF;
if(a) dp[0][a][b][c] = min(dp[1][a - 1][b][c], dp[2][a - 1][b][c]) + abs(cnt[1][pos[0][a - 1]] - b) + abs(cnt[2][pos[0][a - 1]] - c);
if(b) dp[1][a][b][c] = min(dp[0][a][b - 1][c], dp[2][a][b - 1][c]) + abs(cnt[0][pos[1][b - 1]] - a) + abs(cnt[2][pos[1][b - 1]] - c);
if(c) dp[2][a][b][c] = min(dp[0][a][b][c - 1], dp[1][a][b][c - 1]) + abs(cnt[0][pos[2][c - 1]] - a) + abs(cnt[1][pos[2][c - 1]] - b);
}
}
}
int ans = INF;
for(int i = 0; i < 3; i++) ans = min(ans, dp[i][pos[0].size()][pos[1].size()][pos[2].size()]);
cout<<ans / 2<<"\n";
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:33:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
33 | if(pos[i].size() > (n + 1) / 2){
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:38:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 0; i < 3; i++) for(int j = 0; j <= pos[0].size(); j++) for(int k = 0; k <= pos[1].size(); k++) for(int l = 0; l <= pos[2].size(); l++) dp[i][j][k][l] = INF;
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:38:86: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 0; i < 3; i++) for(int j = 0; j <= pos[0].size(); j++) for(int k = 0; k <= pos[1].size(); k++) for(int l = 0; l <= pos[2].size(); l++) dp[i][j][k][l] = INF;
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:38:126: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 0; i < 3; i++) for(int j = 0; j <= pos[0].size(); j++) for(int k = 0; k <= pos[1].size(); k++) for(int l = 0; l <= pos[2].size(); l++) dp[i][j][k][l] = INF;
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:43:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int a = 0; a <= pos[0].size(); a++){
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:44:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int b = 0; b <= pos[1].size(); b++){
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:46:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if(c > pos[2].size()) continue;
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
16848 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14936 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
16848 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14936 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
5 ms |
49500 KB |
Output is correct |
19 |
Correct |
5 ms |
41308 KB |
Output is correct |
20 |
Correct |
6 ms |
53852 KB |
Output is correct |
21 |
Correct |
5 ms |
43356 KB |
Output is correct |
22 |
Correct |
5 ms |
51804 KB |
Output is correct |
23 |
Correct |
4 ms |
37216 KB |
Output is correct |
24 |
Correct |
3 ms |
25080 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
6 ms |
57700 KB |
Output is correct |
28 |
Correct |
5 ms |
45576 KB |
Output is correct |
29 |
Correct |
5 ms |
47460 KB |
Output is correct |
30 |
Correct |
4 ms |
37220 KB |
Output is correct |
31 |
Correct |
4 ms |
35176 KB |
Output is correct |
32 |
Correct |
5 ms |
39272 KB |
Output is correct |
33 |
Correct |
0 ms |
352 KB |
Output is correct |
34 |
Correct |
0 ms |
356 KB |
Output is correct |
35 |
Correct |
5 ms |
49508 KB |
Output is correct |
36 |
Correct |
5 ms |
43364 KB |
Output is correct |
37 |
Correct |
3 ms |
31076 KB |
Output is correct |
38 |
Correct |
3 ms |
33124 KB |
Output is correct |
39 |
Correct |
4 ms |
41316 KB |
Output is correct |
40 |
Correct |
0 ms |
356 KB |
Output is correct |
41 |
Correct |
0 ms |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8544 KB |
Output is correct |
2 |
Correct |
12 ms |
115784 KB |
Output is correct |
3 |
Correct |
13 ms |
113960 KB |
Output is correct |
4 |
Correct |
12 ms |
114012 KB |
Output is correct |
5 |
Correct |
12 ms |
113752 KB |
Output is correct |
6 |
Correct |
12 ms |
113784 KB |
Output is correct |
7 |
Correct |
12 ms |
113756 KB |
Output is correct |
8 |
Correct |
12 ms |
113752 KB |
Output is correct |
9 |
Correct |
12 ms |
113716 KB |
Output is correct |
10 |
Correct |
12 ms |
115804 KB |
Output is correct |
11 |
Correct |
12 ms |
113756 KB |
Output is correct |
12 |
Correct |
11 ms |
113904 KB |
Output is correct |
13 |
Correct |
12 ms |
116568 KB |
Output is correct |
14 |
Correct |
11 ms |
112988 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
2 ms |
16732 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
16848 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14936 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
5 ms |
49500 KB |
Output is correct |
19 |
Correct |
5 ms |
41308 KB |
Output is correct |
20 |
Correct |
6 ms |
53852 KB |
Output is correct |
21 |
Correct |
5 ms |
43356 KB |
Output is correct |
22 |
Correct |
5 ms |
51804 KB |
Output is correct |
23 |
Correct |
4 ms |
37216 KB |
Output is correct |
24 |
Correct |
3 ms |
25080 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
6 ms |
57700 KB |
Output is correct |
28 |
Correct |
5 ms |
45576 KB |
Output is correct |
29 |
Correct |
5 ms |
47460 KB |
Output is correct |
30 |
Correct |
4 ms |
37220 KB |
Output is correct |
31 |
Correct |
4 ms |
35176 KB |
Output is correct |
32 |
Correct |
5 ms |
39272 KB |
Output is correct |
33 |
Correct |
0 ms |
352 KB |
Output is correct |
34 |
Correct |
0 ms |
356 KB |
Output is correct |
35 |
Correct |
5 ms |
49508 KB |
Output is correct |
36 |
Correct |
5 ms |
43364 KB |
Output is correct |
37 |
Correct |
3 ms |
31076 KB |
Output is correct |
38 |
Correct |
3 ms |
33124 KB |
Output is correct |
39 |
Correct |
4 ms |
41316 KB |
Output is correct |
40 |
Correct |
0 ms |
356 KB |
Output is correct |
41 |
Correct |
0 ms |
472 KB |
Output is correct |
42 |
Correct |
1 ms |
8544 KB |
Output is correct |
43 |
Correct |
12 ms |
115784 KB |
Output is correct |
44 |
Correct |
13 ms |
113960 KB |
Output is correct |
45 |
Correct |
12 ms |
114012 KB |
Output is correct |
46 |
Correct |
12 ms |
113752 KB |
Output is correct |
47 |
Correct |
12 ms |
113784 KB |
Output is correct |
48 |
Correct |
12 ms |
113756 KB |
Output is correct |
49 |
Correct |
12 ms |
113752 KB |
Output is correct |
50 |
Correct |
12 ms |
113716 KB |
Output is correct |
51 |
Correct |
12 ms |
115804 KB |
Output is correct |
52 |
Correct |
12 ms |
113756 KB |
Output is correct |
53 |
Correct |
11 ms |
113904 KB |
Output is correct |
54 |
Correct |
12 ms |
116568 KB |
Output is correct |
55 |
Correct |
11 ms |
112988 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
66 ms |
161752 KB |
Output is correct |
59 |
Correct |
65 ms |
162392 KB |
Output is correct |
60 |
Correct |
67 ms |
165764 KB |
Output is correct |
61 |
Correct |
64 ms |
163408 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
19 ms |
120872 KB |
Output is correct |
64 |
Correct |
36 ms |
136408 KB |
Output is correct |
65 |
Correct |
47 ms |
141136 KB |
Output is correct |
66 |
Correct |
63 ms |
165136 KB |
Output is correct |
67 |
Correct |
59 ms |
148828 KB |
Output is correct |
68 |
Correct |
63 ms |
163152 KB |
Output is correct |
69 |
Correct |
65 ms |
164168 KB |
Output is correct |
70 |
Correct |
63 ms |
160852 KB |
Output is correct |
71 |
Correct |
70 ms |
173648 KB |
Output is correct |
72 |
Correct |
0 ms |
344 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |