#include <bits/stdc++.h>
using namespace std;
const int MAXN = 404;
int dp[MAXN][MAXN][MAXN][3], pos[3][MAXN], v[MAXN], cnt[3], pref[3][MAXN];
signed main() {
int n;
string s;
cin >> n >> s;
memset(dp, '?', sizeof dp), memset(pos, 0, sizeof pos), memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= n; ++i) {
v[i] = s[i - 1] == 'R' ? 0 : s[i - 1] == 'G' ? 1 : 2;
pref[0][i] = pref[0][i - 1] + (v[i] == 0);
pref[1][i] = pref[1][i - 1] + (v[i] == 1);
pref[2][i] = pref[2][i - 1] + (v[i] == 2);
pos[v[i]][++cnt[v[i]]] = i;
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int red = 0; red <= cnt[0]; ++red) {
for (int green = 0; green <= cnt[1]; ++green) {
for (int blue = 0; blue <= cnt[2]; ++blue) {
if (red) dp[red][green][blue][0] = min(dp[red - 1][green][blue][1], dp[red - 1][green][blue][2]) + max(0, pref[1][pos[0][red]] - green) + max(0, pref[2][pos[0][red]] - blue);
if (green) dp[red][green][blue][1] = min(dp[red][green - 1][blue][0], dp[red][green - 1][blue][2]) + max(0, pref[0][pos[1][green]] - red) + max(0, pref[2][pos[1][green]] - blue);
if (blue) dp[red][green][blue][2] = min(dp[red][green][blue - 1][0], dp[red][green][blue - 1][1]) + max(0, pref[0][pos[2][blue]] - red) + max(0, pref[1][pos[2][blue]] - green);
}
}
}
int ans = *min_element(dp[cnt[0]][cnt[1]][cnt[2]], dp[cnt[0]][cnt[1]][cnt[2]] + 3);
if (ans > n * n) cout << -1;
else cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
774720 KB |
Output is correct |
2 |
Correct |
227 ms |
774484 KB |
Output is correct |
3 |
Correct |
375 ms |
774500 KB |
Output is correct |
4 |
Correct |
236 ms |
774432 KB |
Output is correct |
5 |
Correct |
224 ms |
774592 KB |
Output is correct |
6 |
Correct |
225 ms |
774480 KB |
Output is correct |
7 |
Correct |
225 ms |
774596 KB |
Output is correct |
8 |
Correct |
242 ms |
774484 KB |
Output is correct |
9 |
Correct |
232 ms |
774632 KB |
Output is correct |
10 |
Correct |
227 ms |
774572 KB |
Output is correct |
11 |
Correct |
237 ms |
774484 KB |
Output is correct |
12 |
Correct |
229 ms |
774448 KB |
Output is correct |
13 |
Correct |
228 ms |
774556 KB |
Output is correct |
14 |
Correct |
229 ms |
774472 KB |
Output is correct |
15 |
Correct |
236 ms |
774464 KB |
Output is correct |
16 |
Correct |
226 ms |
774468 KB |
Output is correct |
17 |
Correct |
226 ms |
774424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
774720 KB |
Output is correct |
2 |
Correct |
227 ms |
774484 KB |
Output is correct |
3 |
Correct |
375 ms |
774500 KB |
Output is correct |
4 |
Correct |
236 ms |
774432 KB |
Output is correct |
5 |
Correct |
224 ms |
774592 KB |
Output is correct |
6 |
Correct |
225 ms |
774480 KB |
Output is correct |
7 |
Correct |
225 ms |
774596 KB |
Output is correct |
8 |
Correct |
242 ms |
774484 KB |
Output is correct |
9 |
Correct |
232 ms |
774632 KB |
Output is correct |
10 |
Correct |
227 ms |
774572 KB |
Output is correct |
11 |
Correct |
237 ms |
774484 KB |
Output is correct |
12 |
Correct |
229 ms |
774448 KB |
Output is correct |
13 |
Correct |
228 ms |
774556 KB |
Output is correct |
14 |
Correct |
229 ms |
774472 KB |
Output is correct |
15 |
Correct |
236 ms |
774464 KB |
Output is correct |
16 |
Correct |
226 ms |
774468 KB |
Output is correct |
17 |
Correct |
226 ms |
774424 KB |
Output is correct |
18 |
Correct |
238 ms |
774488 KB |
Output is correct |
19 |
Correct |
235 ms |
774520 KB |
Output is correct |
20 |
Correct |
225 ms |
774548 KB |
Output is correct |
21 |
Correct |
244 ms |
774600 KB |
Output is correct |
22 |
Correct |
227 ms |
774436 KB |
Output is correct |
23 |
Correct |
227 ms |
774652 KB |
Output is correct |
24 |
Correct |
230 ms |
774520 KB |
Output is correct |
25 |
Correct |
219 ms |
774480 KB |
Output is correct |
26 |
Correct |
228 ms |
774740 KB |
Output is correct |
27 |
Correct |
236 ms |
774484 KB |
Output is correct |
28 |
Correct |
224 ms |
774480 KB |
Output is correct |
29 |
Correct |
227 ms |
774556 KB |
Output is correct |
30 |
Correct |
224 ms |
774484 KB |
Output is correct |
31 |
Correct |
227 ms |
774480 KB |
Output is correct |
32 |
Correct |
223 ms |
774520 KB |
Output is correct |
33 |
Correct |
222 ms |
774504 KB |
Output is correct |
34 |
Correct |
236 ms |
774584 KB |
Output is correct |
35 |
Correct |
223 ms |
774472 KB |
Output is correct |
36 |
Correct |
225 ms |
774492 KB |
Output is correct |
37 |
Correct |
224 ms |
774464 KB |
Output is correct |
38 |
Correct |
225 ms |
774416 KB |
Output is correct |
39 |
Correct |
228 ms |
774620 KB |
Output is correct |
40 |
Correct |
224 ms |
774484 KB |
Output is correct |
41 |
Correct |
224 ms |
774640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
229 ms |
774484 KB |
Output is correct |
2 |
Correct |
223 ms |
774628 KB |
Output is correct |
3 |
Correct |
224 ms |
774484 KB |
Output is correct |
4 |
Correct |
228 ms |
774808 KB |
Output is correct |
5 |
Correct |
226 ms |
774484 KB |
Output is correct |
6 |
Correct |
227 ms |
774484 KB |
Output is correct |
7 |
Correct |
229 ms |
774752 KB |
Output is correct |
8 |
Correct |
227 ms |
774484 KB |
Output is correct |
9 |
Correct |
226 ms |
774656 KB |
Output is correct |
10 |
Correct |
229 ms |
774480 KB |
Output is correct |
11 |
Correct |
227 ms |
774484 KB |
Output is correct |
12 |
Correct |
229 ms |
774544 KB |
Output is correct |
13 |
Correct |
238 ms |
774588 KB |
Output is correct |
14 |
Correct |
223 ms |
774680 KB |
Output is correct |
15 |
Correct |
232 ms |
774512 KB |
Output is correct |
16 |
Correct |
227 ms |
774480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
774720 KB |
Output is correct |
2 |
Correct |
227 ms |
774484 KB |
Output is correct |
3 |
Correct |
375 ms |
774500 KB |
Output is correct |
4 |
Correct |
236 ms |
774432 KB |
Output is correct |
5 |
Correct |
224 ms |
774592 KB |
Output is correct |
6 |
Correct |
225 ms |
774480 KB |
Output is correct |
7 |
Correct |
225 ms |
774596 KB |
Output is correct |
8 |
Correct |
242 ms |
774484 KB |
Output is correct |
9 |
Correct |
232 ms |
774632 KB |
Output is correct |
10 |
Correct |
227 ms |
774572 KB |
Output is correct |
11 |
Correct |
237 ms |
774484 KB |
Output is correct |
12 |
Correct |
229 ms |
774448 KB |
Output is correct |
13 |
Correct |
228 ms |
774556 KB |
Output is correct |
14 |
Correct |
229 ms |
774472 KB |
Output is correct |
15 |
Correct |
236 ms |
774464 KB |
Output is correct |
16 |
Correct |
226 ms |
774468 KB |
Output is correct |
17 |
Correct |
226 ms |
774424 KB |
Output is correct |
18 |
Correct |
238 ms |
774488 KB |
Output is correct |
19 |
Correct |
235 ms |
774520 KB |
Output is correct |
20 |
Correct |
225 ms |
774548 KB |
Output is correct |
21 |
Correct |
244 ms |
774600 KB |
Output is correct |
22 |
Correct |
227 ms |
774436 KB |
Output is correct |
23 |
Correct |
227 ms |
774652 KB |
Output is correct |
24 |
Correct |
230 ms |
774520 KB |
Output is correct |
25 |
Correct |
219 ms |
774480 KB |
Output is correct |
26 |
Correct |
228 ms |
774740 KB |
Output is correct |
27 |
Correct |
236 ms |
774484 KB |
Output is correct |
28 |
Correct |
224 ms |
774480 KB |
Output is correct |
29 |
Correct |
227 ms |
774556 KB |
Output is correct |
30 |
Correct |
224 ms |
774484 KB |
Output is correct |
31 |
Correct |
227 ms |
774480 KB |
Output is correct |
32 |
Correct |
223 ms |
774520 KB |
Output is correct |
33 |
Correct |
222 ms |
774504 KB |
Output is correct |
34 |
Correct |
236 ms |
774584 KB |
Output is correct |
35 |
Correct |
223 ms |
774472 KB |
Output is correct |
36 |
Correct |
225 ms |
774492 KB |
Output is correct |
37 |
Correct |
224 ms |
774464 KB |
Output is correct |
38 |
Correct |
225 ms |
774416 KB |
Output is correct |
39 |
Correct |
228 ms |
774620 KB |
Output is correct |
40 |
Correct |
224 ms |
774484 KB |
Output is correct |
41 |
Correct |
224 ms |
774640 KB |
Output is correct |
42 |
Correct |
229 ms |
774484 KB |
Output is correct |
43 |
Correct |
223 ms |
774628 KB |
Output is correct |
44 |
Correct |
224 ms |
774484 KB |
Output is correct |
45 |
Correct |
228 ms |
774808 KB |
Output is correct |
46 |
Correct |
226 ms |
774484 KB |
Output is correct |
47 |
Correct |
227 ms |
774484 KB |
Output is correct |
48 |
Correct |
229 ms |
774752 KB |
Output is correct |
49 |
Correct |
227 ms |
774484 KB |
Output is correct |
50 |
Correct |
226 ms |
774656 KB |
Output is correct |
51 |
Correct |
229 ms |
774480 KB |
Output is correct |
52 |
Correct |
227 ms |
774484 KB |
Output is correct |
53 |
Correct |
229 ms |
774544 KB |
Output is correct |
54 |
Correct |
238 ms |
774588 KB |
Output is correct |
55 |
Correct |
223 ms |
774680 KB |
Output is correct |
56 |
Correct |
232 ms |
774512 KB |
Output is correct |
57 |
Correct |
227 ms |
774480 KB |
Output is correct |
58 |
Correct |
244 ms |
774444 KB |
Output is correct |
59 |
Correct |
251 ms |
774660 KB |
Output is correct |
60 |
Correct |
239 ms |
774556 KB |
Output is correct |
61 |
Correct |
243 ms |
774736 KB |
Output is correct |
62 |
Correct |
231 ms |
774480 KB |
Output is correct |
63 |
Correct |
234 ms |
774588 KB |
Output is correct |
64 |
Correct |
237 ms |
774480 KB |
Output is correct |
65 |
Correct |
256 ms |
774656 KB |
Output is correct |
66 |
Correct |
244 ms |
774996 KB |
Output is correct |
67 |
Correct |
241 ms |
774664 KB |
Output is correct |
68 |
Correct |
239 ms |
774544 KB |
Output is correct |
69 |
Correct |
240 ms |
774672 KB |
Output is correct |
70 |
Correct |
241 ms |
774480 KB |
Output is correct |
71 |
Correct |
239 ms |
774664 KB |
Output is correct |
72 |
Correct |
230 ms |
774536 KB |
Output is correct |
73 |
Correct |
223 ms |
774604 KB |
Output is correct |