#include <bits/stdc++.h>
using namespace std;
#define minim(X, Y) (X = min(X, Y))
#define F(X, Y) for(int X = 0; X < Y; X++)
#define g(X) (X ? (X > 1 ? z : y) : x)
const int Z = 202, INF = 1e9;
int N, A[Z*2], DP[Z][Z][Z][3], p[3][Z*2], h[3][Z*2], c[3], x, y, z;
int main() {
cin >> N, getchar();
F(i, N) {
char v = getchar();
A[i] = (v < 82) + (v < 89);
}
F(i, Z) F(j, Z) F(k, Z) F(l, 3)
DP[i][j][k][l] = (i || j || k) ? INF : 0;
F(i, N) {
if(++c[A[i]] >= Z-1) break;
p[A[i]][g(A[i]) = c[A[i]]] = i;
F(j, 3) h[j][i] = c[j];
F(j, i+1) F(k, i+1) {
(A[i] ? x : y) = j;
(A[i] < 2 ? z : y) = k;
F(o, 3) if(g(o) > c[o]) continue;
F(o, 3) {
int &w = g(o);
if(!w) continue;
int a = INF, b = 0; --w;
F(r, 3) minim(a, r != o ? DP[x][y][z][r] : INF); ++w;
F(r, 3) b += r != o ? max(0, g(r) - h[r][p[o][w]]) : 0;
minim(DP[x][y][z][o], a + b);
}
}
}
int res = INF;
F(i, 3) minim(res, DP[c[0]][c[1]][c[2]][i]);
cout << (res == INF ? -1 : res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
96964 KB |
Output is correct |
2 |
Correct |
43 ms |
97060 KB |
Output is correct |
3 |
Correct |
43 ms |
97032 KB |
Output is correct |
4 |
Correct |
44 ms |
96980 KB |
Output is correct |
5 |
Correct |
42 ms |
97016 KB |
Output is correct |
6 |
Correct |
43 ms |
96944 KB |
Output is correct |
7 |
Correct |
42 ms |
96964 KB |
Output is correct |
8 |
Correct |
42 ms |
96964 KB |
Output is correct |
9 |
Correct |
43 ms |
96968 KB |
Output is correct |
10 |
Correct |
42 ms |
97064 KB |
Output is correct |
11 |
Correct |
42 ms |
96992 KB |
Output is correct |
12 |
Correct |
42 ms |
96972 KB |
Output is correct |
13 |
Correct |
43 ms |
97048 KB |
Output is correct |
14 |
Correct |
42 ms |
96960 KB |
Output is correct |
15 |
Correct |
43 ms |
97020 KB |
Output is correct |
16 |
Correct |
42 ms |
96968 KB |
Output is correct |
17 |
Correct |
42 ms |
97064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
96964 KB |
Output is correct |
2 |
Correct |
43 ms |
97060 KB |
Output is correct |
3 |
Correct |
43 ms |
97032 KB |
Output is correct |
4 |
Correct |
44 ms |
96980 KB |
Output is correct |
5 |
Correct |
42 ms |
97016 KB |
Output is correct |
6 |
Correct |
43 ms |
96944 KB |
Output is correct |
7 |
Correct |
42 ms |
96964 KB |
Output is correct |
8 |
Correct |
42 ms |
96964 KB |
Output is correct |
9 |
Correct |
43 ms |
96968 KB |
Output is correct |
10 |
Correct |
42 ms |
97064 KB |
Output is correct |
11 |
Correct |
42 ms |
96992 KB |
Output is correct |
12 |
Correct |
42 ms |
96972 KB |
Output is correct |
13 |
Correct |
43 ms |
97048 KB |
Output is correct |
14 |
Correct |
42 ms |
96960 KB |
Output is correct |
15 |
Correct |
43 ms |
97020 KB |
Output is correct |
16 |
Correct |
42 ms |
96968 KB |
Output is correct |
17 |
Correct |
42 ms |
97064 KB |
Output is correct |
18 |
Correct |
47 ms |
96944 KB |
Output is correct |
19 |
Correct |
46 ms |
96964 KB |
Output is correct |
20 |
Correct |
46 ms |
96964 KB |
Output is correct |
21 |
Correct |
45 ms |
96996 KB |
Output is correct |
22 |
Correct |
45 ms |
97040 KB |
Output is correct |
23 |
Correct |
46 ms |
97028 KB |
Output is correct |
24 |
Correct |
47 ms |
96952 KB |
Output is correct |
25 |
Correct |
46 ms |
97028 KB |
Output is correct |
26 |
Correct |
46 ms |
96964 KB |
Output is correct |
27 |
Correct |
47 ms |
96964 KB |
Output is correct |
28 |
Correct |
45 ms |
97044 KB |
Output is correct |
29 |
Correct |
45 ms |
96992 KB |
Output is correct |
30 |
Correct |
46 ms |
97000 KB |
Output is correct |
31 |
Correct |
46 ms |
97124 KB |
Output is correct |
32 |
Correct |
46 ms |
97068 KB |
Output is correct |
33 |
Correct |
48 ms |
97108 KB |
Output is correct |
34 |
Correct |
45 ms |
96972 KB |
Output is correct |
35 |
Correct |
46 ms |
97024 KB |
Output is correct |
36 |
Correct |
45 ms |
96964 KB |
Output is correct |
37 |
Correct |
45 ms |
97044 KB |
Output is correct |
38 |
Correct |
46 ms |
96964 KB |
Output is correct |
39 |
Correct |
46 ms |
96960 KB |
Output is correct |
40 |
Correct |
45 ms |
96988 KB |
Output is correct |
41 |
Correct |
45 ms |
97072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
96992 KB |
Output is correct |
2 |
Runtime error |
219 ms |
196528 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
96964 KB |
Output is correct |
2 |
Correct |
43 ms |
97060 KB |
Output is correct |
3 |
Correct |
43 ms |
97032 KB |
Output is correct |
4 |
Correct |
44 ms |
96980 KB |
Output is correct |
5 |
Correct |
42 ms |
97016 KB |
Output is correct |
6 |
Correct |
43 ms |
96944 KB |
Output is correct |
7 |
Correct |
42 ms |
96964 KB |
Output is correct |
8 |
Correct |
42 ms |
96964 KB |
Output is correct |
9 |
Correct |
43 ms |
96968 KB |
Output is correct |
10 |
Correct |
42 ms |
97064 KB |
Output is correct |
11 |
Correct |
42 ms |
96992 KB |
Output is correct |
12 |
Correct |
42 ms |
96972 KB |
Output is correct |
13 |
Correct |
43 ms |
97048 KB |
Output is correct |
14 |
Correct |
42 ms |
96960 KB |
Output is correct |
15 |
Correct |
43 ms |
97020 KB |
Output is correct |
16 |
Correct |
42 ms |
96968 KB |
Output is correct |
17 |
Correct |
42 ms |
97064 KB |
Output is correct |
18 |
Correct |
47 ms |
96944 KB |
Output is correct |
19 |
Correct |
46 ms |
96964 KB |
Output is correct |
20 |
Correct |
46 ms |
96964 KB |
Output is correct |
21 |
Correct |
45 ms |
96996 KB |
Output is correct |
22 |
Correct |
45 ms |
97040 KB |
Output is correct |
23 |
Correct |
46 ms |
97028 KB |
Output is correct |
24 |
Correct |
47 ms |
96952 KB |
Output is correct |
25 |
Correct |
46 ms |
97028 KB |
Output is correct |
26 |
Correct |
46 ms |
96964 KB |
Output is correct |
27 |
Correct |
47 ms |
96964 KB |
Output is correct |
28 |
Correct |
45 ms |
97044 KB |
Output is correct |
29 |
Correct |
45 ms |
96992 KB |
Output is correct |
30 |
Correct |
46 ms |
97000 KB |
Output is correct |
31 |
Correct |
46 ms |
97124 KB |
Output is correct |
32 |
Correct |
46 ms |
97068 KB |
Output is correct |
33 |
Correct |
48 ms |
97108 KB |
Output is correct |
34 |
Correct |
45 ms |
96972 KB |
Output is correct |
35 |
Correct |
46 ms |
97024 KB |
Output is correct |
36 |
Correct |
45 ms |
96964 KB |
Output is correct |
37 |
Correct |
45 ms |
97044 KB |
Output is correct |
38 |
Correct |
46 ms |
96964 KB |
Output is correct |
39 |
Correct |
46 ms |
96960 KB |
Output is correct |
40 |
Correct |
45 ms |
96988 KB |
Output is correct |
41 |
Correct |
45 ms |
97072 KB |
Output is correct |
42 |
Correct |
42 ms |
96992 KB |
Output is correct |
43 |
Runtime error |
219 ms |
196528 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |