#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 400;
const int INF = 1000000000;
int ima[N+5][3];
int dp[N+5][N+5][N+5][3];
int tr[3];
int gde[3][N+5];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
string s;
cin >> s;
for(auto &c : s){
if(c == 'R') c = '0';
else if(c == 'G') c = '1';
else c = '2';
}
for(int i=1; i<=n; i++){
for(int j=0; j<=2; j++) ima[i][j] = ima[i-1][j];
int k = s[i-1] - '0';
ima[i][k]++;
++tr[k];
gde[k][tr[k]] = i;
}
for(int i=0; i<=ima[n][0]; i++) for(int j=0; j<=ima[n][1]; j++) for(int k=0; k<=ima[n][2]; k++) for(int l=0; l<=2; l++) dp[i][j][k][l] = INF;
for(int i=0; i<=2; i++) dp[0][0][0][i] = 0;
for(int i=0; i<=ima[n][0]; i++){
for(int j=0; j<=ima[n][1]; j++){
for(int k=0; k<=ima[n][2]; k++){
for(int l=0; l<=2; l++){
if(i + j + k > 0){
if(i == 0 && l == 0) continue;
if(j == 0 && l == 1) continue;
if(k == 0 && l == 2) continue;
}
if(i < ima[n][0] && l != 0){
int h = gde[0][i+1];
dp[i+1][j][k][0] = min(dp[i+1][j][k][0], dp[i][j][k][l] + max(0, j - ima[h][1]) + max(0, k - ima[h][2]));
}
if(j < ima[n][1] && l != 1){
int h = gde[1][j+1];
dp[i][j+1][k][1] = min(dp[i][j+1][k][1], dp[i][j][k][l] + max(0, i - ima[h][0]) + max(0, k - ima[h][2]));
}
if(k < ima[n][2] && l != 2){
int h = gde[2][k+1];
dp[i][j][k+1][2] = min(dp[i][j][k+1][2], dp[i][j][k][l] + max(0, i - ima[h][0]) + max(0, j - ima[h][1]));
}
}
}
}
}
int g = INF;
for(int i=0; i<=2; i++) g = min(g, dp[ima[n][0]][ima[n][1]][ima[n][2]][i]);
cout << (g == INF ? -1 : g) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
380 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
380 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Correct |
2 ms |
2380 KB |
Output is correct |
22 |
Correct |
2 ms |
1612 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
3 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4044 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
2 ms |
2232 KB |
Output is correct |
31 |
Correct |
2 ms |
1996 KB |
Output is correct |
32 |
Correct |
1 ms |
2380 KB |
Output is correct |
33 |
Correct |
3 ms |
3788 KB |
Output is correct |
34 |
Correct |
3 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
67 ms |
162900 KB |
Output is correct |
3 |
Correct |
71 ms |
162152 KB |
Output is correct |
4 |
Correct |
61 ms |
162900 KB |
Output is correct |
5 |
Correct |
73 ms |
162928 KB |
Output is correct |
6 |
Correct |
65 ms |
162884 KB |
Output is correct |
7 |
Correct |
83 ms |
162100 KB |
Output is correct |
8 |
Correct |
63 ms |
162120 KB |
Output is correct |
9 |
Correct |
60 ms |
161324 KB |
Output is correct |
10 |
Correct |
64 ms |
162904 KB |
Output is correct |
11 |
Correct |
64 ms |
162900 KB |
Output is correct |
12 |
Correct |
24 ms |
43980 KB |
Output is correct |
13 |
Correct |
35 ms |
77160 KB |
Output is correct |
14 |
Correct |
47 ms |
111344 KB |
Output is correct |
15 |
Correct |
68 ms |
162852 KB |
Output is correct |
16 |
Correct |
66 ms |
162932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
380 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Correct |
2 ms |
2380 KB |
Output is correct |
22 |
Correct |
2 ms |
1612 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
3 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4044 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2252 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
2 ms |
2232 KB |
Output is correct |
31 |
Correct |
2 ms |
1996 KB |
Output is correct |
32 |
Correct |
1 ms |
2380 KB |
Output is correct |
33 |
Correct |
3 ms |
3788 KB |
Output is correct |
34 |
Correct |
3 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2252 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
2124 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
67 ms |
162900 KB |
Output is correct |
44 |
Correct |
71 ms |
162152 KB |
Output is correct |
45 |
Correct |
61 ms |
162900 KB |
Output is correct |
46 |
Correct |
73 ms |
162928 KB |
Output is correct |
47 |
Correct |
65 ms |
162884 KB |
Output is correct |
48 |
Correct |
83 ms |
162100 KB |
Output is correct |
49 |
Correct |
63 ms |
162120 KB |
Output is correct |
50 |
Correct |
60 ms |
161324 KB |
Output is correct |
51 |
Correct |
64 ms |
162904 KB |
Output is correct |
52 |
Correct |
64 ms |
162900 KB |
Output is correct |
53 |
Correct |
24 ms |
43980 KB |
Output is correct |
54 |
Correct |
35 ms |
77160 KB |
Output is correct |
55 |
Correct |
47 ms |
111344 KB |
Output is correct |
56 |
Correct |
68 ms |
162852 KB |
Output is correct |
57 |
Correct |
66 ms |
162932 KB |
Output is correct |
58 |
Correct |
88 ms |
76224 KB |
Output is correct |
59 |
Correct |
91 ms |
96580 KB |
Output is correct |
60 |
Correct |
93 ms |
87820 KB |
Output is correct |
61 |
Correct |
90 ms |
84864 KB |
Output is correct |
62 |
Correct |
69 ms |
160644 KB |
Output is correct |
63 |
Correct |
72 ms |
160192 KB |
Output is correct |
64 |
Correct |
87 ms |
150108 KB |
Output is correct |
65 |
Correct |
119 ms |
138652 KB |
Output is correct |
66 |
Correct |
97 ms |
86084 KB |
Output is correct |
67 |
Correct |
93 ms |
83008 KB |
Output is correct |
68 |
Correct |
98 ms |
88716 KB |
Output is correct |
69 |
Correct |
94 ms |
85444 KB |
Output is correct |
70 |
Correct |
97 ms |
92444 KB |
Output is correct |
71 |
Correct |
84 ms |
83960 KB |
Output is correct |
72 |
Correct |
37 ms |
41548 KB |
Output is correct |
73 |
Correct |
5 ms |
2764 KB |
Output is correct |