// In the name of God
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 400 + 5;
int n;
string s;
int dp[N][N][N][3], cnt[N][3];
vector<int> vec[3];
void solve() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3 && i; j++)
cnt[i][j] = cnt[i - 1][j];
if (s[i] == 'R') {
cnt[i][0]++;
vec[0].pb(i);
}
else if (s[i] == 'G') {
cnt[i][1]++;
vec[1].pb(i);
}
else {
cnt[i][2]++;
vec[2].pb(i);
}
}
for (int r = 0; r <= cnt[n - 1][0]; r++) {
for (int g = 0; g <= cnt[n - 1][1]; g++) {
for (int y = 0; y <= cnt[n - 1][2]; y++) {
for (int i = 0; i < 3; i++) {
dp[r][g][y][i] = 1e9;
if ((i == 0 && r == 0) || (i == 1 && g == 0) || (i == 2 && y == 0)) {
if (r == 0 && g == 0 && y == 0)
dp[r][g][y][i] = 0;
continue;
}
int r2 = r - (i == 0), g2 = g - (i == 1), y2 = y - (i == 2);
int k = vec[i][(i == 0 ? r : (i == 1 ? g : y)) - 1];
int d = (i != 0) * max(0, cnt[k][0] - r) + (i != 1) * max(0, cnt[k][1] - g) + (i != 2) * max(0, cnt[k][2] - y);
for (int j = 0; j < 3; j++) {
if (i == j)
continue;
dp[r][g][y][i] = min(dp[r][g][y][i], dp[r2][g2][y2][j] + d);
}
}
}
}
}
int ans = 1e9;
for (int i = 0; i < 3; i++)
ans = min(ans, dp[cnt[n - 1][0]][cnt[n - 1][1]][cnt[n - 1][2]][i]);
cout << (ans < 1e9 ? ans : -1) << endl;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tc = 1; // cin >> tc;
while (tc--) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2504 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8536 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2504 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8536 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
6 ms |
45660 KB |
Output is correct |
19 |
Correct |
4 ms |
37468 KB |
Output is correct |
20 |
Correct |
5 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
39492 KB |
Output is correct |
22 |
Correct |
5 ms |
47708 KB |
Output is correct |
23 |
Correct |
4 ms |
33372 KB |
Output is correct |
24 |
Correct |
3 ms |
21084 KB |
Output is correct |
25 |
Correct |
12 ms |
72140 KB |
Output is correct |
26 |
Correct |
7 ms |
62296 KB |
Output is correct |
27 |
Correct |
6 ms |
53852 KB |
Output is correct |
28 |
Correct |
5 ms |
41564 KB |
Output is correct |
29 |
Correct |
6 ms |
43612 KB |
Output is correct |
30 |
Correct |
4 ms |
33224 KB |
Output is correct |
31 |
Correct |
4 ms |
31324 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
5 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43480 KB |
Output is correct |
35 |
Correct |
5 ms |
45656 KB |
Output is correct |
36 |
Correct |
4 ms |
39516 KB |
Output is correct |
37 |
Correct |
3 ms |
27228 KB |
Output is correct |
38 |
Correct |
3 ms |
29276 KB |
Output is correct |
39 |
Correct |
4 ms |
37464 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
90 ms |
379476 KB |
Output is correct |
3 |
Correct |
39 ms |
379580 KB |
Output is correct |
4 |
Correct |
39 ms |
382032 KB |
Output is correct |
5 |
Correct |
38 ms |
381780 KB |
Output is correct |
6 |
Correct |
41 ms |
381964 KB |
Output is correct |
7 |
Correct |
42 ms |
381132 KB |
Output is correct |
8 |
Correct |
39 ms |
381008 KB |
Output is correct |
9 |
Correct |
39 ms |
379820 KB |
Output is correct |
10 |
Correct |
38 ms |
379472 KB |
Output is correct |
11 |
Correct |
39 ms |
379508 KB |
Output is correct |
12 |
Correct |
19 ms |
199412 KB |
Output is correct |
13 |
Correct |
26 ms |
265300 KB |
Output is correct |
14 |
Correct |
32 ms |
320596 KB |
Output is correct |
15 |
Correct |
38 ms |
378708 KB |
Output is correct |
16 |
Correct |
39 ms |
378704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2504 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8536 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
6 ms |
45660 KB |
Output is correct |
19 |
Correct |
4 ms |
37468 KB |
Output is correct |
20 |
Correct |
5 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
39492 KB |
Output is correct |
22 |
Correct |
5 ms |
47708 KB |
Output is correct |
23 |
Correct |
4 ms |
33372 KB |
Output is correct |
24 |
Correct |
3 ms |
21084 KB |
Output is correct |
25 |
Correct |
12 ms |
72140 KB |
Output is correct |
26 |
Correct |
7 ms |
62296 KB |
Output is correct |
27 |
Correct |
6 ms |
53852 KB |
Output is correct |
28 |
Correct |
5 ms |
41564 KB |
Output is correct |
29 |
Correct |
6 ms |
43612 KB |
Output is correct |
30 |
Correct |
4 ms |
33224 KB |
Output is correct |
31 |
Correct |
4 ms |
31324 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
5 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43480 KB |
Output is correct |
35 |
Correct |
5 ms |
45656 KB |
Output is correct |
36 |
Correct |
4 ms |
39516 KB |
Output is correct |
37 |
Correct |
3 ms |
27228 KB |
Output is correct |
38 |
Correct |
3 ms |
29276 KB |
Output is correct |
39 |
Correct |
4 ms |
37464 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
19036 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
90 ms |
379476 KB |
Output is correct |
44 |
Correct |
39 ms |
379580 KB |
Output is correct |
45 |
Correct |
39 ms |
382032 KB |
Output is correct |
46 |
Correct |
38 ms |
381780 KB |
Output is correct |
47 |
Correct |
41 ms |
381964 KB |
Output is correct |
48 |
Correct |
42 ms |
381132 KB |
Output is correct |
49 |
Correct |
39 ms |
381008 KB |
Output is correct |
50 |
Correct |
39 ms |
379820 KB |
Output is correct |
51 |
Correct |
38 ms |
379472 KB |
Output is correct |
52 |
Correct |
39 ms |
379508 KB |
Output is correct |
53 |
Correct |
19 ms |
199412 KB |
Output is correct |
54 |
Correct |
26 ms |
265300 KB |
Output is correct |
55 |
Correct |
32 ms |
320596 KB |
Output is correct |
56 |
Correct |
38 ms |
378708 KB |
Output is correct |
57 |
Correct |
39 ms |
378704 KB |
Output is correct |
58 |
Correct |
103 ms |
238416 KB |
Output is correct |
59 |
Correct |
94 ms |
279632 KB |
Output is correct |
60 |
Correct |
98 ms |
275536 KB |
Output is correct |
61 |
Correct |
92 ms |
259152 KB |
Output is correct |
62 |
Correct |
49 ms |
390740 KB |
Output is correct |
63 |
Correct |
47 ms |
375636 KB |
Output is correct |
64 |
Correct |
69 ms |
361556 KB |
Output is correct |
65 |
Correct |
87 ms |
308364 KB |
Output is correct |
66 |
Correct |
93 ms |
263248 KB |
Output is correct |
67 |
Correct |
87 ms |
218056 KB |
Output is correct |
68 |
Correct |
93 ms |
263252 KB |
Output is correct |
69 |
Correct |
97 ms |
263248 KB |
Output is correct |
70 |
Correct |
100 ms |
261204 KB |
Output is correct |
71 |
Correct |
98 ms |
291924 KB |
Output is correct |
72 |
Correct |
60 ms |
424784 KB |
Output is correct |
73 |
Correct |
5 ms |
8792 KB |
Output is correct |