#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb push_back
using namespace std;
const int N=405;
int pref[3][N],dp[3][N][N][N];
vector <int> pos[3];
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n;
cin>>n;
string s;
cin>>s;
s='.'+s;
pos[0].pb(0);pos[1].pb(0);pos[2].pb(0);
for(int i=1;i<=n;i++){
if(s[i]=='R'){
pref[0][i]++;
pos[0].pb(i);
}
else if(s[i]=='G'){
pref[1][i]++;
pos[1].pb(i);
}
else {
pref[2][i]++;
pos[2].pb(i);
}
pref[0][i]+=pref[0][i-1];
pref[1][i]+=pref[1][i-1];
pref[2][i]+=pref[2][i-1];
}
for(int i=0;i<pos[0].size();i++){
for(int j=0;j<pos[1].size();j++){
for(int k=0;k<pos[2].size();k++){
for(int l=0;l<3;l++){
dp[l][i][j][k]=1e18;
}
}
}
}
dp[0][0][0][0]=0;
dp[1][0][0][0]=0;
dp[2][0][0][0]=0;
for(int i=0;i<pos[0].size();i++){
for(int j=0;j<pos[1].size();j++){
for(int k=0;k<pos[2].size();k++){
for(int fr=0;fr<3;fr++){
for(int sc=0;sc<3;sc++){
if(sc==fr)continue;
if(sc==0 && i==pos[0].size()-1)continue;
if(sc==1 && j==pos[1].size()-1)continue;
if(sc==2 && k==pos[2].size()-1)continue;
int pos_fr=0,pos_sc=0,pos_thr=0;
int thr=3-fr-sc;
if(fr==0)pos_fr=pos[fr][i];
else if(fr==1)pos_fr=pos[fr][j];
else pos_fr=pos[fr][k];
if(thr==0)pos_thr=pos[thr][i];
else if(thr==1)pos_thr=pos[thr][j];
else pos_thr=pos[thr][k];
if(sc==0)pos_sc=pos[sc][i+1];
else if(sc==1)pos_sc=pos[sc][j+1];
else pos_sc=pos[sc][k+1];
int ans=dp[fr][i][j][k];
ans+=max(0ll,pref[fr][pos_sc]-pref[fr][pos_fr]);
ans+=max(0ll,pref[thr][pos_sc]-pref[thr][pos_thr]);
if(sc==0)dp[sc][i+1][j][k]=min(ans,dp[sc][i+1][j][k]);
else if(sc==1)dp[sc][i][j+1][k]=min(ans,dp[sc][i][j+1][k]);
else dp[sc][i][j][k+1]=min(ans,dp[sc][i][j][k+1]);
}
}
}
}
}
int ans=1e18;
for(int i=0;i<3;i++)ans=min(ans,dp[i][pos[0].size()-1][pos[1].size()-1][pos[2].size()-1]);
if(ans==1e18)ans=-1;
cout<<ans<<"\n";
}
/*
*/
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:38:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i=0;i<pos[0].size();i++){
| ~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:39:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int j=0;j<pos[1].size();j++){
| ~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:40:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int k=0;k<pos[2].size();k++){
| ~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:50:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i=0;i<pos[0].size();i++){
| ~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:51:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int j=0;j<pos[1].size();j++){
| ~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:52:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int k=0;k<pos[2].size();k++){
| ~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:56:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if(sc==0 && i==pos[0].size()-1)continue;
| ~^~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:57:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if(sc==1 && j==pos[1].size()-1)continue;
| ~^~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:58:38: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | if(sc==2 && k==pos[2].size()-1)continue;
| ~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
25028 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
6 ms |
33116 KB |
Output is correct |
8 |
Correct |
5 ms |
29172 KB |
Output is correct |
9 |
Correct |
4 ms |
29020 KB |
Output is correct |
10 |
Correct |
9 ms |
45404 KB |
Output is correct |
11 |
Correct |
4 ms |
24920 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
3 ms |
16732 KB |
Output is correct |
14 |
Correct |
4 ms |
29020 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
7 ms |
47452 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
25028 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
6 ms |
33116 KB |
Output is correct |
8 |
Correct |
5 ms |
29172 KB |
Output is correct |
9 |
Correct |
4 ms |
29020 KB |
Output is correct |
10 |
Correct |
9 ms |
45404 KB |
Output is correct |
11 |
Correct |
4 ms |
24920 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
3 ms |
16732 KB |
Output is correct |
14 |
Correct |
4 ms |
29020 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
7 ms |
47452 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
12 ms |
73304 KB |
Output is correct |
19 |
Correct |
10 ms |
72540 KB |
Output is correct |
20 |
Correct |
11 ms |
73408 KB |
Output is correct |
21 |
Correct |
10 ms |
72796 KB |
Output is correct |
22 |
Correct |
11 ms |
73052 KB |
Output is correct |
23 |
Correct |
11 ms |
70488 KB |
Output is correct |
24 |
Correct |
6 ms |
45660 KB |
Output is correct |
25 |
Correct |
12 ms |
78652 KB |
Output is correct |
26 |
Correct |
12 ms |
76120 KB |
Output is correct |
27 |
Correct |
10 ms |
74584 KB |
Output is correct |
28 |
Correct |
11 ms |
74952 KB |
Output is correct |
29 |
Correct |
10 ms |
73020 KB |
Output is correct |
30 |
Correct |
11 ms |
70236 KB |
Output is correct |
31 |
Correct |
10 ms |
64088 KB |
Output is correct |
32 |
Correct |
9 ms |
68444 KB |
Output is correct |
33 |
Correct |
11 ms |
72796 KB |
Output is correct |
34 |
Correct |
11 ms |
72024 KB |
Output is correct |
35 |
Correct |
11 ms |
69704 KB |
Output is correct |
36 |
Correct |
10 ms |
68700 KB |
Output is correct |
37 |
Correct |
8 ms |
55900 KB |
Output is correct |
38 |
Correct |
9 ms |
59996 KB |
Output is correct |
39 |
Correct |
10 ms |
70748 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
7 ms |
41572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
196 ms |
422664 KB |
Output is correct |
3 |
Correct |
150 ms |
421464 KB |
Output is correct |
4 |
Correct |
135 ms |
426160 KB |
Output is correct |
5 |
Correct |
143 ms |
428880 KB |
Output is correct |
6 |
Correct |
130 ms |
428924 KB |
Output is correct |
7 |
Correct |
126 ms |
427084 KB |
Output is correct |
8 |
Correct |
147 ms |
427088 KB |
Output is correct |
9 |
Correct |
125 ms |
425296 KB |
Output is correct |
10 |
Correct |
128 ms |
428952 KB |
Output is correct |
11 |
Correct |
127 ms |
429036 KB |
Output is correct |
12 |
Correct |
45 ms |
166484 KB |
Output is correct |
13 |
Correct |
67 ms |
238304 KB |
Output is correct |
14 |
Correct |
90 ms |
313736 KB |
Output is correct |
15 |
Correct |
140 ms |
429516 KB |
Output is correct |
16 |
Correct |
125 ms |
428624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
25028 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
6 ms |
33116 KB |
Output is correct |
8 |
Correct |
5 ms |
29172 KB |
Output is correct |
9 |
Correct |
4 ms |
29020 KB |
Output is correct |
10 |
Correct |
9 ms |
45404 KB |
Output is correct |
11 |
Correct |
4 ms |
24920 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
3 ms |
16732 KB |
Output is correct |
14 |
Correct |
4 ms |
29020 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
7 ms |
47452 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
12 ms |
73304 KB |
Output is correct |
19 |
Correct |
10 ms |
72540 KB |
Output is correct |
20 |
Correct |
11 ms |
73408 KB |
Output is correct |
21 |
Correct |
10 ms |
72796 KB |
Output is correct |
22 |
Correct |
11 ms |
73052 KB |
Output is correct |
23 |
Correct |
11 ms |
70488 KB |
Output is correct |
24 |
Correct |
6 ms |
45660 KB |
Output is correct |
25 |
Correct |
12 ms |
78652 KB |
Output is correct |
26 |
Correct |
12 ms |
76120 KB |
Output is correct |
27 |
Correct |
10 ms |
74584 KB |
Output is correct |
28 |
Correct |
11 ms |
74952 KB |
Output is correct |
29 |
Correct |
10 ms |
73020 KB |
Output is correct |
30 |
Correct |
11 ms |
70236 KB |
Output is correct |
31 |
Correct |
10 ms |
64088 KB |
Output is correct |
32 |
Correct |
9 ms |
68444 KB |
Output is correct |
33 |
Correct |
11 ms |
72796 KB |
Output is correct |
34 |
Correct |
11 ms |
72024 KB |
Output is correct |
35 |
Correct |
11 ms |
69704 KB |
Output is correct |
36 |
Correct |
10 ms |
68700 KB |
Output is correct |
37 |
Correct |
8 ms |
55900 KB |
Output is correct |
38 |
Correct |
9 ms |
59996 KB |
Output is correct |
39 |
Correct |
10 ms |
70748 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
7 ms |
41572 KB |
Output is correct |
42 |
Correct |
3 ms |
12636 KB |
Output is correct |
43 |
Correct |
196 ms |
422664 KB |
Output is correct |
44 |
Correct |
150 ms |
421464 KB |
Output is correct |
45 |
Correct |
135 ms |
426160 KB |
Output is correct |
46 |
Correct |
143 ms |
428880 KB |
Output is correct |
47 |
Correct |
130 ms |
428924 KB |
Output is correct |
48 |
Correct |
126 ms |
427084 KB |
Output is correct |
49 |
Correct |
147 ms |
427088 KB |
Output is correct |
50 |
Correct |
125 ms |
425296 KB |
Output is correct |
51 |
Correct |
128 ms |
428952 KB |
Output is correct |
52 |
Correct |
127 ms |
429036 KB |
Output is correct |
53 |
Correct |
45 ms |
166484 KB |
Output is correct |
54 |
Correct |
67 ms |
238304 KB |
Output is correct |
55 |
Correct |
90 ms |
313736 KB |
Output is correct |
56 |
Correct |
140 ms |
429516 KB |
Output is correct |
57 |
Correct |
125 ms |
428624 KB |
Output is correct |
58 |
Correct |
148 ms |
211792 KB |
Output is correct |
59 |
Correct |
159 ms |
249172 KB |
Output is correct |
60 |
Correct |
156 ms |
234048 KB |
Output is correct |
61 |
Correct |
179 ms |
228176 KB |
Output is correct |
62 |
Correct |
135 ms |
422740 KB |
Output is correct |
63 |
Correct |
138 ms |
415120 KB |
Output is correct |
64 |
Correct |
154 ms |
370836 KB |
Output is correct |
65 |
Correct |
159 ms |
329044 KB |
Output is correct |
66 |
Correct |
151 ms |
229716 KB |
Output is correct |
67 |
Correct |
149 ms |
219892 KB |
Output is correct |
68 |
Correct |
161 ms |
235348 KB |
Output is correct |
69 |
Correct |
153 ms |
228560 KB |
Output is correct |
70 |
Correct |
157 ms |
241672 KB |
Output is correct |
71 |
Correct |
157 ms |
228688 KB |
Output is correct |
72 |
Correct |
66 ms |
164176 KB |
Output is correct |
73 |
Correct |
10 ms |
21084 KB |
Output is correct |