#include "bits/stdc++.h"
using namespace std;
#define ar array
const int N = 405;
int dp[N][N][N][3];
vector<int> in[3];
int pos[3][N][3];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
string s; cin>>s;
ar<int, 3> sz {};
for(int i=0;i<n;i++){
if(s[i] == 'R') in[0].push_back(i), sz[0]++;
if(s[i] == 'G') in[1].push_back(i), sz[1]++;
if(s[i] == 'Y') in[2].push_back(i), sz[2]++;
}
for(int t=0;t<3;t++){
for(int i=0;i<sz[t];i++){
for(int k=0;k<3;k++){
for(int j=0;j<sz[k] && in[k][j] <= in[t][i];j++){
pos[t][i][k]++;
}
}
}
}
memset(dp, 127, sizeof dp);
dp[0][0][0][0] =
dp[0][0][0][1] =
dp[0][0][0][2] = 0;
for(int i=0;i<=sz[0];i++){
for(int j=0;j<=sz[1];j++){
for(int l=0;l<=sz[2];l++){
if(i < sz[0]){
int cc = max(0, pos[0][i][1] - j) + max(0, pos[0][i][2] - l);
dp[i+1][j][l][0] = min(dp[i+1][j][l][0], dp[i][j][l][1] + cc);
dp[i+1][j][l][0] = min(dp[i+1][j][l][0], dp[i][j][l][2] + cc);
} if(j < sz[1]){
int cc = max(0, pos[1][j][0] - i) + max(0, pos[1][j][2] - l);
dp[i][j+1][l][1] = min(dp[i][j+1][l][1], dp[i][j][l][0] + cc);
dp[i][j+1][l][1] = min(dp[i][j+1][l][1], dp[i][j][l][2] + cc);
} if(l < sz[2]){
int cc = max(0, pos[2][l][0] - i) + max(0, pos[2][l][1] - j);
dp[i][j][l+1][2] = min(dp[i][j][l+1][2], dp[i][j][l][0] + cc);
dp[i][j][l+1][2] = min(dp[i][j][l+1][2], dp[i][j][l][1] + cc);
}
}
}
}
int res = 1e9;
for(int i=0;i<3;i++) res = min(res, dp[sz[0]][sz[1]][sz[2]][i]);
if(res < 1e9) cout<<res<<"\n";
else cout<<-1<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
305 ms |
780304 KB |
Output is correct |
2 |
Correct |
303 ms |
780376 KB |
Output is correct |
3 |
Correct |
299 ms |
780236 KB |
Output is correct |
4 |
Correct |
300 ms |
780260 KB |
Output is correct |
5 |
Correct |
277 ms |
780308 KB |
Output is correct |
6 |
Correct |
305 ms |
780260 KB |
Output is correct |
7 |
Correct |
295 ms |
780260 KB |
Output is correct |
8 |
Correct |
298 ms |
780232 KB |
Output is correct |
9 |
Correct |
293 ms |
780252 KB |
Output is correct |
10 |
Correct |
304 ms |
780256 KB |
Output is correct |
11 |
Correct |
292 ms |
780296 KB |
Output is correct |
12 |
Correct |
299 ms |
780204 KB |
Output is correct |
13 |
Correct |
300 ms |
780296 KB |
Output is correct |
14 |
Correct |
311 ms |
780236 KB |
Output is correct |
15 |
Correct |
297 ms |
780220 KB |
Output is correct |
16 |
Correct |
296 ms |
780228 KB |
Output is correct |
17 |
Correct |
316 ms |
780344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
305 ms |
780304 KB |
Output is correct |
2 |
Correct |
303 ms |
780376 KB |
Output is correct |
3 |
Correct |
299 ms |
780236 KB |
Output is correct |
4 |
Correct |
300 ms |
780260 KB |
Output is correct |
5 |
Correct |
277 ms |
780308 KB |
Output is correct |
6 |
Correct |
305 ms |
780260 KB |
Output is correct |
7 |
Correct |
295 ms |
780260 KB |
Output is correct |
8 |
Correct |
298 ms |
780232 KB |
Output is correct |
9 |
Correct |
293 ms |
780252 KB |
Output is correct |
10 |
Correct |
304 ms |
780256 KB |
Output is correct |
11 |
Correct |
292 ms |
780296 KB |
Output is correct |
12 |
Correct |
299 ms |
780204 KB |
Output is correct |
13 |
Correct |
300 ms |
780296 KB |
Output is correct |
14 |
Correct |
311 ms |
780236 KB |
Output is correct |
15 |
Correct |
297 ms |
780220 KB |
Output is correct |
16 |
Correct |
296 ms |
780228 KB |
Output is correct |
17 |
Correct |
316 ms |
780344 KB |
Output is correct |
18 |
Correct |
292 ms |
780224 KB |
Output is correct |
19 |
Correct |
316 ms |
780228 KB |
Output is correct |
20 |
Correct |
292 ms |
780180 KB |
Output is correct |
21 |
Correct |
335 ms |
780260 KB |
Output is correct |
22 |
Correct |
328 ms |
780192 KB |
Output is correct |
23 |
Correct |
351 ms |
780228 KB |
Output is correct |
24 |
Correct |
297 ms |
780200 KB |
Output is correct |
25 |
Correct |
320 ms |
780312 KB |
Output is correct |
26 |
Correct |
298 ms |
780228 KB |
Output is correct |
27 |
Correct |
289 ms |
780200 KB |
Output is correct |
28 |
Correct |
309 ms |
780384 KB |
Output is correct |
29 |
Correct |
297 ms |
780280 KB |
Output is correct |
30 |
Correct |
342 ms |
780284 KB |
Output is correct |
31 |
Correct |
293 ms |
780188 KB |
Output is correct |
32 |
Correct |
299 ms |
780216 KB |
Output is correct |
33 |
Correct |
293 ms |
780256 KB |
Output is correct |
34 |
Correct |
312 ms |
780304 KB |
Output is correct |
35 |
Correct |
292 ms |
780328 KB |
Output is correct |
36 |
Correct |
328 ms |
780208 KB |
Output is correct |
37 |
Correct |
283 ms |
780228 KB |
Output is correct |
38 |
Correct |
303 ms |
780244 KB |
Output is correct |
39 |
Correct |
291 ms |
780272 KB |
Output is correct |
40 |
Correct |
290 ms |
780256 KB |
Output is correct |
41 |
Correct |
299 ms |
780260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
299 ms |
780268 KB |
Output is correct |
2 |
Correct |
287 ms |
780272 KB |
Output is correct |
3 |
Correct |
315 ms |
780228 KB |
Output is correct |
4 |
Correct |
301 ms |
780212 KB |
Output is correct |
5 |
Correct |
326 ms |
780252 KB |
Output is correct |
6 |
Correct |
301 ms |
780396 KB |
Output is correct |
7 |
Correct |
334 ms |
780312 KB |
Output is correct |
8 |
Correct |
307 ms |
780204 KB |
Output is correct |
9 |
Correct |
313 ms |
780284 KB |
Output is correct |
10 |
Correct |
295 ms |
780220 KB |
Output is correct |
11 |
Correct |
316 ms |
780252 KB |
Output is correct |
12 |
Correct |
294 ms |
780248 KB |
Output is correct |
13 |
Correct |
307 ms |
780428 KB |
Output is correct |
14 |
Correct |
303 ms |
780272 KB |
Output is correct |
15 |
Correct |
289 ms |
780280 KB |
Output is correct |
16 |
Correct |
301 ms |
780408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
305 ms |
780304 KB |
Output is correct |
2 |
Correct |
303 ms |
780376 KB |
Output is correct |
3 |
Correct |
299 ms |
780236 KB |
Output is correct |
4 |
Correct |
300 ms |
780260 KB |
Output is correct |
5 |
Correct |
277 ms |
780308 KB |
Output is correct |
6 |
Correct |
305 ms |
780260 KB |
Output is correct |
7 |
Correct |
295 ms |
780260 KB |
Output is correct |
8 |
Correct |
298 ms |
780232 KB |
Output is correct |
9 |
Correct |
293 ms |
780252 KB |
Output is correct |
10 |
Correct |
304 ms |
780256 KB |
Output is correct |
11 |
Correct |
292 ms |
780296 KB |
Output is correct |
12 |
Correct |
299 ms |
780204 KB |
Output is correct |
13 |
Correct |
300 ms |
780296 KB |
Output is correct |
14 |
Correct |
311 ms |
780236 KB |
Output is correct |
15 |
Correct |
297 ms |
780220 KB |
Output is correct |
16 |
Correct |
296 ms |
780228 KB |
Output is correct |
17 |
Correct |
316 ms |
780344 KB |
Output is correct |
18 |
Correct |
292 ms |
780224 KB |
Output is correct |
19 |
Correct |
316 ms |
780228 KB |
Output is correct |
20 |
Correct |
292 ms |
780180 KB |
Output is correct |
21 |
Correct |
335 ms |
780260 KB |
Output is correct |
22 |
Correct |
328 ms |
780192 KB |
Output is correct |
23 |
Correct |
351 ms |
780228 KB |
Output is correct |
24 |
Correct |
297 ms |
780200 KB |
Output is correct |
25 |
Correct |
320 ms |
780312 KB |
Output is correct |
26 |
Correct |
298 ms |
780228 KB |
Output is correct |
27 |
Correct |
289 ms |
780200 KB |
Output is correct |
28 |
Correct |
309 ms |
780384 KB |
Output is correct |
29 |
Correct |
297 ms |
780280 KB |
Output is correct |
30 |
Correct |
342 ms |
780284 KB |
Output is correct |
31 |
Correct |
293 ms |
780188 KB |
Output is correct |
32 |
Correct |
299 ms |
780216 KB |
Output is correct |
33 |
Correct |
293 ms |
780256 KB |
Output is correct |
34 |
Correct |
312 ms |
780304 KB |
Output is correct |
35 |
Correct |
292 ms |
780328 KB |
Output is correct |
36 |
Correct |
328 ms |
780208 KB |
Output is correct |
37 |
Correct |
283 ms |
780228 KB |
Output is correct |
38 |
Correct |
303 ms |
780244 KB |
Output is correct |
39 |
Correct |
291 ms |
780272 KB |
Output is correct |
40 |
Correct |
290 ms |
780256 KB |
Output is correct |
41 |
Correct |
299 ms |
780260 KB |
Output is correct |
42 |
Correct |
299 ms |
780268 KB |
Output is correct |
43 |
Correct |
287 ms |
780272 KB |
Output is correct |
44 |
Correct |
315 ms |
780228 KB |
Output is correct |
45 |
Correct |
301 ms |
780212 KB |
Output is correct |
46 |
Correct |
326 ms |
780252 KB |
Output is correct |
47 |
Correct |
301 ms |
780396 KB |
Output is correct |
48 |
Correct |
334 ms |
780312 KB |
Output is correct |
49 |
Correct |
307 ms |
780204 KB |
Output is correct |
50 |
Correct |
313 ms |
780284 KB |
Output is correct |
51 |
Correct |
295 ms |
780220 KB |
Output is correct |
52 |
Correct |
316 ms |
780252 KB |
Output is correct |
53 |
Correct |
294 ms |
780248 KB |
Output is correct |
54 |
Correct |
307 ms |
780428 KB |
Output is correct |
55 |
Correct |
303 ms |
780272 KB |
Output is correct |
56 |
Correct |
289 ms |
780280 KB |
Output is correct |
57 |
Correct |
301 ms |
780408 KB |
Output is correct |
58 |
Correct |
326 ms |
780228 KB |
Output is correct |
59 |
Correct |
456 ms |
780260 KB |
Output is correct |
60 |
Correct |
320 ms |
780316 KB |
Output is correct |
61 |
Correct |
312 ms |
780260 KB |
Output is correct |
62 |
Correct |
314 ms |
780264 KB |
Output is correct |
63 |
Correct |
298 ms |
780200 KB |
Output is correct |
64 |
Correct |
311 ms |
780320 KB |
Output is correct |
65 |
Correct |
324 ms |
780228 KB |
Output is correct |
66 |
Correct |
317 ms |
780444 KB |
Output is correct |
67 |
Correct |
330 ms |
780312 KB |
Output is correct |
68 |
Correct |
316 ms |
780336 KB |
Output is correct |
69 |
Correct |
311 ms |
780248 KB |
Output is correct |
70 |
Correct |
332 ms |
780220 KB |
Output is correct |
71 |
Correct |
322 ms |
780292 KB |
Output is correct |
72 |
Correct |
322 ms |
780300 KB |
Output is correct |
73 |
Correct |
301 ms |
780280 KB |
Output is correct |