#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];
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 |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
324 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 |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1360 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 |
1408 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 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 |
1248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
324 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 |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1360 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 |
1408 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 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 |
1248 KB |
Output is correct |
18 |
Correct |
7 ms |
18004 KB |
Output is correct |
19 |
Correct |
7 ms |
18004 KB |
Output is correct |
20 |
Correct |
8 ms |
18004 KB |
Output is correct |
21 |
Correct |
9 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 |
18004 KB |
Output is correct |
25 |
Correct |
7 ms |
18004 KB |
Output is correct |
26 |
Correct |
7 ms |
18004 KB |
Output is correct |
27 |
Correct |
7 ms |
18008 KB |
Output is correct |
28 |
Correct |
7 ms |
18004 KB |
Output is correct |
29 |
Correct |
7 ms |
18004 KB |
Output is correct |
30 |
Correct |
8 ms |
18004 KB |
Output is correct |
31 |
Correct |
8 ms |
17944 KB |
Output is correct |
32 |
Correct |
7 ms |
18024 KB |
Output is correct |
33 |
Correct |
7 ms |
17364 KB |
Output is correct |
34 |
Correct |
7 ms |
17364 KB |
Output is correct |
35 |
Correct |
7 ms |
16852 KB |
Output is correct |
36 |
Correct |
7 ms |
17352 KB |
Output is correct |
37 |
Correct |
7 ms |
16180 KB |
Output is correct |
38 |
Correct |
7 ms |
17996 KB |
Output is correct |
39 |
Correct |
8 ms |
18004 KB |
Output is correct |
40 |
Correct |
8 ms |
17356 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 |
356 ms |
766504 KB |
Output is correct |
3 |
Correct |
294 ms |
762684 KB |
Output is correct |
4 |
Correct |
304 ms |
766588 KB |
Output is correct |
5 |
Correct |
292 ms |
766556 KB |
Output is correct |
6 |
Correct |
295 ms |
766552 KB |
Output is correct |
7 |
Correct |
293 ms |
762652 KB |
Output is correct |
8 |
Correct |
304 ms |
762720 KB |
Output is correct |
9 |
Correct |
290 ms |
758976 KB |
Output is correct |
10 |
Correct |
287 ms |
766472 KB |
Output is correct |
11 |
Correct |
295 ms |
766456 KB |
Output is correct |
12 |
Correct |
86 ms |
204756 KB |
Output is correct |
13 |
Correct |
140 ms |
360852 KB |
Output is correct |
14 |
Correct |
197 ms |
522552 KB |
Output is correct |
15 |
Correct |
298 ms |
766544 KB |
Output is correct |
16 |
Correct |
293 ms |
766496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
324 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 |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1360 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 |
1408 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 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 |
1248 KB |
Output is correct |
18 |
Correct |
7 ms |
18004 KB |
Output is correct |
19 |
Correct |
7 ms |
18004 KB |
Output is correct |
20 |
Correct |
8 ms |
18004 KB |
Output is correct |
21 |
Correct |
9 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 |
18004 KB |
Output is correct |
25 |
Correct |
7 ms |
18004 KB |
Output is correct |
26 |
Correct |
7 ms |
18004 KB |
Output is correct |
27 |
Correct |
7 ms |
18008 KB |
Output is correct |
28 |
Correct |
7 ms |
18004 KB |
Output is correct |
29 |
Correct |
7 ms |
18004 KB |
Output is correct |
30 |
Correct |
8 ms |
18004 KB |
Output is correct |
31 |
Correct |
8 ms |
17944 KB |
Output is correct |
32 |
Correct |
7 ms |
18024 KB |
Output is correct |
33 |
Correct |
7 ms |
17364 KB |
Output is correct |
34 |
Correct |
7 ms |
17364 KB |
Output is correct |
35 |
Correct |
7 ms |
16852 KB |
Output is correct |
36 |
Correct |
7 ms |
17352 KB |
Output is correct |
37 |
Correct |
7 ms |
16180 KB |
Output is correct |
38 |
Correct |
7 ms |
17996 KB |
Output is correct |
39 |
Correct |
8 ms |
18004 KB |
Output is correct |
40 |
Correct |
8 ms |
17356 KB |
Output is correct |
41 |
Correct |
7 ms |
18004 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
356 ms |
766504 KB |
Output is correct |
44 |
Correct |
294 ms |
762684 KB |
Output is correct |
45 |
Correct |
304 ms |
766588 KB |
Output is correct |
46 |
Correct |
292 ms |
766556 KB |
Output is correct |
47 |
Correct |
295 ms |
766552 KB |
Output is correct |
48 |
Correct |
293 ms |
762652 KB |
Output is correct |
49 |
Correct |
304 ms |
762720 KB |
Output is correct |
50 |
Correct |
290 ms |
758976 KB |
Output is correct |
51 |
Correct |
287 ms |
766472 KB |
Output is correct |
52 |
Correct |
295 ms |
766456 KB |
Output is correct |
53 |
Correct |
86 ms |
204756 KB |
Output is correct |
54 |
Correct |
140 ms |
360852 KB |
Output is correct |
55 |
Correct |
197 ms |
522552 KB |
Output is correct |
56 |
Correct |
298 ms |
766544 KB |
Output is correct |
57 |
Correct |
293 ms |
766496 KB |
Output is correct |
58 |
Correct |
312 ms |
766644 KB |
Output is correct |
59 |
Correct |
306 ms |
766520 KB |
Output is correct |
60 |
Correct |
305 ms |
758924 KB |
Output is correct |
61 |
Correct |
307 ms |
762784 KB |
Output is correct |
62 |
Correct |
299 ms |
766540 KB |
Output is correct |
63 |
Correct |
300 ms |
766580 KB |
Output is correct |
64 |
Correct |
306 ms |
766668 KB |
Output is correct |
65 |
Correct |
309 ms |
762748 KB |
Output is correct |
66 |
Correct |
323 ms |
755156 KB |
Output is correct |
67 |
Correct |
316 ms |
766472 KB |
Output is correct |
68 |
Correct |
313 ms |
762760 KB |
Output is correct |
69 |
Correct |
305 ms |
766504 KB |
Output is correct |
70 |
Correct |
353 ms |
766616 KB |
Output is correct |
71 |
Correct |
305 ms |
762676 KB |
Output is correct |
72 |
Correct |
301 ms |
762704 KB |
Output is correct |
73 |
Correct |
290 ms |
766516 KB |
Output is correct |