#include <bits/stdc++.h>
using namespace std;
const int DIM = 405;
const int INF = 405 * 405;
char str[DIM];
int dp[DIM][DIM][DIM][3], idx[3];
int tot[3], psm[DIM][3], lst[DIM][3];
inline void f(int &x, int y) {
if (x >= y) { x = y; } }
int main(void) {
#ifdef HOME
freopen("vegetables.in", "r", stdin);
freopen("vegetables.out", "w", stdout);
#endif
int n; cin >> n >> (str + 1);
for (int i = 1; i <= n; ++i) {
char ch = str[i];
if (ch == 'R') { lst[++tot[0]][0] = i; ++psm[i][0]; }
if (ch == 'G') { lst[++tot[1]][1] = i; ++psm[i][1]; }
if (ch == 'Y') { lst[++tot[2]][2] = i; ++psm[i][2]; }
for (int l = 0; l <= 2; ++l) {
psm[i][l] += psm[i - 1][l]; } }
for (int a = 0; a <= tot[0]; ++a) {
for (int b = 0; b <= tot[1]; ++b) {
for (int c = 0; c <= tot[2]; ++c) {
for (int l = 0; l <= 2; ++l) {
dp[a][b][c][l] = INF; } } } }
for (int l = 0; l <= 2; ++l) {
dp[0][0][0][l] = 0; }
for (int a = 0; a <= tot[0]; ++a) {
for (int b = 0; b <= tot[1]; ++b) {
for (int c = 0; c <= tot[2]; ++c) {
for (int l = 0; l <= 2; ++l) {
if (l != 0 and a < tot[0]) {
int p = lst[a + 1][0];
f(dp[a + 1][b][c][0], dp[a][b][c][l] + max(0, psm[p][1] - b) + max(0, psm[p][2] - c)); }
if (l != 1 and b < tot[1]) {
int p = lst[b + 1][1];
f(dp[a][b + 1][c][1], dp[a][b][c][l] + max(0, psm[p][2] - c) + max(0, psm[p][0] - a)); }
if (l != 2 and c < tot[2]) {
int p = lst[c + 1][2];
f(dp[a][b][c + 1][2], dp[a][b][c][l] + max(0, psm[p][0] - a) + max(0, psm[p][1] - b)); } } } } }
int v = min( { dp[tot[0]][tot[1]][tot[2]][0], dp[tot[0]][tot[1]][tot[2]][1], dp[tot[0]][tot[1]][tot[2]][2] } );
cout << (v >= INF ? -1 : v) << endl;
return 0; }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
2432 KB |
Output is correct |
19 |
Correct |
5 ms |
1920 KB |
Output is correct |
20 |
Correct |
4 ms |
2304 KB |
Output is correct |
21 |
Correct |
4 ms |
2432 KB |
Output is correct |
22 |
Correct |
4 ms |
1792 KB |
Output is correct |
23 |
Correct |
4 ms |
2176 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
6 ms |
3968 KB |
Output is correct |
26 |
Correct |
7 ms |
4224 KB |
Output is correct |
27 |
Correct |
5 ms |
3072 KB |
Output is correct |
28 |
Correct |
4 ms |
2432 KB |
Output is correct |
29 |
Correct |
4 ms |
2304 KB |
Output is correct |
30 |
Correct |
3 ms |
2304 KB |
Output is correct |
31 |
Correct |
4 ms |
2048 KB |
Output is correct |
32 |
Correct |
4 ms |
2432 KB |
Output is correct |
33 |
Correct |
6 ms |
3968 KB |
Output is correct |
34 |
Correct |
4 ms |
3584 KB |
Output is correct |
35 |
Correct |
4 ms |
2688 KB |
Output is correct |
36 |
Correct |
4 ms |
1920 KB |
Output is correct |
37 |
Correct |
4 ms |
1764 KB |
Output is correct |
38 |
Correct |
4 ms |
2176 KB |
Output is correct |
39 |
Correct |
4 ms |
2304 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
4 ms |
2352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
150 ms |
163000 KB |
Output is correct |
3 |
Correct |
142 ms |
162168 KB |
Output is correct |
4 |
Correct |
129 ms |
162936 KB |
Output is correct |
5 |
Correct |
140 ms |
162936 KB |
Output is correct |
6 |
Correct |
132 ms |
162944 KB |
Output is correct |
7 |
Correct |
133 ms |
162216 KB |
Output is correct |
8 |
Correct |
148 ms |
162168 KB |
Output is correct |
9 |
Correct |
122 ms |
161440 KB |
Output is correct |
10 |
Correct |
138 ms |
162944 KB |
Output is correct |
11 |
Correct |
135 ms |
163052 KB |
Output is correct |
12 |
Correct |
43 ms |
44012 KB |
Output is correct |
13 |
Correct |
60 ms |
77124 KB |
Output is correct |
14 |
Correct |
105 ms |
111352 KB |
Output is correct |
15 |
Correct |
142 ms |
162936 KB |
Output is correct |
16 |
Correct |
146 ms |
163152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
2432 KB |
Output is correct |
19 |
Correct |
5 ms |
1920 KB |
Output is correct |
20 |
Correct |
4 ms |
2304 KB |
Output is correct |
21 |
Correct |
4 ms |
2432 KB |
Output is correct |
22 |
Correct |
4 ms |
1792 KB |
Output is correct |
23 |
Correct |
4 ms |
2176 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
6 ms |
3968 KB |
Output is correct |
26 |
Correct |
7 ms |
4224 KB |
Output is correct |
27 |
Correct |
5 ms |
3072 KB |
Output is correct |
28 |
Correct |
4 ms |
2432 KB |
Output is correct |
29 |
Correct |
4 ms |
2304 KB |
Output is correct |
30 |
Correct |
3 ms |
2304 KB |
Output is correct |
31 |
Correct |
4 ms |
2048 KB |
Output is correct |
32 |
Correct |
4 ms |
2432 KB |
Output is correct |
33 |
Correct |
6 ms |
3968 KB |
Output is correct |
34 |
Correct |
4 ms |
3584 KB |
Output is correct |
35 |
Correct |
4 ms |
2688 KB |
Output is correct |
36 |
Correct |
4 ms |
1920 KB |
Output is correct |
37 |
Correct |
4 ms |
1764 KB |
Output is correct |
38 |
Correct |
4 ms |
2176 KB |
Output is correct |
39 |
Correct |
4 ms |
2304 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
4 ms |
2352 KB |
Output is correct |
42 |
Correct |
3 ms |
384 KB |
Output is correct |
43 |
Correct |
150 ms |
163000 KB |
Output is correct |
44 |
Correct |
142 ms |
162168 KB |
Output is correct |
45 |
Correct |
129 ms |
162936 KB |
Output is correct |
46 |
Correct |
140 ms |
162936 KB |
Output is correct |
47 |
Correct |
132 ms |
162944 KB |
Output is correct |
48 |
Correct |
133 ms |
162216 KB |
Output is correct |
49 |
Correct |
148 ms |
162168 KB |
Output is correct |
50 |
Correct |
122 ms |
161440 KB |
Output is correct |
51 |
Correct |
138 ms |
162944 KB |
Output is correct |
52 |
Correct |
135 ms |
163052 KB |
Output is correct |
53 |
Correct |
43 ms |
44012 KB |
Output is correct |
54 |
Correct |
60 ms |
77124 KB |
Output is correct |
55 |
Correct |
105 ms |
111352 KB |
Output is correct |
56 |
Correct |
142 ms |
162936 KB |
Output is correct |
57 |
Correct |
146 ms |
163152 KB |
Output is correct |
58 |
Correct |
100 ms |
76292 KB |
Output is correct |
59 |
Correct |
117 ms |
96632 KB |
Output is correct |
60 |
Correct |
113 ms |
87836 KB |
Output is correct |
61 |
Correct |
118 ms |
84936 KB |
Output is correct |
62 |
Correct |
128 ms |
160608 KB |
Output is correct |
63 |
Correct |
146 ms |
160248 KB |
Output is correct |
64 |
Correct |
170 ms |
150144 KB |
Output is correct |
65 |
Correct |
153 ms |
138616 KB |
Output is correct |
66 |
Correct |
133 ms |
86192 KB |
Output is correct |
67 |
Correct |
111 ms |
82952 KB |
Output is correct |
68 |
Correct |
137 ms |
88884 KB |
Output is correct |
69 |
Correct |
110 ms |
85420 KB |
Output is correct |
70 |
Correct |
110 ms |
92536 KB |
Output is correct |
71 |
Correct |
153 ms |
84060 KB |
Output is correct |
72 |
Correct |
52 ms |
41688 KB |
Output is correct |
73 |
Correct |
6 ms |
2944 KB |
Output is correct |