#include "bits/stdc++.h"
using namespace std;
const int maxn = 405;
int n, a[maxn];
string s;
int pref[maxn][3];
long long f[maxn][maxn][maxn][3];
vector<int> pos[maxn];
int convert(char c) {
if(c == 'R') return 0;
if(c == 'G') return 1;
if(c == 'Y') return 2;
return -1;
}
void solve() {
cin >> n >> s;
s = ' ' + s;
for(int i = 1; i <= n; ++i) {
a[i] = convert(s[i]);
for(int j = 0; j < 3; ++j) {
pref[i][j] = pref[i - 1][j];
}
pref[i][a[i]]++;
pos[a[i]].push_back(i);
}
for(int i = 0; i <= pref[n][0]; ++i) {
for(int j = 0; j <= pref[n][1]; ++j) {
for(int k = 0; k <= pref[n][2]; ++k) {
for(int x = 0; x < 3; ++x) f[i][j][k][x] = 1e9;
}
}
}
for(int i = 0; i < 3; ++i) f[0][0][0][i] = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j <= min(pref[n][0], i); ++j) {
for(int k = 0; k <= min(pref[n][1], i - j); ++k) {
int l = i - j - k;
if(l > pref[n][2]) continue;
if(j < pref[n][0]) {
long long cur = min(f[j][k][l][1], f[j][k][l][2]);
cur += max(0, pref[pos[0][j]][1] - k) + max(0, pref[pos[0][j]][2] - l);
f[j + 1][k][l][0] = min(f[j + 1][k][l][0], cur);
}
if(k < pref[n][1]) {
long long cur = min(f[j][k][l][0], f[j][k][l][2]);
cur += max(0, pref[pos[1][k]][0] - j) + max(0, pref[pos[1][k]][2] - l);
f[j][k + 1][l][1] = min(f[j][k + 1][l][1], cur);
}
if(l < pref[n][2]) {
long long cur = min(f[j][k][l][1], f[j][k][l][0]);
cur += max(0, pref[pos[2][l]][1] - k) + max(0, pref[pos[2][l]][0] - j);
f[j][k][l + 1][2] = min(f[j][k][l + 1][2], cur);
}
}
}
}
long long res = 1e9;
for(int i = 0; i < 3; ++i) {
res = min(res, f[pref[n][0]][pref[n][1]][pref[n][2]][i]);
}
cout << (res == 1e9 ? -1 : res);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
616 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
2528 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
616 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
2528 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
1968 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4184 KB |
Output is correct |
27 |
Correct |
1 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
2648 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2140 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
3932 KB |
Output is correct |
34 |
Correct |
1 ms |
3676 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
36 |
Correct |
1 ms |
2140 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
59 ms |
164176 KB |
Output is correct |
3 |
Correct |
70 ms |
163384 KB |
Output is correct |
4 |
Correct |
59 ms |
164128 KB |
Output is correct |
5 |
Correct |
59 ms |
165712 KB |
Output is correct |
6 |
Correct |
60 ms |
165364 KB |
Output is correct |
7 |
Correct |
62 ms |
164688 KB |
Output is correct |
8 |
Correct |
62 ms |
164688 KB |
Output is correct |
9 |
Correct |
74 ms |
163760 KB |
Output is correct |
10 |
Correct |
67 ms |
165460 KB |
Output is correct |
11 |
Correct |
62 ms |
165460 KB |
Output is correct |
12 |
Correct |
19 ms |
46172 KB |
Output is correct |
13 |
Correct |
31 ms |
79196 KB |
Output is correct |
14 |
Correct |
40 ms |
113492 KB |
Output is correct |
15 |
Correct |
65 ms |
165456 KB |
Output is correct |
16 |
Correct |
64 ms |
165248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
616 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
2528 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
1968 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4184 KB |
Output is correct |
27 |
Correct |
1 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
2648 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2140 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
3932 KB |
Output is correct |
34 |
Correct |
1 ms |
3676 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
36 |
Correct |
1 ms |
2140 KB |
Output is correct |
37 |
Correct |
1 ms |
1884 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
0 ms |
2396 KB |
Output is correct |
43 |
Correct |
59 ms |
164176 KB |
Output is correct |
44 |
Correct |
70 ms |
163384 KB |
Output is correct |
45 |
Correct |
59 ms |
164128 KB |
Output is correct |
46 |
Correct |
59 ms |
165712 KB |
Output is correct |
47 |
Correct |
60 ms |
165364 KB |
Output is correct |
48 |
Correct |
62 ms |
164688 KB |
Output is correct |
49 |
Correct |
62 ms |
164688 KB |
Output is correct |
50 |
Correct |
74 ms |
163760 KB |
Output is correct |
51 |
Correct |
67 ms |
165460 KB |
Output is correct |
52 |
Correct |
62 ms |
165460 KB |
Output is correct |
53 |
Correct |
19 ms |
46172 KB |
Output is correct |
54 |
Correct |
31 ms |
79196 KB |
Output is correct |
55 |
Correct |
40 ms |
113492 KB |
Output is correct |
56 |
Correct |
65 ms |
165456 KB |
Output is correct |
57 |
Correct |
64 ms |
165248 KB |
Output is correct |
58 |
Correct |
75 ms |
121940 KB |
Output is correct |
59 |
Correct |
82 ms |
139328 KB |
Output is correct |
60 |
Correct |
80 ms |
131408 KB |
Output is correct |
61 |
Correct |
86 ms |
129460 KB |
Output is correct |
62 |
Correct |
73 ms |
164992 KB |
Output is correct |
63 |
Correct |
72 ms |
166228 KB |
Output is correct |
64 |
Correct |
78 ms |
165952 KB |
Output is correct |
65 |
Correct |
86 ms |
160340 KB |
Output is correct |
66 |
Correct |
83 ms |
129620 KB |
Output is correct |
67 |
Correct |
78 ms |
126800 KB |
Output is correct |
68 |
Correct |
83 ms |
132688 KB |
Output is correct |
69 |
Correct |
82 ms |
130420 KB |
Output is correct |
70 |
Correct |
86 ms |
136132 KB |
Output is correct |
71 |
Correct |
75 ms |
127828 KB |
Output is correct |
72 |
Correct |
27 ms |
51848 KB |
Output is correct |
73 |
Correct |
4 ms |
6748 KB |
Output is correct |