#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int r, g, b;
const int MXN = 405;
int dp[MXN][MXN][MXN][4];
vector<int> rs, gs, bs;
int pfr[MXN];
int pfg[MXN];
int pfb[MXN];
signed main() {
cin >> n >> s;
int rc = 0, bc = 0, gc = 0;
for (int i=0; i<n; i++) {
if (s[i] == 'R') {
rs.push_back(i+1);
r++;
rc++;
}
if (s[i] == 'G') {
gs.push_back(i+1);
g++;
gc++;
}
if (s[i] == 'Y') {
bs.push_back(i+1);
b++;
bc++;
}
pfr[i+1] = r;
pfg[i+1] = g;
pfb[i+1] = b;
}
s = ' ' + s;
for (int ind=1; ind<=n; ind++) {
for (int r=0; r<=rc; r++) {
for (int g=0; g<=gc; g++) {
int b = ind - r - g;
if (b >= 0 && b <= bc) {
for (int whi=1; whi<=3; whi++) {
int re = INT_MAX / 2;
if (whi != 1 && r > 0) {
int h = dp[ind-1][r-1][g][1];
int l = rs[r-1];
h += max(l + max(g - pfg[l], 0) + max(b - pfb[l], 0) - ind, 0);
re = min(re, h);
}
if (whi != 2 && g > 0) {
int h = dp[ind-1][r][g-1][2];
int l = gs[g-1];
h += max(l + max(b - pfb[l], 0) + max(r - pfr[l], 0) - ind, 0);
re = min(re, h);
}
if (whi != 3 && b > 0) {
int h = dp[ind-1][r][g][3];
int l = bs[b-1];
h += max(l + max(g - pfg[l], 0) + max(r - pfr[l], 0) - ind, 0);
re = min(re, h);
}
dp[ind][r][g][whi] = re;
}
}
}
}
}
int re = dp[n][r][g][1];
for (int i=1; i<=3; i++) re = min(re, dp[n][r][g][i]);
if (re > n * n * 2) {
cout << -1 << endl;
}
else {
cout << re << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
4352 KB |
Output is correct |
19 |
Correct |
2 ms |
4096 KB |
Output is correct |
20 |
Correct |
3 ms |
4480 KB |
Output is correct |
21 |
Correct |
3 ms |
4096 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
3840 KB |
Output is correct |
24 |
Correct |
2 ms |
2944 KB |
Output is correct |
25 |
Correct |
2 ms |
4224 KB |
Output is correct |
26 |
Correct |
4 ms |
4480 KB |
Output is correct |
27 |
Correct |
3 ms |
4480 KB |
Output is correct |
28 |
Correct |
3 ms |
4224 KB |
Output is correct |
29 |
Correct |
3 ms |
4224 KB |
Output is correct |
30 |
Correct |
2 ms |
3840 KB |
Output is correct |
31 |
Correct |
2 ms |
3584 KB |
Output is correct |
32 |
Correct |
2 ms |
3968 KB |
Output is correct |
33 |
Correct |
3 ms |
4224 KB |
Output is correct |
34 |
Correct |
2 ms |
4096 KB |
Output is correct |
35 |
Correct |
3 ms |
4116 KB |
Output is correct |
36 |
Correct |
3 ms |
4096 KB |
Output is correct |
37 |
Correct |
2 ms |
3200 KB |
Output is correct |
38 |
Correct |
2 ms |
3456 KB |
Output is correct |
39 |
Correct |
3 ms |
4096 KB |
Output is correct |
40 |
Correct |
1 ms |
768 KB |
Output is correct |
41 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
95 ms |
163832 KB |
Output is correct |
3 |
Correct |
93 ms |
163080 KB |
Output is correct |
4 |
Correct |
93 ms |
163832 KB |
Output is correct |
5 |
Correct |
95 ms |
163832 KB |
Output is correct |
6 |
Correct |
90 ms |
163856 KB |
Output is correct |
7 |
Correct |
95 ms |
163192 KB |
Output is correct |
8 |
Correct |
98 ms |
163040 KB |
Output is correct |
9 |
Correct |
91 ms |
162296 KB |
Output is correct |
10 |
Correct |
94 ms |
163864 KB |
Output is correct |
11 |
Correct |
91 ms |
163816 KB |
Output is correct |
12 |
Correct |
26 ms |
44544 KB |
Output is correct |
13 |
Correct |
42 ms |
77816 KB |
Output is correct |
14 |
Correct |
67 ms |
112120 KB |
Output is correct |
15 |
Correct |
93 ms |
163832 KB |
Output is correct |
16 |
Correct |
94 ms |
163852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
4352 KB |
Output is correct |
19 |
Correct |
2 ms |
4096 KB |
Output is correct |
20 |
Correct |
3 ms |
4480 KB |
Output is correct |
21 |
Correct |
3 ms |
4096 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
3 ms |
3840 KB |
Output is correct |
24 |
Correct |
2 ms |
2944 KB |
Output is correct |
25 |
Correct |
2 ms |
4224 KB |
Output is correct |
26 |
Correct |
4 ms |
4480 KB |
Output is correct |
27 |
Correct |
3 ms |
4480 KB |
Output is correct |
28 |
Correct |
3 ms |
4224 KB |
Output is correct |
29 |
Correct |
3 ms |
4224 KB |
Output is correct |
30 |
Correct |
2 ms |
3840 KB |
Output is correct |
31 |
Correct |
2 ms |
3584 KB |
Output is correct |
32 |
Correct |
2 ms |
3968 KB |
Output is correct |
33 |
Correct |
3 ms |
4224 KB |
Output is correct |
34 |
Correct |
2 ms |
4096 KB |
Output is correct |
35 |
Correct |
3 ms |
4116 KB |
Output is correct |
36 |
Correct |
3 ms |
4096 KB |
Output is correct |
37 |
Correct |
2 ms |
3200 KB |
Output is correct |
38 |
Correct |
2 ms |
3456 KB |
Output is correct |
39 |
Correct |
3 ms |
4096 KB |
Output is correct |
40 |
Correct |
1 ms |
768 KB |
Output is correct |
41 |
Correct |
2 ms |
2688 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
95 ms |
163832 KB |
Output is correct |
44 |
Correct |
93 ms |
163080 KB |
Output is correct |
45 |
Correct |
93 ms |
163832 KB |
Output is correct |
46 |
Correct |
95 ms |
163832 KB |
Output is correct |
47 |
Correct |
90 ms |
163856 KB |
Output is correct |
48 |
Correct |
95 ms |
163192 KB |
Output is correct |
49 |
Correct |
98 ms |
163040 KB |
Output is correct |
50 |
Correct |
91 ms |
162296 KB |
Output is correct |
51 |
Correct |
94 ms |
163864 KB |
Output is correct |
52 |
Correct |
91 ms |
163816 KB |
Output is correct |
53 |
Correct |
26 ms |
44544 KB |
Output is correct |
54 |
Correct |
42 ms |
77816 KB |
Output is correct |
55 |
Correct |
67 ms |
112120 KB |
Output is correct |
56 |
Correct |
93 ms |
163832 KB |
Output is correct |
57 |
Correct |
94 ms |
163852 KB |
Output is correct |
58 |
Correct |
136 ms |
177000 KB |
Output is correct |
59 |
Correct |
145 ms |
188280 KB |
Output is correct |
60 |
Correct |
144 ms |
186104 KB |
Output is correct |
61 |
Correct |
140 ms |
182640 KB |
Output is correct |
62 |
Correct |
106 ms |
165556 KB |
Output is correct |
63 |
Correct |
96 ms |
168568 KB |
Output is correct |
64 |
Correct |
126 ms |
180984 KB |
Output is correct |
65 |
Correct |
151 ms |
183160 KB |
Output is correct |
66 |
Correct |
134 ms |
182520 KB |
Output is correct |
67 |
Correct |
132 ms |
169336 KB |
Output is correct |
68 |
Correct |
141 ms |
184312 KB |
Output is correct |
69 |
Correct |
141 ms |
185080 KB |
Output is correct |
70 |
Correct |
147 ms |
184184 KB |
Output is correct |
71 |
Correct |
141 ms |
189944 KB |
Output is correct |
72 |
Correct |
82 ms |
131064 KB |
Output is correct |
73 |
Correct |
12 ms |
12288 KB |
Output is correct |