#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
unordered_map<char, int> conv = {{'R', 0}, {'G', 1}, {'Y', 2}};
const int mxN = 402;
int dp[mxN][mxN][mxN][3];
int a[mxN];
vector<int> v[3];
int ps[3][mxN];
int n;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
string temp; cin >> temp;
for (int i = 1; i <= n; i++) a[i] = conv[temp[i - 1]];
for (int i = 0; i < 3; i++) {
v[i] = {-1};
ps[i][0] = 0;
for (int j = 1; j <= n; j++) {
ps[i][j] = ps[i][j - 1];
if (a[j] == i) {
ps[i][j]++; v[i].pb(j);
}
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
for (int l = 0; l < 3; l++) {
dp[i][j][k][l] = 1e8;
}
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int a = 0; a <= ps[0][n]; a++) {
for (int b = 0; b <= ps[1][n]; b++) {
for (int c = 0; c <= ps[2][n]; c++) {
if (a + b + c == 0) continue;
if (a) {
dp[a][b][c][0] = min(dp[a - 1][b][c][1], dp[a - 1][b][c][2]) + max(b - ps[1][v[0][a]], 0) + max(c - ps[2][v[0][a]], 0);
}
if (b) {
dp[a][b][c][1] = min(dp[a][b - 1][c][0], dp[a][b - 1][c][2]) + max(a - ps[0][v[1][b]], 0) + max(c - ps[2][v[1][b]], 0);
}
if (c) {
dp[a][b][c][2] = min(dp[a][b][c - 1][0], dp[a][b][c - 1][1]) + max(a - ps[0][v[2][c]], 0) + max(b - ps[1][v[2][c]], 0);
}
}
}
}
int ans = 1e8;
for (int i = 0; i < 3; i++) ans = min(ans, dp[ps[0][n]][ps[1][n]][ps[2][n]][i]);
cout << (ans == 1e8 ? -1 : ans) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
29016 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29260 KB |
Output is correct |
9 |
Correct |
6 ms |
29020 KB |
Output is correct |
10 |
Correct |
5 ms |
29032 KB |
Output is correct |
11 |
Correct |
5 ms |
29020 KB |
Output is correct |
12 |
Correct |
5 ms |
29020 KB |
Output is correct |
13 |
Correct |
4 ms |
29020 KB |
Output is correct |
14 |
Correct |
4 ms |
26972 KB |
Output is correct |
15 |
Correct |
5 ms |
29020 KB |
Output is correct |
16 |
Correct |
5 ms |
29020 KB |
Output is correct |
17 |
Correct |
5 ms |
27096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
29016 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29260 KB |
Output is correct |
9 |
Correct |
6 ms |
29020 KB |
Output is correct |
10 |
Correct |
5 ms |
29032 KB |
Output is correct |
11 |
Correct |
5 ms |
29020 KB |
Output is correct |
12 |
Correct |
5 ms |
29020 KB |
Output is correct |
13 |
Correct |
4 ms |
29020 KB |
Output is correct |
14 |
Correct |
4 ms |
26972 KB |
Output is correct |
15 |
Correct |
5 ms |
29020 KB |
Output is correct |
16 |
Correct |
5 ms |
29020 KB |
Output is correct |
17 |
Correct |
5 ms |
27096 KB |
Output is correct |
18 |
Correct |
17 ms |
115280 KB |
Output is correct |
19 |
Correct |
17 ms |
115548 KB |
Output is correct |
20 |
Correct |
17 ms |
115412 KB |
Output is correct |
21 |
Correct |
16 ms |
115548 KB |
Output is correct |
22 |
Correct |
17 ms |
115508 KB |
Output is correct |
23 |
Correct |
17 ms |
115548 KB |
Output is correct |
24 |
Correct |
17 ms |
115548 KB |
Output is correct |
25 |
Correct |
17 ms |
115548 KB |
Output is correct |
26 |
Correct |
17 ms |
115548 KB |
Output is correct |
27 |
Correct |
17 ms |
115492 KB |
Output is correct |
28 |
Correct |
18 ms |
115800 KB |
Output is correct |
29 |
Correct |
17 ms |
115548 KB |
Output is correct |
30 |
Correct |
17 ms |
115548 KB |
Output is correct |
31 |
Correct |
17 ms |
115548 KB |
Output is correct |
32 |
Correct |
17 ms |
115484 KB |
Output is correct |
33 |
Correct |
17 ms |
113244 KB |
Output is correct |
34 |
Correct |
17 ms |
113496 KB |
Output is correct |
35 |
Correct |
16 ms |
111196 KB |
Output is correct |
36 |
Correct |
18 ms |
113232 KB |
Output is correct |
37 |
Correct |
17 ms |
109400 KB |
Output is correct |
38 |
Correct |
18 ms |
115544 KB |
Output is correct |
39 |
Correct |
17 ms |
115280 KB |
Output is correct |
40 |
Correct |
17 ms |
113332 KB |
Output is correct |
41 |
Correct |
17 ms |
115292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
311 ms |
761112 KB |
Output is correct |
3 |
Correct |
302 ms |
757584 KB |
Output is correct |
4 |
Correct |
310 ms |
760916 KB |
Output is correct |
5 |
Correct |
311 ms |
760968 KB |
Output is correct |
6 |
Correct |
303 ms |
760912 KB |
Output is correct |
7 |
Correct |
302 ms |
757880 KB |
Output is correct |
8 |
Correct |
297 ms |
757728 KB |
Output is correct |
9 |
Correct |
302 ms |
754376 KB |
Output is correct |
10 |
Correct |
300 ms |
761016 KB |
Output is correct |
11 |
Correct |
305 ms |
760912 KB |
Output is correct |
12 |
Correct |
75 ms |
299600 KB |
Output is correct |
13 |
Correct |
126 ms |
420176 KB |
Output is correct |
14 |
Correct |
185 ms |
553512 KB |
Output is correct |
15 |
Correct |
306 ms |
761212 KB |
Output is correct |
16 |
Correct |
301 ms |
761108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
5 ms |
29016 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
6 ms |
29260 KB |
Output is correct |
9 |
Correct |
6 ms |
29020 KB |
Output is correct |
10 |
Correct |
5 ms |
29032 KB |
Output is correct |
11 |
Correct |
5 ms |
29020 KB |
Output is correct |
12 |
Correct |
5 ms |
29020 KB |
Output is correct |
13 |
Correct |
4 ms |
29020 KB |
Output is correct |
14 |
Correct |
4 ms |
26972 KB |
Output is correct |
15 |
Correct |
5 ms |
29020 KB |
Output is correct |
16 |
Correct |
5 ms |
29020 KB |
Output is correct |
17 |
Correct |
5 ms |
27096 KB |
Output is correct |
18 |
Correct |
17 ms |
115280 KB |
Output is correct |
19 |
Correct |
17 ms |
115548 KB |
Output is correct |
20 |
Correct |
17 ms |
115412 KB |
Output is correct |
21 |
Correct |
16 ms |
115548 KB |
Output is correct |
22 |
Correct |
17 ms |
115508 KB |
Output is correct |
23 |
Correct |
17 ms |
115548 KB |
Output is correct |
24 |
Correct |
17 ms |
115548 KB |
Output is correct |
25 |
Correct |
17 ms |
115548 KB |
Output is correct |
26 |
Correct |
17 ms |
115548 KB |
Output is correct |
27 |
Correct |
17 ms |
115492 KB |
Output is correct |
28 |
Correct |
18 ms |
115800 KB |
Output is correct |
29 |
Correct |
17 ms |
115548 KB |
Output is correct |
30 |
Correct |
17 ms |
115548 KB |
Output is correct |
31 |
Correct |
17 ms |
115548 KB |
Output is correct |
32 |
Correct |
17 ms |
115484 KB |
Output is correct |
33 |
Correct |
17 ms |
113244 KB |
Output is correct |
34 |
Correct |
17 ms |
113496 KB |
Output is correct |
35 |
Correct |
16 ms |
111196 KB |
Output is correct |
36 |
Correct |
18 ms |
113232 KB |
Output is correct |
37 |
Correct |
17 ms |
109400 KB |
Output is correct |
38 |
Correct |
18 ms |
115544 KB |
Output is correct |
39 |
Correct |
17 ms |
115280 KB |
Output is correct |
40 |
Correct |
17 ms |
113332 KB |
Output is correct |
41 |
Correct |
17 ms |
115292 KB |
Output is correct |
42 |
Correct |
1 ms |
6488 KB |
Output is correct |
43 |
Correct |
311 ms |
761112 KB |
Output is correct |
44 |
Correct |
302 ms |
757584 KB |
Output is correct |
45 |
Correct |
310 ms |
760916 KB |
Output is correct |
46 |
Correct |
311 ms |
760968 KB |
Output is correct |
47 |
Correct |
303 ms |
760912 KB |
Output is correct |
48 |
Correct |
302 ms |
757880 KB |
Output is correct |
49 |
Correct |
297 ms |
757728 KB |
Output is correct |
50 |
Correct |
302 ms |
754376 KB |
Output is correct |
51 |
Correct |
300 ms |
761016 KB |
Output is correct |
52 |
Correct |
305 ms |
760912 KB |
Output is correct |
53 |
Correct |
75 ms |
299600 KB |
Output is correct |
54 |
Correct |
126 ms |
420176 KB |
Output is correct |
55 |
Correct |
185 ms |
553512 KB |
Output is correct |
56 |
Correct |
306 ms |
761212 KB |
Output is correct |
57 |
Correct |
301 ms |
761108 KB |
Output is correct |
58 |
Correct |
321 ms |
760944 KB |
Output is correct |
59 |
Correct |
319 ms |
761104 KB |
Output is correct |
60 |
Correct |
318 ms |
754768 KB |
Output is correct |
61 |
Correct |
316 ms |
757840 KB |
Output is correct |
62 |
Correct |
316 ms |
761076 KB |
Output is correct |
63 |
Correct |
308 ms |
760912 KB |
Output is correct |
64 |
Correct |
313 ms |
761096 KB |
Output is correct |
65 |
Correct |
318 ms |
757844 KB |
Output is correct |
66 |
Correct |
312 ms |
751184 KB |
Output is correct |
67 |
Correct |
317 ms |
761004 KB |
Output is correct |
68 |
Correct |
315 ms |
757844 KB |
Output is correct |
69 |
Correct |
317 ms |
761232 KB |
Output is correct |
70 |
Correct |
314 ms |
760912 KB |
Output is correct |
71 |
Correct |
319 ms |
757908 KB |
Output is correct |
72 |
Correct |
304 ms |
757812 KB |
Output is correct |
73 |
Correct |
300 ms |
760912 KB |
Output is correct |