///In the name of GOD
//#pragma GCC optimize("O2")
//pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MXN = 400 + 10;
int n, cnt[3][MXN];
int dp[3][MXN][MXN][MXN];
vector<int> Pos[3];
string s;
inline void upd(int &x, int y){ if(x > y) x = y; }
inline int Calc(int ch, int a, int b, int c){
int p = Pos[ch][(ch == 0 ? a : (ch == 1 ? b : c))];
return max(0, cnt[0][p - 1] - a) + max(0, cnt[1][p - 1] - b) + max(0, cnt[2][p - 1] - c);;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
cin >> n >> s;
s = "$" + s;
memset(dp, 31, sizeof dp);
for(int i = 1; i <= n; i ++){
switch(s[i]){
case 'R' : s[i] = 'A'; break;
case 'G' : s[i] = 'B'; break;
case 'Y' : s[i] = 'C'; break;
}
cnt[0][i] = cnt[0][i - 1];
cnt[1][i] = cnt[1][i - 1];
cnt[2][i] = cnt[2][i - 1];
cnt[s[i] - 'A'][i] ++;
Pos[s[i] - 'A'].push_back(i);
}
if(max({cnt[0][n], cnt[1][n], cnt[2][n]}) > (n + 1) / 2)
return cout << -1, 0;
if(cnt[0][n]) dp[0][1][0][0] = Calc(0, 0, 0, 0);
if(cnt[1][n]) dp[1][0][1][0] = Calc(1, 0, 0, 0);
if(cnt[2][n]) dp[2][0][0][1] = Calc(2, 0, 0, 0);
for(int a = 0; a <= cnt[0][n]; a ++){
for(int b = 0; b <= cnt[1][n]; b ++){
for(int c = 0; c <= cnt[2][n]; c ++){
if(a + b + c == 0) continue;
if(a < cnt[0][n]){
ll ca = Calc(0, a, b, c);
upd(dp[0][a + 1][b][c], dp[1][a][b][c] + ca);
upd(dp[0][a + 1][b][c], dp[2][a][b][c] + ca);
}
if(b < cnt[1][n]){
ll ca = Calc(1, a, b, c);
upd(dp[1][a][b + 1][c], dp[0][a][b][c] + ca);
upd(dp[1][a][b + 1][c], dp[2][a][b][c] + ca);
}
if(c < cnt[2][n]){
ll ca = Calc(2, a, b, c);
upd(dp[2][a][b][c + 1], dp[0][a][b][c] + ca);
upd(dp[2][a][b][c + 1], dp[1][a][b][c] + ca);
}
}
}
}
int ans = dp[0][cnt[0][n]][cnt[1][n]][cnt[2][n]];
ans = min(ans, dp[1][cnt[0][n]][cnt[1][n]][cnt[2][n]]);
ans = min(ans, dp[2][cnt[0][n]][cnt[1][n]][cnt[2][n]]);
cout << ans << '\n';
return 0;
}
/*!
HE'S AN INSTIGATOR,
ENEMY ELIMINATOR,
AND WHEN HE KNOCKS YOU BETTER
YOU BETTER LET HIM IN.
*/
//! N.N
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
434 ms |
809624 KB |
Output is correct |
2 |
Correct |
439 ms |
809708 KB |
Output is correct |
3 |
Correct |
440 ms |
809708 KB |
Output is correct |
4 |
Correct |
434 ms |
809708 KB |
Output is correct |
5 |
Correct |
446 ms |
809544 KB |
Output is correct |
6 |
Correct |
439 ms |
809708 KB |
Output is correct |
7 |
Correct |
437 ms |
809680 KB |
Output is correct |
8 |
Correct |
436 ms |
809708 KB |
Output is correct |
9 |
Correct |
437 ms |
809708 KB |
Output is correct |
10 |
Correct |
435 ms |
809580 KB |
Output is correct |
11 |
Correct |
440 ms |
809580 KB |
Output is correct |
12 |
Correct |
431 ms |
809708 KB |
Output is correct |
13 |
Correct |
457 ms |
809708 KB |
Output is correct |
14 |
Correct |
437 ms |
809580 KB |
Output is correct |
15 |
Correct |
437 ms |
809580 KB |
Output is correct |
16 |
Correct |
438 ms |
809708 KB |
Output is correct |
17 |
Correct |
443 ms |
809580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
434 ms |
809624 KB |
Output is correct |
2 |
Correct |
439 ms |
809708 KB |
Output is correct |
3 |
Correct |
440 ms |
809708 KB |
Output is correct |
4 |
Correct |
434 ms |
809708 KB |
Output is correct |
5 |
Correct |
446 ms |
809544 KB |
Output is correct |
6 |
Correct |
439 ms |
809708 KB |
Output is correct |
7 |
Correct |
437 ms |
809680 KB |
Output is correct |
8 |
Correct |
436 ms |
809708 KB |
Output is correct |
9 |
Correct |
437 ms |
809708 KB |
Output is correct |
10 |
Correct |
435 ms |
809580 KB |
Output is correct |
11 |
Correct |
440 ms |
809580 KB |
Output is correct |
12 |
Correct |
431 ms |
809708 KB |
Output is correct |
13 |
Correct |
457 ms |
809708 KB |
Output is correct |
14 |
Correct |
437 ms |
809580 KB |
Output is correct |
15 |
Correct |
437 ms |
809580 KB |
Output is correct |
16 |
Correct |
438 ms |
809708 KB |
Output is correct |
17 |
Correct |
443 ms |
809580 KB |
Output is correct |
18 |
Correct |
450 ms |
809584 KB |
Output is correct |
19 |
Correct |
441 ms |
809580 KB |
Output is correct |
20 |
Correct |
434 ms |
809580 KB |
Output is correct |
21 |
Correct |
439 ms |
809708 KB |
Output is correct |
22 |
Correct |
439 ms |
809692 KB |
Output is correct |
23 |
Correct |
434 ms |
809580 KB |
Output is correct |
24 |
Correct |
436 ms |
809580 KB |
Output is correct |
25 |
Correct |
441 ms |
809716 KB |
Output is correct |
26 |
Correct |
439 ms |
809580 KB |
Output is correct |
27 |
Correct |
435 ms |
809580 KB |
Output is correct |
28 |
Correct |
440 ms |
809708 KB |
Output is correct |
29 |
Correct |
437 ms |
809572 KB |
Output is correct |
30 |
Correct |
439 ms |
809680 KB |
Output is correct |
31 |
Correct |
433 ms |
809580 KB |
Output is correct |
32 |
Correct |
440 ms |
809708 KB |
Output is correct |
33 |
Correct |
444 ms |
809580 KB |
Output is correct |
34 |
Correct |
434 ms |
809708 KB |
Output is correct |
35 |
Correct |
458 ms |
809708 KB |
Output is correct |
36 |
Correct |
435 ms |
809580 KB |
Output is correct |
37 |
Correct |
432 ms |
809580 KB |
Output is correct |
38 |
Correct |
440 ms |
809656 KB |
Output is correct |
39 |
Correct |
432 ms |
809708 KB |
Output is correct |
40 |
Correct |
434 ms |
809580 KB |
Output is correct |
41 |
Correct |
440 ms |
809640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
441 ms |
809708 KB |
Output is correct |
2 |
Correct |
469 ms |
809652 KB |
Output is correct |
3 |
Correct |
438 ms |
809708 KB |
Output is correct |
4 |
Correct |
440 ms |
809724 KB |
Output is correct |
5 |
Correct |
437 ms |
809612 KB |
Output is correct |
6 |
Correct |
438 ms |
809580 KB |
Output is correct |
7 |
Correct |
439 ms |
809580 KB |
Output is correct |
8 |
Correct |
440 ms |
809580 KB |
Output is correct |
9 |
Correct |
437 ms |
809588 KB |
Output is correct |
10 |
Correct |
437 ms |
809708 KB |
Output is correct |
11 |
Correct |
440 ms |
809580 KB |
Output is correct |
12 |
Correct |
436 ms |
809568 KB |
Output is correct |
13 |
Correct |
441 ms |
809616 KB |
Output is correct |
14 |
Correct |
436 ms |
809580 KB |
Output is correct |
15 |
Correct |
440 ms |
809580 KB |
Output is correct |
16 |
Correct |
436 ms |
809804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
434 ms |
809624 KB |
Output is correct |
2 |
Correct |
439 ms |
809708 KB |
Output is correct |
3 |
Correct |
440 ms |
809708 KB |
Output is correct |
4 |
Correct |
434 ms |
809708 KB |
Output is correct |
5 |
Correct |
446 ms |
809544 KB |
Output is correct |
6 |
Correct |
439 ms |
809708 KB |
Output is correct |
7 |
Correct |
437 ms |
809680 KB |
Output is correct |
8 |
Correct |
436 ms |
809708 KB |
Output is correct |
9 |
Correct |
437 ms |
809708 KB |
Output is correct |
10 |
Correct |
435 ms |
809580 KB |
Output is correct |
11 |
Correct |
440 ms |
809580 KB |
Output is correct |
12 |
Correct |
431 ms |
809708 KB |
Output is correct |
13 |
Correct |
457 ms |
809708 KB |
Output is correct |
14 |
Correct |
437 ms |
809580 KB |
Output is correct |
15 |
Correct |
437 ms |
809580 KB |
Output is correct |
16 |
Correct |
438 ms |
809708 KB |
Output is correct |
17 |
Correct |
443 ms |
809580 KB |
Output is correct |
18 |
Correct |
450 ms |
809584 KB |
Output is correct |
19 |
Correct |
441 ms |
809580 KB |
Output is correct |
20 |
Correct |
434 ms |
809580 KB |
Output is correct |
21 |
Correct |
439 ms |
809708 KB |
Output is correct |
22 |
Correct |
439 ms |
809692 KB |
Output is correct |
23 |
Correct |
434 ms |
809580 KB |
Output is correct |
24 |
Correct |
436 ms |
809580 KB |
Output is correct |
25 |
Correct |
441 ms |
809716 KB |
Output is correct |
26 |
Correct |
439 ms |
809580 KB |
Output is correct |
27 |
Correct |
435 ms |
809580 KB |
Output is correct |
28 |
Correct |
440 ms |
809708 KB |
Output is correct |
29 |
Correct |
437 ms |
809572 KB |
Output is correct |
30 |
Correct |
439 ms |
809680 KB |
Output is correct |
31 |
Correct |
433 ms |
809580 KB |
Output is correct |
32 |
Correct |
440 ms |
809708 KB |
Output is correct |
33 |
Correct |
444 ms |
809580 KB |
Output is correct |
34 |
Correct |
434 ms |
809708 KB |
Output is correct |
35 |
Correct |
458 ms |
809708 KB |
Output is correct |
36 |
Correct |
435 ms |
809580 KB |
Output is correct |
37 |
Correct |
432 ms |
809580 KB |
Output is correct |
38 |
Correct |
440 ms |
809656 KB |
Output is correct |
39 |
Correct |
432 ms |
809708 KB |
Output is correct |
40 |
Correct |
434 ms |
809580 KB |
Output is correct |
41 |
Correct |
440 ms |
809640 KB |
Output is correct |
42 |
Correct |
441 ms |
809708 KB |
Output is correct |
43 |
Correct |
469 ms |
809652 KB |
Output is correct |
44 |
Correct |
438 ms |
809708 KB |
Output is correct |
45 |
Correct |
440 ms |
809724 KB |
Output is correct |
46 |
Correct |
437 ms |
809612 KB |
Output is correct |
47 |
Correct |
438 ms |
809580 KB |
Output is correct |
48 |
Correct |
439 ms |
809580 KB |
Output is correct |
49 |
Correct |
440 ms |
809580 KB |
Output is correct |
50 |
Correct |
437 ms |
809588 KB |
Output is correct |
51 |
Correct |
437 ms |
809708 KB |
Output is correct |
52 |
Correct |
440 ms |
809580 KB |
Output is correct |
53 |
Correct |
436 ms |
809568 KB |
Output is correct |
54 |
Correct |
441 ms |
809616 KB |
Output is correct |
55 |
Correct |
436 ms |
809580 KB |
Output is correct |
56 |
Correct |
440 ms |
809580 KB |
Output is correct |
57 |
Correct |
436 ms |
809804 KB |
Output is correct |
58 |
Correct |
467 ms |
809580 KB |
Output is correct |
59 |
Correct |
483 ms |
809580 KB |
Output is correct |
60 |
Correct |
469 ms |
809836 KB |
Output is correct |
61 |
Correct |
478 ms |
809752 KB |
Output is correct |
62 |
Correct |
439 ms |
809588 KB |
Output is correct |
63 |
Correct |
449 ms |
809648 KB |
Output is correct |
64 |
Correct |
464 ms |
809580 KB |
Output is correct |
65 |
Correct |
465 ms |
809604 KB |
Output is correct |
66 |
Correct |
468 ms |
809648 KB |
Output is correct |
67 |
Correct |
487 ms |
809560 KB |
Output is correct |
68 |
Execution timed out |
504 ms |
809656 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |