#include <bits/stdc++.h>
using namespace std;
const int maxn = 405;
const int inf = 1e9;
int n;
string s;
vector<int> pos[3];
int preffix[maxn][3];
int dp[maxn][maxn][maxn][3];
int decode(char c)
{
if (c == 'R')
{
return 0;
}
if (c == 'G')
{
return 1;
}
return 2;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n >> s;
s = '#' + s;
for (int i = 1; i <= n; ++i)
{
int curr = decode(s[i]);
for (int j = 0; j < 3; ++j)
{
preffix[i][j] = preffix[i - 1][j];
}
++preffix[i][curr];
pos[curr].push_back(i);
}
for (int i = 0; i <= n; ++i)
{
for (int j = 0; j <= n; ++j)
{
for (int k = 0; k <= n; ++k)
{
for (int p = 0; p <= 2; ++p)
{
dp[i][j][k][p] = inf;
}
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
int cnt0 = preffix[n][0];
int cnt1 = preffix[n][1];
int cnt2 = preffix[n][2];
if ((1LL * cnt0) * (1LL * cnt1) * (1LL * cnt2) > 1e7)
{
return 1;
}
for (int i = 0; i <= cnt0; ++i)
{
for (int j = 0; j <= cnt1; ++j)
{
for (int k = 0; k <= cnt2; ++k)
{
if (i != 0)
{
int curr_min = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]);
int cost = max(0, preffix[pos[0][i - 1]][1] - j) + max(0, preffix[pos[0][i - 1]][2] - k);
dp[i][j][k][0] = min(dp[i][j][k][0], curr_min + cost);
}
if (j != 0)
{
int curr_min = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]);
int cost = max(0, preffix[pos[1][j - 1]][0] - i) + max(0, preffix[pos[1][j - 1]][2] - k);
dp[i][j][k][1] = min(dp[i][j][k][1], curr_min + cost);
}
if (k != 0)
{
int curr_min = min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]);
int cost = max(0, preffix[pos[2][k - 1]][0] - i) + max(0, preffix[pos[2][k - 1]][1] - j);
dp[i][j][k][2] = min(dp[i][j][k][2], curr_min + cost);
}
}
}
}
int ans = min(dp[cnt0][cnt1][cnt2][0], min(dp[cnt0][cnt1][cnt2][1], dp[cnt0][cnt1][cnt2][2]));
if (ans == inf)
{
cout << -1 << endl;
}
else
{
cout << ans << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1352 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1332 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1352 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1332 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
8 ms |
18004 KB |
Output is correct |
19 |
Correct |
7 ms |
18052 KB |
Output is correct |
20 |
Correct |
7 ms |
18004 KB |
Output is correct |
21 |
Correct |
7 ms |
18004 KB |
Output is correct |
22 |
Correct |
7 ms |
18004 KB |
Output is correct |
23 |
Correct |
8 ms |
18004 KB |
Output is correct |
24 |
Correct |
8 ms |
17988 KB |
Output is correct |
25 |
Correct |
9 ms |
17960 KB |
Output is correct |
26 |
Correct |
7 ms |
17948 KB |
Output is correct |
27 |
Correct |
7 ms |
18004 KB |
Output is correct |
28 |
Correct |
7 ms |
17964 KB |
Output is correct |
29 |
Correct |
9 ms |
18004 KB |
Output is correct |
30 |
Correct |
7 ms |
17992 KB |
Output is correct |
31 |
Correct |
8 ms |
18000 KB |
Output is correct |
32 |
Correct |
8 ms |
18004 KB |
Output is correct |
33 |
Correct |
9 ms |
17484 KB |
Output is correct |
34 |
Correct |
7 ms |
17428 KB |
Output is correct |
35 |
Correct |
7 ms |
16768 KB |
Output is correct |
36 |
Correct |
8 ms |
17364 KB |
Output is correct |
37 |
Correct |
6 ms |
16240 KB |
Output is correct |
38 |
Correct |
7 ms |
18004 KB |
Output is correct |
39 |
Correct |
7 ms |
17988 KB |
Output is correct |
40 |
Correct |
7 ms |
17432 KB |
Output is correct |
41 |
Correct |
7 ms |
18004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
364 ms |
766532 KB |
Output is correct |
3 |
Correct |
312 ms |
762672 KB |
Output is correct |
4 |
Correct |
311 ms |
766460 KB |
Output is correct |
5 |
Correct |
308 ms |
766552 KB |
Output is correct |
6 |
Correct |
327 ms |
766472 KB |
Output is correct |
7 |
Correct |
313 ms |
762724 KB |
Output is correct |
8 |
Correct |
326 ms |
762772 KB |
Output is correct |
9 |
Correct |
311 ms |
758872 KB |
Output is correct |
10 |
Correct |
311 ms |
766476 KB |
Output is correct |
11 |
Correct |
318 ms |
766476 KB |
Output is correct |
12 |
Correct |
88 ms |
204744 KB |
Output is correct |
13 |
Correct |
156 ms |
361016 KB |
Output is correct |
14 |
Correct |
220 ms |
522544 KB |
Output is correct |
15 |
Correct |
310 ms |
766520 KB |
Output is correct |
16 |
Correct |
307 ms |
766476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1352 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1332 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
8 ms |
18004 KB |
Output is correct |
19 |
Correct |
7 ms |
18052 KB |
Output is correct |
20 |
Correct |
7 ms |
18004 KB |
Output is correct |
21 |
Correct |
7 ms |
18004 KB |
Output is correct |
22 |
Correct |
7 ms |
18004 KB |
Output is correct |
23 |
Correct |
8 ms |
18004 KB |
Output is correct |
24 |
Correct |
8 ms |
17988 KB |
Output is correct |
25 |
Correct |
9 ms |
17960 KB |
Output is correct |
26 |
Correct |
7 ms |
17948 KB |
Output is correct |
27 |
Correct |
7 ms |
18004 KB |
Output is correct |
28 |
Correct |
7 ms |
17964 KB |
Output is correct |
29 |
Correct |
9 ms |
18004 KB |
Output is correct |
30 |
Correct |
7 ms |
17992 KB |
Output is correct |
31 |
Correct |
8 ms |
18000 KB |
Output is correct |
32 |
Correct |
8 ms |
18004 KB |
Output is correct |
33 |
Correct |
9 ms |
17484 KB |
Output is correct |
34 |
Correct |
7 ms |
17428 KB |
Output is correct |
35 |
Correct |
7 ms |
16768 KB |
Output is correct |
36 |
Correct |
8 ms |
17364 KB |
Output is correct |
37 |
Correct |
6 ms |
16240 KB |
Output is correct |
38 |
Correct |
7 ms |
18004 KB |
Output is correct |
39 |
Correct |
7 ms |
17988 KB |
Output is correct |
40 |
Correct |
7 ms |
17432 KB |
Output is correct |
41 |
Correct |
7 ms |
18004 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
364 ms |
766532 KB |
Output is correct |
44 |
Correct |
312 ms |
762672 KB |
Output is correct |
45 |
Correct |
311 ms |
766460 KB |
Output is correct |
46 |
Correct |
308 ms |
766552 KB |
Output is correct |
47 |
Correct |
327 ms |
766472 KB |
Output is correct |
48 |
Correct |
313 ms |
762724 KB |
Output is correct |
49 |
Correct |
326 ms |
762772 KB |
Output is correct |
50 |
Correct |
311 ms |
758872 KB |
Output is correct |
51 |
Correct |
311 ms |
766476 KB |
Output is correct |
52 |
Correct |
318 ms |
766476 KB |
Output is correct |
53 |
Correct |
88 ms |
204744 KB |
Output is correct |
54 |
Correct |
156 ms |
361016 KB |
Output is correct |
55 |
Correct |
220 ms |
522544 KB |
Output is correct |
56 |
Correct |
310 ms |
766520 KB |
Output is correct |
57 |
Correct |
307 ms |
766476 KB |
Output is correct |
58 |
Correct |
323 ms |
766576 KB |
Output is correct |
59 |
Correct |
347 ms |
766616 KB |
Output is correct |
60 |
Correct |
324 ms |
758868 KB |
Output is correct |
61 |
Correct |
328 ms |
762864 KB |
Output is correct |
62 |
Correct |
316 ms |
766564 KB |
Output is correct |
63 |
Correct |
312 ms |
766480 KB |
Output is correct |
64 |
Correct |
329 ms |
766572 KB |
Output is correct |
65 |
Correct |
337 ms |
762752 KB |
Output is correct |
66 |
Correct |
325 ms |
755172 KB |
Output is correct |
67 |
Correct |
320 ms |
766532 KB |
Output is correct |
68 |
Correct |
327 ms |
762768 KB |
Output is correct |
69 |
Correct |
333 ms |
766592 KB |
Output is correct |
70 |
Correct |
371 ms |
766564 KB |
Output is correct |
71 |
Correct |
340 ms |
762668 KB |
Output is correct |
72 |
Correct |
309 ms |
762736 KB |
Output is correct |
73 |
Correct |
305 ms |
766868 KB |
Output is correct |