#include <bits/stdc++.h>
using namespace std;
int const mxN = 405;
int n;
int dp[mxN][mxN][mxN][4];
int cnt[mxN][3], ind[mxN+5][3];
int solve ( int i, int red, int yell, int b ) {
if(i==n) return 0;
if(~dp[i][red][yell][b]) return dp[i][red][yell][b];
int ans = 1e9, green = i-(red+yell), gn = ind[green+1][2], rn = ind[red+1][0], yn = ind[yell+1][1];
if(b!=0 && red+1<=cnt[n][0]) ans = min( solve(i+1, red+1, yell, 0) + max(0, cnt[rn][2]-green) + max(0, cnt[rn][1]-yell), ans );
if(b!=1 && yell+1<=cnt[n][1]) ans = min (ans, solve(i+1, red, yell+1, 1) + max(0, cnt[yn][0]-red) + max(0, cnt[yn][2]-green));
if(b!=2 && green+1<=cnt[n][2]) ans = min (ans, solve(i+1, red, yell, 2) + max(0, cnt[gn][0]-red) + max(0, cnt[gn][1]-yell));
return dp[i][red][yell][b] = ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
cin>>n;
string s; cin>>s;
memset(dp,-1,sizeof dp);
for(int i=1; i<=n; i++) {
cnt[i][0] = cnt[i-1][0];
cnt[i][1] = cnt[i-1][1];
cnt[i][2] = cnt[i-1][2];
if(s[i-1]=='R') { cnt[i][0]++; ind[cnt[i][0]][0] = i; }
else if(s[i-1]=='Y') { cnt[i][1]++; ind[cnt[i][1]][1] = i;}
else { cnt[i][2]++; ind[cnt[i][2]][2] = i; }
}
long long fin = solve(0,0,0,3);
cout<<(fin==1e9? -1:fin);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
348 ms |
1040280 KB |
Output is correct |
2 |
Correct |
319 ms |
1040296 KB |
Output is correct |
3 |
Correct |
311 ms |
1040332 KB |
Output is correct |
4 |
Correct |
311 ms |
1040208 KB |
Output is correct |
5 |
Correct |
297 ms |
1040208 KB |
Output is correct |
6 |
Correct |
297 ms |
1040376 KB |
Output is correct |
7 |
Correct |
308 ms |
1040320 KB |
Output is correct |
8 |
Correct |
297 ms |
1040284 KB |
Output is correct |
9 |
Correct |
308 ms |
1040580 KB |
Output is correct |
10 |
Correct |
294 ms |
1040388 KB |
Output is correct |
11 |
Correct |
299 ms |
1040224 KB |
Output is correct |
12 |
Correct |
295 ms |
1040216 KB |
Output is correct |
13 |
Correct |
304 ms |
1040244 KB |
Output is correct |
14 |
Correct |
295 ms |
1040308 KB |
Output is correct |
15 |
Correct |
297 ms |
1040312 KB |
Output is correct |
16 |
Correct |
307 ms |
1040372 KB |
Output is correct |
17 |
Correct |
296 ms |
1040400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
348 ms |
1040280 KB |
Output is correct |
2 |
Correct |
319 ms |
1040296 KB |
Output is correct |
3 |
Correct |
311 ms |
1040332 KB |
Output is correct |
4 |
Correct |
311 ms |
1040208 KB |
Output is correct |
5 |
Correct |
297 ms |
1040208 KB |
Output is correct |
6 |
Correct |
297 ms |
1040376 KB |
Output is correct |
7 |
Correct |
308 ms |
1040320 KB |
Output is correct |
8 |
Correct |
297 ms |
1040284 KB |
Output is correct |
9 |
Correct |
308 ms |
1040580 KB |
Output is correct |
10 |
Correct |
294 ms |
1040388 KB |
Output is correct |
11 |
Correct |
299 ms |
1040224 KB |
Output is correct |
12 |
Correct |
295 ms |
1040216 KB |
Output is correct |
13 |
Correct |
304 ms |
1040244 KB |
Output is correct |
14 |
Correct |
295 ms |
1040308 KB |
Output is correct |
15 |
Correct |
297 ms |
1040312 KB |
Output is correct |
16 |
Correct |
307 ms |
1040372 KB |
Output is correct |
17 |
Correct |
296 ms |
1040400 KB |
Output is correct |
18 |
Correct |
299 ms |
1040816 KB |
Output is correct |
19 |
Correct |
296 ms |
1040208 KB |
Output is correct |
20 |
Correct |
297 ms |
1040468 KB |
Output is correct |
21 |
Correct |
293 ms |
1040212 KB |
Output is correct |
22 |
Correct |
299 ms |
1040360 KB |
Output is correct |
23 |
Correct |
299 ms |
1040432 KB |
Output is correct |
24 |
Correct |
297 ms |
1040328 KB |
Output is correct |
25 |
Correct |
299 ms |
1040408 KB |
Output is correct |
26 |
Correct |
294 ms |
1040212 KB |
Output is correct |
27 |
Correct |
297 ms |
1040476 KB |
Output is correct |
28 |
Correct |
294 ms |
1040208 KB |
Output is correct |
29 |
Correct |
293 ms |
1040212 KB |
Output is correct |
30 |
Correct |
294 ms |
1040328 KB |
Output is correct |
31 |
Correct |
295 ms |
1040248 KB |
Output is correct |
32 |
Correct |
395 ms |
1040204 KB |
Output is correct |
33 |
Correct |
296 ms |
1040312 KB |
Output is correct |
34 |
Correct |
297 ms |
1040372 KB |
Output is correct |
35 |
Correct |
296 ms |
1040396 KB |
Output is correct |
36 |
Correct |
309 ms |
1040360 KB |
Output is correct |
37 |
Correct |
303 ms |
1040212 KB |
Output is correct |
38 |
Correct |
313 ms |
1040720 KB |
Output is correct |
39 |
Correct |
301 ms |
1040208 KB |
Output is correct |
40 |
Correct |
298 ms |
1040212 KB |
Output is correct |
41 |
Correct |
300 ms |
1040200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
295 ms |
1040364 KB |
Output is correct |
2 |
Correct |
300 ms |
1040488 KB |
Output is correct |
3 |
Correct |
298 ms |
1040344 KB |
Output is correct |
4 |
Correct |
300 ms |
1040400 KB |
Output is correct |
5 |
Correct |
293 ms |
1040284 KB |
Output is correct |
6 |
Correct |
313 ms |
1040300 KB |
Output is correct |
7 |
Correct |
294 ms |
1040328 KB |
Output is correct |
8 |
Correct |
312 ms |
1040468 KB |
Output is correct |
9 |
Correct |
298 ms |
1040352 KB |
Output is correct |
10 |
Correct |
305 ms |
1040468 KB |
Output is correct |
11 |
Correct |
301 ms |
1040684 KB |
Output is correct |
12 |
Correct |
300 ms |
1040400 KB |
Output is correct |
13 |
Correct |
303 ms |
1040468 KB |
Output is correct |
14 |
Correct |
308 ms |
1040468 KB |
Output is correct |
15 |
Correct |
294 ms |
1040480 KB |
Output is correct |
16 |
Correct |
310 ms |
1040400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
348 ms |
1040280 KB |
Output is correct |
2 |
Correct |
319 ms |
1040296 KB |
Output is correct |
3 |
Correct |
311 ms |
1040332 KB |
Output is correct |
4 |
Correct |
311 ms |
1040208 KB |
Output is correct |
5 |
Correct |
297 ms |
1040208 KB |
Output is correct |
6 |
Correct |
297 ms |
1040376 KB |
Output is correct |
7 |
Correct |
308 ms |
1040320 KB |
Output is correct |
8 |
Correct |
297 ms |
1040284 KB |
Output is correct |
9 |
Correct |
308 ms |
1040580 KB |
Output is correct |
10 |
Correct |
294 ms |
1040388 KB |
Output is correct |
11 |
Correct |
299 ms |
1040224 KB |
Output is correct |
12 |
Correct |
295 ms |
1040216 KB |
Output is correct |
13 |
Correct |
304 ms |
1040244 KB |
Output is correct |
14 |
Correct |
295 ms |
1040308 KB |
Output is correct |
15 |
Correct |
297 ms |
1040312 KB |
Output is correct |
16 |
Correct |
307 ms |
1040372 KB |
Output is correct |
17 |
Correct |
296 ms |
1040400 KB |
Output is correct |
18 |
Correct |
299 ms |
1040816 KB |
Output is correct |
19 |
Correct |
296 ms |
1040208 KB |
Output is correct |
20 |
Correct |
297 ms |
1040468 KB |
Output is correct |
21 |
Correct |
293 ms |
1040212 KB |
Output is correct |
22 |
Correct |
299 ms |
1040360 KB |
Output is correct |
23 |
Correct |
299 ms |
1040432 KB |
Output is correct |
24 |
Correct |
297 ms |
1040328 KB |
Output is correct |
25 |
Correct |
299 ms |
1040408 KB |
Output is correct |
26 |
Correct |
294 ms |
1040212 KB |
Output is correct |
27 |
Correct |
297 ms |
1040476 KB |
Output is correct |
28 |
Correct |
294 ms |
1040208 KB |
Output is correct |
29 |
Correct |
293 ms |
1040212 KB |
Output is correct |
30 |
Correct |
294 ms |
1040328 KB |
Output is correct |
31 |
Correct |
295 ms |
1040248 KB |
Output is correct |
32 |
Correct |
395 ms |
1040204 KB |
Output is correct |
33 |
Correct |
296 ms |
1040312 KB |
Output is correct |
34 |
Correct |
297 ms |
1040372 KB |
Output is correct |
35 |
Correct |
296 ms |
1040396 KB |
Output is correct |
36 |
Correct |
309 ms |
1040360 KB |
Output is correct |
37 |
Correct |
303 ms |
1040212 KB |
Output is correct |
38 |
Correct |
313 ms |
1040720 KB |
Output is correct |
39 |
Correct |
301 ms |
1040208 KB |
Output is correct |
40 |
Correct |
298 ms |
1040212 KB |
Output is correct |
41 |
Correct |
300 ms |
1040200 KB |
Output is correct |
42 |
Correct |
295 ms |
1040364 KB |
Output is correct |
43 |
Correct |
300 ms |
1040488 KB |
Output is correct |
44 |
Correct |
298 ms |
1040344 KB |
Output is correct |
45 |
Correct |
300 ms |
1040400 KB |
Output is correct |
46 |
Correct |
293 ms |
1040284 KB |
Output is correct |
47 |
Correct |
313 ms |
1040300 KB |
Output is correct |
48 |
Correct |
294 ms |
1040328 KB |
Output is correct |
49 |
Correct |
312 ms |
1040468 KB |
Output is correct |
50 |
Correct |
298 ms |
1040352 KB |
Output is correct |
51 |
Correct |
305 ms |
1040468 KB |
Output is correct |
52 |
Correct |
301 ms |
1040684 KB |
Output is correct |
53 |
Correct |
300 ms |
1040400 KB |
Output is correct |
54 |
Correct |
303 ms |
1040468 KB |
Output is correct |
55 |
Correct |
308 ms |
1040468 KB |
Output is correct |
56 |
Correct |
294 ms |
1040480 KB |
Output is correct |
57 |
Correct |
310 ms |
1040400 KB |
Output is correct |
58 |
Correct |
403 ms |
1040464 KB |
Output is correct |
59 |
Correct |
399 ms |
1040356 KB |
Output is correct |
60 |
Correct |
407 ms |
1040360 KB |
Output is correct |
61 |
Correct |
409 ms |
1040488 KB |
Output is correct |
62 |
Correct |
298 ms |
1040324 KB |
Output is correct |
63 |
Correct |
296 ms |
1040464 KB |
Output is correct |
64 |
Correct |
320 ms |
1040272 KB |
Output is correct |
65 |
Correct |
333 ms |
1040364 KB |
Output is correct |
66 |
Correct |
402 ms |
1040268 KB |
Output is correct |
67 |
Correct |
396 ms |
1040488 KB |
Output is correct |
68 |
Correct |
412 ms |
1040612 KB |
Output is correct |
69 |
Correct |
419 ms |
1040484 KB |
Output is correct |
70 |
Correct |
401 ms |
1040252 KB |
Output is correct |
71 |
Correct |
419 ms |
1040480 KB |
Output is correct |
72 |
Correct |
307 ms |
1040368 KB |
Output is correct |
73 |
Correct |
297 ms |
1040460 KB |
Output is correct |