#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 405;
int n;
string s;
int sf[3][N][N], dp[N][N][N][3];
int conv(char c) {
return c == 'R' ? 0 : (c == 'G' ? 1 : 2);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> s;
array<int, 3> cnt{};
array<vector<int>, 3> pos;
for (int i = 0; i < n; ++i) {
int c = conv(s[i]);
++cnt[c];
pos[c].push_back(i);
}
for (int i = 0; i < 3; ++i) {
for (int j = 1; j <= n; ++j) {
for (int k = cnt[i] - 1; k >= 0; --k) {
sf[i][j][k] = sf[i][j][k + 1] + (pos[i][k] < j);
}
}
}
memset(dp, 0x3f, sizeof(dp));
for (int j = 0; j < 3; ++j) {
dp[0][0][0][j] = 0;
}
for (int i = 0; i <= cnt[0]; ++i) {
for (int j = 0; j <= cnt[1]; ++j) {
for (int k = 0; k <= cnt[2]; ++k) {
for (int c = 0; c < 3; ++c) {
if (dp[i][j][k][c] < 1e9) {
auto cost = [&](int p) {
return sf[0][p][i] + sf[1][p][j] + sf[2][p][k];
};
if (c != 0 && i + 1 <= cnt[0]) {
dp[i + 1][j][k][0] = min(dp[i + 1][j][k][0], dp[i][j][k][c] + cost(pos[0][i]));
}
if (c != 1 && j + 1 <= cnt[1]) {
dp[i][j + 1][k][1] = min(dp[i][j + 1][k][1], dp[i][j][k][c] + cost(pos[1][j]));
}
if (c != 2 && k + 1 <= cnt[2]) {
dp[i][j][k + 1][2] = min(dp[i][j][k + 1][2], dp[i][j][k][c] + cost(pos[2][k]));
}
}
}
}
}
}
int res = *min_element(dp[cnt[0]][cnt[1]][cnt[2]], dp[cnt[0]][cnt[1]][cnt[2]] + 3);
cout << (res > 1e9 ? -1 : res);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
780400 KB |
Output is correct |
2 |
Correct |
308 ms |
780432 KB |
Output is correct |
3 |
Correct |
323 ms |
780396 KB |
Output is correct |
4 |
Correct |
326 ms |
780368 KB |
Output is correct |
5 |
Correct |
330 ms |
780372 KB |
Output is correct |
6 |
Correct |
316 ms |
780376 KB |
Output is correct |
7 |
Correct |
318 ms |
780372 KB |
Output is correct |
8 |
Correct |
310 ms |
780372 KB |
Output is correct |
9 |
Correct |
308 ms |
780372 KB |
Output is correct |
10 |
Correct |
303 ms |
780368 KB |
Output is correct |
11 |
Correct |
319 ms |
780336 KB |
Output is correct |
12 |
Correct |
345 ms |
780512 KB |
Output is correct |
13 |
Correct |
333 ms |
780368 KB |
Output is correct |
14 |
Correct |
343 ms |
780384 KB |
Output is correct |
15 |
Correct |
339 ms |
780296 KB |
Output is correct |
16 |
Correct |
336 ms |
780416 KB |
Output is correct |
17 |
Correct |
328 ms |
780296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
780400 KB |
Output is correct |
2 |
Correct |
308 ms |
780432 KB |
Output is correct |
3 |
Correct |
323 ms |
780396 KB |
Output is correct |
4 |
Correct |
326 ms |
780368 KB |
Output is correct |
5 |
Correct |
330 ms |
780372 KB |
Output is correct |
6 |
Correct |
316 ms |
780376 KB |
Output is correct |
7 |
Correct |
318 ms |
780372 KB |
Output is correct |
8 |
Correct |
310 ms |
780372 KB |
Output is correct |
9 |
Correct |
308 ms |
780372 KB |
Output is correct |
10 |
Correct |
303 ms |
780368 KB |
Output is correct |
11 |
Correct |
319 ms |
780336 KB |
Output is correct |
12 |
Correct |
345 ms |
780512 KB |
Output is correct |
13 |
Correct |
333 ms |
780368 KB |
Output is correct |
14 |
Correct |
343 ms |
780384 KB |
Output is correct |
15 |
Correct |
339 ms |
780296 KB |
Output is correct |
16 |
Correct |
336 ms |
780416 KB |
Output is correct |
17 |
Correct |
328 ms |
780296 KB |
Output is correct |
18 |
Correct |
334 ms |
780636 KB |
Output is correct |
19 |
Correct |
332 ms |
780636 KB |
Output is correct |
20 |
Correct |
337 ms |
780628 KB |
Output is correct |
21 |
Correct |
327 ms |
780624 KB |
Output is correct |
22 |
Correct |
346 ms |
780516 KB |
Output is correct |
23 |
Correct |
343 ms |
780548 KB |
Output is correct |
24 |
Correct |
325 ms |
780568 KB |
Output is correct |
25 |
Correct |
324 ms |
780628 KB |
Output is correct |
26 |
Correct |
339 ms |
780628 KB |
Output is correct |
27 |
Correct |
324 ms |
780636 KB |
Output is correct |
28 |
Correct |
327 ms |
780536 KB |
Output is correct |
29 |
Correct |
326 ms |
780624 KB |
Output is correct |
30 |
Correct |
324 ms |
780628 KB |
Output is correct |
31 |
Correct |
388 ms |
780628 KB |
Output is correct |
32 |
Correct |
324 ms |
780628 KB |
Output is correct |
33 |
Correct |
340 ms |
780728 KB |
Output is correct |
34 |
Correct |
365 ms |
780884 KB |
Output is correct |
35 |
Correct |
338 ms |
780520 KB |
Output is correct |
36 |
Correct |
326 ms |
780520 KB |
Output is correct |
37 |
Correct |
347 ms |
780704 KB |
Output is correct |
38 |
Correct |
330 ms |
780628 KB |
Output is correct |
39 |
Correct |
329 ms |
780548 KB |
Output is correct |
40 |
Correct |
339 ms |
780564 KB |
Output is correct |
41 |
Correct |
337 ms |
780516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
339 ms |
780368 KB |
Output is correct |
2 |
Correct |
348 ms |
781676 KB |
Output is correct |
3 |
Correct |
371 ms |
781704 KB |
Output is correct |
4 |
Correct |
336 ms |
781544 KB |
Output is correct |
5 |
Correct |
326 ms |
781644 KB |
Output is correct |
6 |
Correct |
326 ms |
781460 KB |
Output is correct |
7 |
Correct |
331 ms |
781648 KB |
Output is correct |
8 |
Correct |
340 ms |
781648 KB |
Output is correct |
9 |
Correct |
368 ms |
781648 KB |
Output is correct |
10 |
Correct |
320 ms |
781648 KB |
Output is correct |
11 |
Correct |
324 ms |
781472 KB |
Output is correct |
12 |
Correct |
331 ms |
780884 KB |
Output is correct |
13 |
Correct |
338 ms |
781140 KB |
Output is correct |
14 |
Correct |
318 ms |
781392 KB |
Output is correct |
15 |
Correct |
326 ms |
781672 KB |
Output is correct |
16 |
Correct |
320 ms |
781652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
780400 KB |
Output is correct |
2 |
Correct |
308 ms |
780432 KB |
Output is correct |
3 |
Correct |
323 ms |
780396 KB |
Output is correct |
4 |
Correct |
326 ms |
780368 KB |
Output is correct |
5 |
Correct |
330 ms |
780372 KB |
Output is correct |
6 |
Correct |
316 ms |
780376 KB |
Output is correct |
7 |
Correct |
318 ms |
780372 KB |
Output is correct |
8 |
Correct |
310 ms |
780372 KB |
Output is correct |
9 |
Correct |
308 ms |
780372 KB |
Output is correct |
10 |
Correct |
303 ms |
780368 KB |
Output is correct |
11 |
Correct |
319 ms |
780336 KB |
Output is correct |
12 |
Correct |
345 ms |
780512 KB |
Output is correct |
13 |
Correct |
333 ms |
780368 KB |
Output is correct |
14 |
Correct |
343 ms |
780384 KB |
Output is correct |
15 |
Correct |
339 ms |
780296 KB |
Output is correct |
16 |
Correct |
336 ms |
780416 KB |
Output is correct |
17 |
Correct |
328 ms |
780296 KB |
Output is correct |
18 |
Correct |
334 ms |
780636 KB |
Output is correct |
19 |
Correct |
332 ms |
780636 KB |
Output is correct |
20 |
Correct |
337 ms |
780628 KB |
Output is correct |
21 |
Correct |
327 ms |
780624 KB |
Output is correct |
22 |
Correct |
346 ms |
780516 KB |
Output is correct |
23 |
Correct |
343 ms |
780548 KB |
Output is correct |
24 |
Correct |
325 ms |
780568 KB |
Output is correct |
25 |
Correct |
324 ms |
780628 KB |
Output is correct |
26 |
Correct |
339 ms |
780628 KB |
Output is correct |
27 |
Correct |
324 ms |
780636 KB |
Output is correct |
28 |
Correct |
327 ms |
780536 KB |
Output is correct |
29 |
Correct |
326 ms |
780624 KB |
Output is correct |
30 |
Correct |
324 ms |
780628 KB |
Output is correct |
31 |
Correct |
388 ms |
780628 KB |
Output is correct |
32 |
Correct |
324 ms |
780628 KB |
Output is correct |
33 |
Correct |
340 ms |
780728 KB |
Output is correct |
34 |
Correct |
365 ms |
780884 KB |
Output is correct |
35 |
Correct |
338 ms |
780520 KB |
Output is correct |
36 |
Correct |
326 ms |
780520 KB |
Output is correct |
37 |
Correct |
347 ms |
780704 KB |
Output is correct |
38 |
Correct |
330 ms |
780628 KB |
Output is correct |
39 |
Correct |
329 ms |
780548 KB |
Output is correct |
40 |
Correct |
339 ms |
780564 KB |
Output is correct |
41 |
Correct |
337 ms |
780516 KB |
Output is correct |
42 |
Correct |
339 ms |
780368 KB |
Output is correct |
43 |
Correct |
348 ms |
781676 KB |
Output is correct |
44 |
Correct |
371 ms |
781704 KB |
Output is correct |
45 |
Correct |
336 ms |
781544 KB |
Output is correct |
46 |
Correct |
326 ms |
781644 KB |
Output is correct |
47 |
Correct |
326 ms |
781460 KB |
Output is correct |
48 |
Correct |
331 ms |
781648 KB |
Output is correct |
49 |
Correct |
340 ms |
781648 KB |
Output is correct |
50 |
Correct |
368 ms |
781648 KB |
Output is correct |
51 |
Correct |
320 ms |
781648 KB |
Output is correct |
52 |
Correct |
324 ms |
781472 KB |
Output is correct |
53 |
Correct |
331 ms |
780884 KB |
Output is correct |
54 |
Correct |
338 ms |
781140 KB |
Output is correct |
55 |
Correct |
318 ms |
781392 KB |
Output is correct |
56 |
Correct |
326 ms |
781672 KB |
Output is correct |
57 |
Correct |
320 ms |
781652 KB |
Output is correct |
58 |
Correct |
366 ms |
782164 KB |
Output is correct |
59 |
Correct |
343 ms |
782164 KB |
Output is correct |
60 |
Correct |
345 ms |
782164 KB |
Output is correct |
61 |
Correct |
348 ms |
782244 KB |
Output is correct |
62 |
Correct |
327 ms |
782160 KB |
Output is correct |
63 |
Correct |
323 ms |
782160 KB |
Output is correct |
64 |
Correct |
338 ms |
782160 KB |
Output is correct |
65 |
Correct |
378 ms |
782220 KB |
Output is correct |
66 |
Correct |
360 ms |
782164 KB |
Output is correct |
67 |
Correct |
351 ms |
782164 KB |
Output is correct |
68 |
Correct |
343 ms |
782256 KB |
Output is correct |
69 |
Correct |
352 ms |
782160 KB |
Output is correct |
70 |
Correct |
367 ms |
782168 KB |
Output is correct |
71 |
Correct |
351 ms |
782164 KB |
Output is correct |
72 |
Correct |
315 ms |
782272 KB |
Output is correct |
73 |
Correct |
346 ms |
782168 KB |
Output is correct |