#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define _size(x) (int)x.size()
#define BIT(i, x) ((x >> i) & 1)
#define MASK(n) ((1 << n) - 1)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++)
#define FORD(i, a, b) for (int i = a, _b = (b); i >= _b; i--)
#define FORB1(i, mask) for (int i = mask; i > 0; i ^= i & - i)
#define FORB0(i, n, mask) for (int i = ((1 << n) - 1) ^ mask; i > 0; i ^= i & - i)
#define FORALL(i, a) for (auto i: a)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);
const int inf = 1e9;
int main() {
fastio;
int n;
cin >> n;
string s;
cin >> s;
vector<int> a(n + 1), cnt(3);
vector<vector<int>> adj(3);
s = ' ' + s;
FOR(i, 1, n) {
if (s[i] == 'R') a[i] = 0;
else if (s[i] == 'G') a[i] = 1;
else a[i] = 2;
cnt[a[i]]++;
adj[a[i]].push_back(i);
}
vector<vector<vector<vector<int>>>> g(3, vector<vector<vector<int>>>(3));
REP(i, 3) {
REP(j, 3) {
if (i == j) continue;
g[i][j].assign(cnt[i] + 1, vector<int>(cnt[j] + 1));
REP(c, cnt[i] + 1) FOR(_c, 1, cnt[j]) {
g[i][j][c][_c] = max((int)(lower_bound(adj[i].begin(), adj[i].end(), adj[j][_c - 1]) - adj[i].begin()) - c, 0);
}
}
}
vector<vector<vector<vector<int>>>> f(3, vector<vector<vector<int>>>(cnt[0] + 1, vector<vector<int>>(cnt[1] + 1, vector<int>(cnt[2] + 1, inf))));
REP(i, 3) f[i][0][0][0] = 0;
REP(sum, n) {
REP(cur, 3) {
REP(i, min(sum, cnt[0]) + 1) REP(j, min(sum - i, cnt[1]) + 1) {
int z = sum - i - j;
if (z > cnt[2]) continue;
if (cur != 0 && i + 1 <= cnt[0]) f[0][i + 1][j][z] = min(f[0][i + 1][j][z], f[cur][i][j][z] + g[1][0][j][i + 1] + g[2][0][z][i + 1]);
if (cur != 1 && j + 1 <= cnt[1]) f[1][i][j + 1][z] = min(f[1][i][j + 1][z], f[cur][i][j][z] + g[0][1][i][j + 1] + g[2][1][z][j + 1]);
if (cur != 2 && z + 1 <= cnt[2]) f[2][i][j][z + 1] = min(f[2][i][j][z + 1], f[cur][i][j][z] + g[0][2][i][z + 1] + g[1][2][j][z + 1]);
}
}
}
int ans = inf;
REP(i, 3) ans = min(ans, f[i][cnt[0]][cnt[1]][cnt[2]]);
cout << (ans == inf ? - 1 : ans) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
268 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
268 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
444 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
452 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
34 ms |
9532 KB |
Output is correct |
3 |
Correct |
34 ms |
9428 KB |
Output is correct |
4 |
Correct |
34 ms |
9572 KB |
Output is correct |
5 |
Correct |
34 ms |
9556 KB |
Output is correct |
6 |
Correct |
34 ms |
9556 KB |
Output is correct |
7 |
Correct |
34 ms |
9508 KB |
Output is correct |
8 |
Correct |
35 ms |
9504 KB |
Output is correct |
9 |
Correct |
35 ms |
9428 KB |
Output is correct |
10 |
Correct |
35 ms |
9556 KB |
Output is correct |
11 |
Correct |
34 ms |
9536 KB |
Output is correct |
12 |
Correct |
6 ms |
2816 KB |
Output is correct |
13 |
Correct |
13 ms |
4692 KB |
Output is correct |
14 |
Correct |
21 ms |
6612 KB |
Output is correct |
15 |
Correct |
34 ms |
9536 KB |
Output is correct |
16 |
Correct |
37 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
268 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
444 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
452 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
34 ms |
9532 KB |
Output is correct |
44 |
Correct |
34 ms |
9428 KB |
Output is correct |
45 |
Correct |
34 ms |
9572 KB |
Output is correct |
46 |
Correct |
34 ms |
9556 KB |
Output is correct |
47 |
Correct |
34 ms |
9556 KB |
Output is correct |
48 |
Correct |
34 ms |
9508 KB |
Output is correct |
49 |
Correct |
35 ms |
9504 KB |
Output is correct |
50 |
Correct |
35 ms |
9428 KB |
Output is correct |
51 |
Correct |
35 ms |
9556 KB |
Output is correct |
52 |
Correct |
34 ms |
9536 KB |
Output is correct |
53 |
Correct |
6 ms |
2816 KB |
Output is correct |
54 |
Correct |
13 ms |
4692 KB |
Output is correct |
55 |
Correct |
21 ms |
6612 KB |
Output is correct |
56 |
Correct |
34 ms |
9536 KB |
Output is correct |
57 |
Correct |
37 ms |
9676 KB |
Output is correct |
58 |
Correct |
123 ms |
41120 KB |
Output is correct |
59 |
Correct |
129 ms |
41812 KB |
Output is correct |
60 |
Correct |
128 ms |
40404 KB |
Output is correct |
61 |
Correct |
127 ms |
41292 KB |
Output is correct |
62 |
Correct |
37 ms |
9300 KB |
Output is correct |
63 |
Correct |
48 ms |
11560 KB |
Output is correct |
64 |
Correct |
72 ms |
25136 KB |
Output is correct |
65 |
Correct |
94 ms |
31216 KB |
Output is correct |
66 |
Correct |
127 ms |
40744 KB |
Output is correct |
67 |
Correct |
124 ms |
40372 KB |
Output is correct |
68 |
Correct |
129 ms |
40804 KB |
Output is correct |
69 |
Correct |
125 ms |
41564 KB |
Output is correct |
70 |
Correct |
138 ms |
41380 KB |
Output is correct |
71 |
Correct |
128 ms |
40752 KB |
Output is correct |
72 |
Correct |
28 ms |
11476 KB |
Output is correct |
73 |
Correct |
6 ms |
3156 KB |
Output is correct |