#include<bits/stdc++.h>
using namespace std;
#define nl '\n'
int dp[401][401][401][3];
inline void solve(){
int n;
cin>>n;
string s;
cin>>s;
s = " "+s;
int pre[3][n+1], p[3][n+1], c[3];
for(int l=0; l<3; l++) c[l] = 0, pre[l][0] = 0;
for(int i=1; i<=n; i++){
if(s[i] == 'R') p[0][++c[0]] = i;
else if(s[i] == 'G') p[1][++c[1]] = i;
else p[2][++c[2]] = i;
for(int l=0; l<3; l++) pre[l][i] = c[l];
}
for(int l=0; l<3; l++) dp[0][0][0][l] = 0;
for(int i=1; i<=n; i++){
for(int r=0; r<=min(c[0], i); r++){
for(int g=0; g<=min(c[1], i-r); g++){
int y = i - r - g;
if(y > c[2]) continue;
if(r > 0) dp[i][r][g][0] = min(dp[i-1][r-1][g][1], dp[i-1][r-1][g][2]) + max(0, g - pre[1][p[0][r]]) + max(0, y - pre[2][p[0][r]]);
else dp[i][r][g][0] = 1e9;
if(g > 0) dp[i][r][g][1] = min(dp[i-1][r][g-1][0], dp[i-1][r][g-1][2]) + max(0, r - pre[0][p[1][g]]) + max(0, y - pre[2][p[1][g]]);
else dp[i][r][g][1] = 1e9;
if(y > 0) dp[i][r][g][2] = min(dp[i-1][r][g][0], dp[i-1][r][g][1]) + max(0, g - pre[1][p[2][y]]) + max(0, r - pre[0][p[2][y]]);
else dp[i][r][g][2] = 1e9;
}
}
}
int ans = min({dp[n][c[0]][c[1]][0], dp[n][c[0]][c[1]][1], dp[n][c[0]][c[1]][2]});
cout<<(ans >= 1e9 ? -1 : ans)<<nl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
int t = 1;
//cin>>t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
20816 KB |
Output is correct |
5 |
Correct |
3 ms |
29008 KB |
Output is correct |
6 |
Correct |
3 ms |
29164 KB |
Output is correct |
7 |
Correct |
3 ms |
29008 KB |
Output is correct |
8 |
Correct |
3 ms |
29008 KB |
Output is correct |
9 |
Correct |
4 ms |
29008 KB |
Output is correct |
10 |
Correct |
4 ms |
29008 KB |
Output is correct |
11 |
Correct |
4 ms |
29008 KB |
Output is correct |
12 |
Correct |
4 ms |
29008 KB |
Output is correct |
13 |
Correct |
4 ms |
29008 KB |
Output is correct |
14 |
Correct |
3 ms |
27124 KB |
Output is correct |
15 |
Correct |
4 ms |
29008 KB |
Output is correct |
16 |
Correct |
4 ms |
29008 KB |
Output is correct |
17 |
Correct |
4 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
20816 KB |
Output is correct |
5 |
Correct |
3 ms |
29008 KB |
Output is correct |
6 |
Correct |
3 ms |
29164 KB |
Output is correct |
7 |
Correct |
3 ms |
29008 KB |
Output is correct |
8 |
Correct |
3 ms |
29008 KB |
Output is correct |
9 |
Correct |
4 ms |
29008 KB |
Output is correct |
10 |
Correct |
4 ms |
29008 KB |
Output is correct |
11 |
Correct |
4 ms |
29008 KB |
Output is correct |
12 |
Correct |
4 ms |
29008 KB |
Output is correct |
13 |
Correct |
4 ms |
29008 KB |
Output is correct |
14 |
Correct |
3 ms |
27124 KB |
Output is correct |
15 |
Correct |
4 ms |
29008 KB |
Output is correct |
16 |
Correct |
4 ms |
29008 KB |
Output is correct |
17 |
Correct |
4 ms |
26960 KB |
Output is correct |
18 |
Correct |
14 ms |
115280 KB |
Output is correct |
19 |
Correct |
11 ms |
115448 KB |
Output is correct |
20 |
Correct |
11 ms |
115280 KB |
Output is correct |
21 |
Correct |
11 ms |
115464 KB |
Output is correct |
22 |
Correct |
12 ms |
115408 KB |
Output is correct |
23 |
Correct |
11 ms |
115280 KB |
Output is correct |
24 |
Correct |
12 ms |
115280 KB |
Output is correct |
25 |
Correct |
12 ms |
115280 KB |
Output is correct |
26 |
Correct |
11 ms |
115448 KB |
Output is correct |
27 |
Correct |
11 ms |
115256 KB |
Output is correct |
28 |
Correct |
12 ms |
115280 KB |
Output is correct |
29 |
Correct |
11 ms |
115280 KB |
Output is correct |
30 |
Correct |
11 ms |
115448 KB |
Output is correct |
31 |
Correct |
11 ms |
115292 KB |
Output is correct |
32 |
Correct |
11 ms |
115216 KB |
Output is correct |
33 |
Correct |
12 ms |
113232 KB |
Output is correct |
34 |
Correct |
13 ms |
113232 KB |
Output is correct |
35 |
Correct |
12 ms |
111184 KB |
Output is correct |
36 |
Correct |
12 ms |
113268 KB |
Output is correct |
37 |
Correct |
12 ms |
109136 KB |
Output is correct |
38 |
Correct |
11 ms |
115280 KB |
Output is correct |
39 |
Correct |
11 ms |
115280 KB |
Output is correct |
40 |
Correct |
11 ms |
113232 KB |
Output is correct |
41 |
Correct |
11 ms |
115280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6480 KB |
Output is correct |
2 |
Correct |
108 ms |
350748 KB |
Output is correct |
3 |
Correct |
104 ms |
383304 KB |
Output is correct |
4 |
Correct |
109 ms |
360008 KB |
Output is correct |
5 |
Correct |
108 ms |
367432 KB |
Output is correct |
6 |
Correct |
105 ms |
375628 KB |
Output is correct |
7 |
Correct |
101 ms |
381512 KB |
Output is correct |
8 |
Correct |
102 ms |
374856 KB |
Output is correct |
9 |
Correct |
101 ms |
403272 KB |
Output is correct |
10 |
Correct |
105 ms |
389340 KB |
Output is correct |
11 |
Correct |
101 ms |
402248 KB |
Output is correct |
12 |
Correct |
29 ms |
255056 KB |
Output is correct |
13 |
Correct |
46 ms |
291896 KB |
Output is correct |
14 |
Correct |
69 ms |
329352 KB |
Output is correct |
15 |
Correct |
94 ms |
386560 KB |
Output is correct |
16 |
Correct |
97 ms |
387912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
20816 KB |
Output is correct |
5 |
Correct |
3 ms |
29008 KB |
Output is correct |
6 |
Correct |
3 ms |
29164 KB |
Output is correct |
7 |
Correct |
3 ms |
29008 KB |
Output is correct |
8 |
Correct |
3 ms |
29008 KB |
Output is correct |
9 |
Correct |
4 ms |
29008 KB |
Output is correct |
10 |
Correct |
4 ms |
29008 KB |
Output is correct |
11 |
Correct |
4 ms |
29008 KB |
Output is correct |
12 |
Correct |
4 ms |
29008 KB |
Output is correct |
13 |
Correct |
4 ms |
29008 KB |
Output is correct |
14 |
Correct |
3 ms |
27124 KB |
Output is correct |
15 |
Correct |
4 ms |
29008 KB |
Output is correct |
16 |
Correct |
4 ms |
29008 KB |
Output is correct |
17 |
Correct |
4 ms |
26960 KB |
Output is correct |
18 |
Correct |
14 ms |
115280 KB |
Output is correct |
19 |
Correct |
11 ms |
115448 KB |
Output is correct |
20 |
Correct |
11 ms |
115280 KB |
Output is correct |
21 |
Correct |
11 ms |
115464 KB |
Output is correct |
22 |
Correct |
12 ms |
115408 KB |
Output is correct |
23 |
Correct |
11 ms |
115280 KB |
Output is correct |
24 |
Correct |
12 ms |
115280 KB |
Output is correct |
25 |
Correct |
12 ms |
115280 KB |
Output is correct |
26 |
Correct |
11 ms |
115448 KB |
Output is correct |
27 |
Correct |
11 ms |
115256 KB |
Output is correct |
28 |
Correct |
12 ms |
115280 KB |
Output is correct |
29 |
Correct |
11 ms |
115280 KB |
Output is correct |
30 |
Correct |
11 ms |
115448 KB |
Output is correct |
31 |
Correct |
11 ms |
115292 KB |
Output is correct |
32 |
Correct |
11 ms |
115216 KB |
Output is correct |
33 |
Correct |
12 ms |
113232 KB |
Output is correct |
34 |
Correct |
13 ms |
113232 KB |
Output is correct |
35 |
Correct |
12 ms |
111184 KB |
Output is correct |
36 |
Correct |
12 ms |
113268 KB |
Output is correct |
37 |
Correct |
12 ms |
109136 KB |
Output is correct |
38 |
Correct |
11 ms |
115280 KB |
Output is correct |
39 |
Correct |
11 ms |
115280 KB |
Output is correct |
40 |
Correct |
11 ms |
113232 KB |
Output is correct |
41 |
Correct |
11 ms |
115280 KB |
Output is correct |
42 |
Correct |
2 ms |
6480 KB |
Output is correct |
43 |
Correct |
108 ms |
350748 KB |
Output is correct |
44 |
Correct |
104 ms |
383304 KB |
Output is correct |
45 |
Correct |
109 ms |
360008 KB |
Output is correct |
46 |
Correct |
108 ms |
367432 KB |
Output is correct |
47 |
Correct |
105 ms |
375628 KB |
Output is correct |
48 |
Correct |
101 ms |
381512 KB |
Output is correct |
49 |
Correct |
102 ms |
374856 KB |
Output is correct |
50 |
Correct |
101 ms |
403272 KB |
Output is correct |
51 |
Correct |
105 ms |
389340 KB |
Output is correct |
52 |
Correct |
101 ms |
402248 KB |
Output is correct |
53 |
Correct |
29 ms |
255056 KB |
Output is correct |
54 |
Correct |
46 ms |
291896 KB |
Output is correct |
55 |
Correct |
69 ms |
329352 KB |
Output is correct |
56 |
Correct |
94 ms |
386560 KB |
Output is correct |
57 |
Correct |
97 ms |
387912 KB |
Output is correct |
58 |
Correct |
103 ms |
389968 KB |
Output is correct |
59 |
Correct |
104 ms |
396104 KB |
Output is correct |
60 |
Correct |
102 ms |
395220 KB |
Output is correct |
61 |
Correct |
103 ms |
380488 KB |
Output is correct |
62 |
Correct |
95 ms |
398408 KB |
Output is correct |
63 |
Correct |
100 ms |
418252 KB |
Output is correct |
64 |
Correct |
99 ms |
430412 KB |
Output is correct |
65 |
Correct |
102 ms |
425556 KB |
Output is correct |
66 |
Correct |
97 ms |
415904 KB |
Output is correct |
67 |
Correct |
92 ms |
403024 KB |
Output is correct |
68 |
Correct |
101 ms |
409096 KB |
Output is correct |
69 |
Correct |
101 ms |
416072 KB |
Output is correct |
70 |
Correct |
100 ms |
425544 KB |
Output is correct |
71 |
Correct |
102 ms |
430920 KB |
Output is correct |
72 |
Correct |
82 ms |
405064 KB |
Output is correct |
73 |
Correct |
37 ms |
336712 KB |
Output is correct |