#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 1e9+9;
int f[401][401][401][3];
void solve(){
int n;
string s;
cin >> n;
cin >> s;
s = ' ' + s;
int R = 0, G = 0, Y = 0;
vector<int> pfr(n+1, 0), pfg(n+1, 0), pfy(n+1, 0);
vector<int> pr(n+1, 0), pg(n+1, 0), py(n+1, 0);
for(int i = 1; i <= n; i++){
pfr[i] = pfr[i-1];
pfg[i] = pfg[i-1];
pfy[i] = pfy[i-1];
if(s[i] == 'R'){
R++;
pr[R] = i;
pfr[i]++;
}
if(s[i] == 'G'){
G++;
pg[G] = i;
pfg[i]++;
}
if(s[i] == 'Y'){
Y++;
py[Y] = i;
pfy[i]++;
}
}
auto chmin = [&](int &a, int b) -> void{
a = min(a, b);
};
for(int r = 0; r <= R; r++){
for(int g = 0; g <= G; g++){
for(int y = 0; y <= Y; y++){
for(int k = 0; k < 3; k++){
f[r][g][y][k] = INF;
}
}
}
}
for(int k = 0; k < 3; k++){
f[0][0][0][k] = 0;
}
for(int i = 1; i <= n; i++){
for(int r = 0; r <= R; r++){
for(int g = 0; g <= G; g++){
int y = i-r-g;
if(y < 0 || y > Y) continue;
if(r > 0){
chmin(f[r][g][y][0], min(f[r-1][g][y][1], f[r-1][g][y][2]) + pr[r]+max(0, g-pfg[pr[r]]) + max(0, y-pfy[pr[r]])-i);
}
if(g > 0){
chmin(f[r][g][y][1], min(f[r][g-1][y][0], f[r][g-1][y][2]) + pg[g]+max(0, r-pfr[pg[g]]) + max(0, y-pfy[pg[g]])-i);
}
if(y > 0){
chmin(f[r][g][y][2], min(f[r][g][y-1][0], f[r][g][y-1][1]) + py[y]+max(0, r-pfr[py[y]]) + max(0, g-pfg[py[y]])-i);
}
}
}
}
int ans = INF;
for(int k = 0; k < 3; k++){
chmin(ans, f[R][G][Y][k]);
}
if(ans > 1e8) ans = -1;
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
6480 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
2 ms |
12624 KB |
Output is correct |
7 |
Correct |
2 ms |
14672 KB |
Output is correct |
8 |
Correct |
2 ms |
12792 KB |
Output is correct |
9 |
Correct |
2 ms |
12624 KB |
Output is correct |
10 |
Correct |
3 ms |
20816 KB |
Output is correct |
11 |
Correct |
2 ms |
10576 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6480 KB |
Output is correct |
14 |
Correct |
2 ms |
12788 KB |
Output is correct |
15 |
Correct |
1 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
10576 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
6480 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
2 ms |
12624 KB |
Output is correct |
7 |
Correct |
2 ms |
14672 KB |
Output is correct |
8 |
Correct |
2 ms |
12792 KB |
Output is correct |
9 |
Correct |
2 ms |
12624 KB |
Output is correct |
10 |
Correct |
3 ms |
20816 KB |
Output is correct |
11 |
Correct |
2 ms |
10576 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6480 KB |
Output is correct |
14 |
Correct |
2 ms |
12788 KB |
Output is correct |
15 |
Correct |
1 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
10576 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
3 ms |
14160 KB |
Output is correct |
19 |
Correct |
3 ms |
15696 KB |
Output is correct |
20 |
Correct |
3 ms |
20048 KB |
Output is correct |
21 |
Correct |
5 ms |
31568 KB |
Output is correct |
22 |
Correct |
5 ms |
33616 KB |
Output is correct |
23 |
Correct |
4 ms |
33360 KB |
Output is correct |
24 |
Correct |
3 ms |
21072 KB |
Output is correct |
25 |
Correct |
6 ms |
44880 KB |
Output is correct |
26 |
Correct |
5 ms |
48464 KB |
Output is correct |
27 |
Correct |
6 ms |
51792 KB |
Output is correct |
28 |
Correct |
5 ms |
41552 KB |
Output is correct |
29 |
Correct |
5 ms |
43600 KB |
Output is correct |
30 |
Correct |
4 ms |
33360 KB |
Output is correct |
31 |
Correct |
5 ms |
29264 KB |
Output is correct |
32 |
Correct |
5 ms |
35420 KB |
Output is correct |
33 |
Correct |
5 ms |
47696 KB |
Output is correct |
34 |
Correct |
6 ms |
43600 KB |
Output is correct |
35 |
Correct |
5 ms |
45648 KB |
Output is correct |
36 |
Correct |
5 ms |
39504 KB |
Output is correct |
37 |
Correct |
3 ms |
25168 KB |
Output is correct |
38 |
Correct |
3 ms |
27216 KB |
Output is correct |
39 |
Correct |
5 ms |
35408 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
3 ms |
19024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
120 ms |
180636 KB |
Output is correct |
3 |
Correct |
115 ms |
189304 KB |
Output is correct |
4 |
Correct |
113 ms |
192088 KB |
Output is correct |
5 |
Correct |
115 ms |
181448 KB |
Output is correct |
6 |
Correct |
118 ms |
186188 KB |
Output is correct |
7 |
Correct |
110 ms |
194632 KB |
Output is correct |
8 |
Correct |
112 ms |
193608 KB |
Output is correct |
9 |
Correct |
108 ms |
195144 KB |
Output is correct |
10 |
Correct |
111 ms |
194460 KB |
Output is correct |
11 |
Correct |
118 ms |
194376 KB |
Output is correct |
12 |
Correct |
29 ms |
82256 KB |
Output is correct |
13 |
Correct |
51 ms |
114504 KB |
Output is correct |
14 |
Correct |
74 ms |
148308 KB |
Output is correct |
15 |
Correct |
110 ms |
196680 KB |
Output is correct |
16 |
Correct |
117 ms |
195364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
6480 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
2 ms |
12624 KB |
Output is correct |
7 |
Correct |
2 ms |
14672 KB |
Output is correct |
8 |
Correct |
2 ms |
12792 KB |
Output is correct |
9 |
Correct |
2 ms |
12624 KB |
Output is correct |
10 |
Correct |
3 ms |
20816 KB |
Output is correct |
11 |
Correct |
2 ms |
10576 KB |
Output is correct |
12 |
Correct |
3 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6480 KB |
Output is correct |
14 |
Correct |
2 ms |
12788 KB |
Output is correct |
15 |
Correct |
1 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
10576 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
3 ms |
14160 KB |
Output is correct |
19 |
Correct |
3 ms |
15696 KB |
Output is correct |
20 |
Correct |
3 ms |
20048 KB |
Output is correct |
21 |
Correct |
5 ms |
31568 KB |
Output is correct |
22 |
Correct |
5 ms |
33616 KB |
Output is correct |
23 |
Correct |
4 ms |
33360 KB |
Output is correct |
24 |
Correct |
3 ms |
21072 KB |
Output is correct |
25 |
Correct |
6 ms |
44880 KB |
Output is correct |
26 |
Correct |
5 ms |
48464 KB |
Output is correct |
27 |
Correct |
6 ms |
51792 KB |
Output is correct |
28 |
Correct |
5 ms |
41552 KB |
Output is correct |
29 |
Correct |
5 ms |
43600 KB |
Output is correct |
30 |
Correct |
4 ms |
33360 KB |
Output is correct |
31 |
Correct |
5 ms |
29264 KB |
Output is correct |
32 |
Correct |
5 ms |
35420 KB |
Output is correct |
33 |
Correct |
5 ms |
47696 KB |
Output is correct |
34 |
Correct |
6 ms |
43600 KB |
Output is correct |
35 |
Correct |
5 ms |
45648 KB |
Output is correct |
36 |
Correct |
5 ms |
39504 KB |
Output is correct |
37 |
Correct |
3 ms |
25168 KB |
Output is correct |
38 |
Correct |
3 ms |
27216 KB |
Output is correct |
39 |
Correct |
5 ms |
35408 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
3 ms |
19024 KB |
Output is correct |
42 |
Correct |
1 ms |
4432 KB |
Output is correct |
43 |
Correct |
120 ms |
180636 KB |
Output is correct |
44 |
Correct |
115 ms |
189304 KB |
Output is correct |
45 |
Correct |
113 ms |
192088 KB |
Output is correct |
46 |
Correct |
115 ms |
181448 KB |
Output is correct |
47 |
Correct |
118 ms |
186188 KB |
Output is correct |
48 |
Correct |
110 ms |
194632 KB |
Output is correct |
49 |
Correct |
112 ms |
193608 KB |
Output is correct |
50 |
Correct |
108 ms |
195144 KB |
Output is correct |
51 |
Correct |
111 ms |
194460 KB |
Output is correct |
52 |
Correct |
118 ms |
194376 KB |
Output is correct |
53 |
Correct |
29 ms |
82256 KB |
Output is correct |
54 |
Correct |
51 ms |
114504 KB |
Output is correct |
55 |
Correct |
74 ms |
148308 KB |
Output is correct |
56 |
Correct |
110 ms |
196680 KB |
Output is correct |
57 |
Correct |
117 ms |
195364 KB |
Output is correct |
58 |
Correct |
83 ms |
115784 KB |
Output is correct |
59 |
Correct |
90 ms |
130248 KB |
Output is correct |
60 |
Correct |
86 ms |
124488 KB |
Output is correct |
61 |
Correct |
87 ms |
116816 KB |
Output is correct |
62 |
Correct |
121 ms |
177832 KB |
Output is correct |
63 |
Correct |
143 ms |
168320 KB |
Output is correct |
64 |
Correct |
107 ms |
175080 KB |
Output is correct |
65 |
Correct |
118 ms |
158280 KB |
Output is correct |
66 |
Correct |
88 ms |
118344 KB |
Output is correct |
67 |
Correct |
93 ms |
113480 KB |
Output is correct |
68 |
Correct |
103 ms |
120464 KB |
Output is correct |
69 |
Correct |
93 ms |
115272 KB |
Output is correct |
70 |
Correct |
98 ms |
127208 KB |
Output is correct |
71 |
Correct |
85 ms |
115436 KB |
Output is correct |
72 |
Correct |
38 ms |
79532 KB |
Output is correct |
73 |
Correct |
3 ms |
8784 KB |
Output is correct |