#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
const int N = 401;
int n, dp[N][N][N][3], sum[N][3], pos[N][3];
string s;
int get_sum(int idx, int l, int r){
if(r < l || l == -1 || r == -1) return 0;
return sum[r][idx] - (l == 0 ? 0 : sum[l - 1][idx]);
}
int solve(int a, int b, int c, int las){
if(a + b + c == n) return 0;
int &ret = dp[a][b][c][las];
if(ret != -1) return ret;
ret = 1e9;
if((las != 0 || a + b + c == 0) && pos[a][0] != -1){
ret = min(ret, solve(a + 1, b, c, 0) + get_sum(1, pos[b][1], pos[a][0]) + get_sum(2, pos[c][2], pos[a][0]));
}
if((las != 1 || a + b + c == 0) && pos[b][1] != -1){
ret = min(ret, solve(a, b + 1, c, 1) + get_sum(0, pos[a][0], pos[b][1]) + get_sum(2, pos[c][2], pos[b][1]));
}
if((las != 2 || a + b + c == 0) && pos[c][2] != -1){
ret = min(ret, solve(a, b, c + 1, 2) + get_sum(0, pos[a][0], pos[c][2]) + get_sum(1, pos[b][1], pos[c][2]));
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(pos, -1, sizeof pos);
memset(dp, -1, sizeof dp);
cin >> n >> s;
for(int i = 0 ; i < n ; i++){
if(s[i] == 'R') s[i] = 0;
else if(s[i] == 'G') s[i] = 1;
else s[i] = 2;
for(int j = 0 ; j < 3 ; j++){
if(i > 0) sum[i][j] = sum[i - 1][j];
sum[i][j] += s[i] == j;
}
}
int pt0 = 0, pt1 = 0, pt2 = 0;
for(int i = 0 ; i < n ; i++){
if(s[i] == 0) pos[pt0++][0] = i;
else if(s[i] == 1) pos[pt1++][1] = i;
else pos[pt2++][2] = i;
}
int ans = solve(0, 0, 0, 0);
if(ans == 1e9) ans = -1;
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
387 ms |
757624 KB |
Output is correct |
2 |
Correct |
388 ms |
757496 KB |
Output is correct |
3 |
Correct |
387 ms |
757496 KB |
Output is correct |
4 |
Correct |
387 ms |
757496 KB |
Output is correct |
5 |
Correct |
387 ms |
757496 KB |
Output is correct |
6 |
Correct |
412 ms |
757496 KB |
Output is correct |
7 |
Correct |
395 ms |
757624 KB |
Output is correct |
8 |
Correct |
387 ms |
757496 KB |
Output is correct |
9 |
Correct |
386 ms |
757496 KB |
Output is correct |
10 |
Correct |
413 ms |
757496 KB |
Output is correct |
11 |
Correct |
383 ms |
757496 KB |
Output is correct |
12 |
Correct |
393 ms |
757624 KB |
Output is correct |
13 |
Correct |
379 ms |
757496 KB |
Output is correct |
14 |
Correct |
384 ms |
757624 KB |
Output is correct |
15 |
Correct |
381 ms |
757492 KB |
Output is correct |
16 |
Correct |
371 ms |
757496 KB |
Output is correct |
17 |
Correct |
386 ms |
757500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
387 ms |
757624 KB |
Output is correct |
2 |
Correct |
388 ms |
757496 KB |
Output is correct |
3 |
Correct |
387 ms |
757496 KB |
Output is correct |
4 |
Correct |
387 ms |
757496 KB |
Output is correct |
5 |
Correct |
387 ms |
757496 KB |
Output is correct |
6 |
Correct |
412 ms |
757496 KB |
Output is correct |
7 |
Correct |
395 ms |
757624 KB |
Output is correct |
8 |
Correct |
387 ms |
757496 KB |
Output is correct |
9 |
Correct |
386 ms |
757496 KB |
Output is correct |
10 |
Correct |
413 ms |
757496 KB |
Output is correct |
11 |
Correct |
383 ms |
757496 KB |
Output is correct |
12 |
Correct |
393 ms |
757624 KB |
Output is correct |
13 |
Correct |
379 ms |
757496 KB |
Output is correct |
14 |
Correct |
384 ms |
757624 KB |
Output is correct |
15 |
Correct |
381 ms |
757492 KB |
Output is correct |
16 |
Correct |
371 ms |
757496 KB |
Output is correct |
17 |
Correct |
386 ms |
757500 KB |
Output is correct |
18 |
Correct |
385 ms |
757496 KB |
Output is correct |
19 |
Correct |
376 ms |
757496 KB |
Output is correct |
20 |
Correct |
397 ms |
757496 KB |
Output is correct |
21 |
Correct |
384 ms |
757496 KB |
Output is correct |
22 |
Correct |
402 ms |
757500 KB |
Output is correct |
23 |
Correct |
378 ms |
757624 KB |
Output is correct |
24 |
Correct |
382 ms |
757496 KB |
Output is correct |
25 |
Correct |
375 ms |
757624 KB |
Output is correct |
26 |
Correct |
384 ms |
757500 KB |
Output is correct |
27 |
Correct |
405 ms |
757496 KB |
Output is correct |
28 |
Correct |
395 ms |
757496 KB |
Output is correct |
29 |
Correct |
385 ms |
757496 KB |
Output is correct |
30 |
Correct |
396 ms |
757532 KB |
Output is correct |
31 |
Correct |
376 ms |
757624 KB |
Output is correct |
32 |
Correct |
394 ms |
757496 KB |
Output is correct |
33 |
Correct |
390 ms |
757500 KB |
Output is correct |
34 |
Correct |
377 ms |
757624 KB |
Output is correct |
35 |
Correct |
379 ms |
757496 KB |
Output is correct |
36 |
Correct |
378 ms |
757592 KB |
Output is correct |
37 |
Correct |
380 ms |
757500 KB |
Output is correct |
38 |
Correct |
380 ms |
757624 KB |
Output is correct |
39 |
Correct |
391 ms |
757496 KB |
Output is correct |
40 |
Correct |
383 ms |
757424 KB |
Output is correct |
41 |
Correct |
376 ms |
757496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
378 ms |
757500 KB |
Output is correct |
2 |
Correct |
373 ms |
757496 KB |
Output is correct |
3 |
Correct |
381 ms |
757496 KB |
Output is correct |
4 |
Correct |
376 ms |
757496 KB |
Output is correct |
5 |
Correct |
382 ms |
757624 KB |
Output is correct |
6 |
Correct |
374 ms |
757496 KB |
Output is correct |
7 |
Correct |
378 ms |
757496 KB |
Output is correct |
8 |
Correct |
385 ms |
757496 KB |
Output is correct |
9 |
Correct |
410 ms |
757500 KB |
Output is correct |
10 |
Correct |
374 ms |
757624 KB |
Output is correct |
11 |
Correct |
383 ms |
757500 KB |
Output is correct |
12 |
Correct |
374 ms |
757496 KB |
Output is correct |
13 |
Correct |
373 ms |
757576 KB |
Output is correct |
14 |
Correct |
372 ms |
757496 KB |
Output is correct |
15 |
Correct |
371 ms |
757500 KB |
Output is correct |
16 |
Correct |
374 ms |
757496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
387 ms |
757624 KB |
Output is correct |
2 |
Correct |
388 ms |
757496 KB |
Output is correct |
3 |
Correct |
387 ms |
757496 KB |
Output is correct |
4 |
Correct |
387 ms |
757496 KB |
Output is correct |
5 |
Correct |
387 ms |
757496 KB |
Output is correct |
6 |
Correct |
412 ms |
757496 KB |
Output is correct |
7 |
Correct |
395 ms |
757624 KB |
Output is correct |
8 |
Correct |
387 ms |
757496 KB |
Output is correct |
9 |
Correct |
386 ms |
757496 KB |
Output is correct |
10 |
Correct |
413 ms |
757496 KB |
Output is correct |
11 |
Correct |
383 ms |
757496 KB |
Output is correct |
12 |
Correct |
393 ms |
757624 KB |
Output is correct |
13 |
Correct |
379 ms |
757496 KB |
Output is correct |
14 |
Correct |
384 ms |
757624 KB |
Output is correct |
15 |
Correct |
381 ms |
757492 KB |
Output is correct |
16 |
Correct |
371 ms |
757496 KB |
Output is correct |
17 |
Correct |
386 ms |
757500 KB |
Output is correct |
18 |
Correct |
385 ms |
757496 KB |
Output is correct |
19 |
Correct |
376 ms |
757496 KB |
Output is correct |
20 |
Correct |
397 ms |
757496 KB |
Output is correct |
21 |
Correct |
384 ms |
757496 KB |
Output is correct |
22 |
Correct |
402 ms |
757500 KB |
Output is correct |
23 |
Correct |
378 ms |
757624 KB |
Output is correct |
24 |
Correct |
382 ms |
757496 KB |
Output is correct |
25 |
Correct |
375 ms |
757624 KB |
Output is correct |
26 |
Correct |
384 ms |
757500 KB |
Output is correct |
27 |
Correct |
405 ms |
757496 KB |
Output is correct |
28 |
Correct |
395 ms |
757496 KB |
Output is correct |
29 |
Correct |
385 ms |
757496 KB |
Output is correct |
30 |
Correct |
396 ms |
757532 KB |
Output is correct |
31 |
Correct |
376 ms |
757624 KB |
Output is correct |
32 |
Correct |
394 ms |
757496 KB |
Output is correct |
33 |
Correct |
390 ms |
757500 KB |
Output is correct |
34 |
Correct |
377 ms |
757624 KB |
Output is correct |
35 |
Correct |
379 ms |
757496 KB |
Output is correct |
36 |
Correct |
378 ms |
757592 KB |
Output is correct |
37 |
Correct |
380 ms |
757500 KB |
Output is correct |
38 |
Correct |
380 ms |
757624 KB |
Output is correct |
39 |
Correct |
391 ms |
757496 KB |
Output is correct |
40 |
Correct |
383 ms |
757424 KB |
Output is correct |
41 |
Correct |
376 ms |
757496 KB |
Output is correct |
42 |
Correct |
378 ms |
757500 KB |
Output is correct |
43 |
Correct |
373 ms |
757496 KB |
Output is correct |
44 |
Correct |
381 ms |
757496 KB |
Output is correct |
45 |
Correct |
376 ms |
757496 KB |
Output is correct |
46 |
Correct |
382 ms |
757624 KB |
Output is correct |
47 |
Correct |
374 ms |
757496 KB |
Output is correct |
48 |
Correct |
378 ms |
757496 KB |
Output is correct |
49 |
Correct |
385 ms |
757496 KB |
Output is correct |
50 |
Correct |
410 ms |
757500 KB |
Output is correct |
51 |
Correct |
374 ms |
757624 KB |
Output is correct |
52 |
Correct |
383 ms |
757500 KB |
Output is correct |
53 |
Correct |
374 ms |
757496 KB |
Output is correct |
54 |
Correct |
373 ms |
757576 KB |
Output is correct |
55 |
Correct |
372 ms |
757496 KB |
Output is correct |
56 |
Correct |
371 ms |
757500 KB |
Output is correct |
57 |
Correct |
374 ms |
757496 KB |
Output is correct |
58 |
Execution timed out |
507 ms |
757624 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |