///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 + 3;
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 = min(dp[1][a][b][c], dp[2][a][b][c]);
ca += Calc(0, a, b, c);
upd(dp[0][a + 1][b][c], ca);
}
if(b < cnt[1][n]){
ll ca = min(dp[0][a][b][c], dp[2][a][b][c]);
ca += Calc(1, a, b, c);
upd(dp[1][a][b + 1][c], ca);
}
if(c < cnt[2][n]){
ll ca = min(dp[0][a][b][c], dp[1][a][b][c]);
ca += Calc(2, a, b, c);
upd(dp[2][a][b][c + 1], 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 |
415 ms |
768876 KB |
Output is correct |
2 |
Correct |
420 ms |
768892 KB |
Output is correct |
3 |
Correct |
414 ms |
768876 KB |
Output is correct |
4 |
Correct |
422 ms |
768876 KB |
Output is correct |
5 |
Correct |
415 ms |
768876 KB |
Output is correct |
6 |
Correct |
413 ms |
768876 KB |
Output is correct |
7 |
Correct |
421 ms |
768932 KB |
Output is correct |
8 |
Correct |
413 ms |
768876 KB |
Output is correct |
9 |
Correct |
414 ms |
768912 KB |
Output is correct |
10 |
Correct |
428 ms |
768780 KB |
Output is correct |
11 |
Correct |
424 ms |
769004 KB |
Output is correct |
12 |
Correct |
414 ms |
768812 KB |
Output is correct |
13 |
Correct |
419 ms |
768876 KB |
Output is correct |
14 |
Correct |
422 ms |
768876 KB |
Output is correct |
15 |
Correct |
417 ms |
768952 KB |
Output is correct |
16 |
Correct |
428 ms |
768876 KB |
Output is correct |
17 |
Correct |
420 ms |
768876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
415 ms |
768876 KB |
Output is correct |
2 |
Correct |
420 ms |
768892 KB |
Output is correct |
3 |
Correct |
414 ms |
768876 KB |
Output is correct |
4 |
Correct |
422 ms |
768876 KB |
Output is correct |
5 |
Correct |
415 ms |
768876 KB |
Output is correct |
6 |
Correct |
413 ms |
768876 KB |
Output is correct |
7 |
Correct |
421 ms |
768932 KB |
Output is correct |
8 |
Correct |
413 ms |
768876 KB |
Output is correct |
9 |
Correct |
414 ms |
768912 KB |
Output is correct |
10 |
Correct |
428 ms |
768780 KB |
Output is correct |
11 |
Correct |
424 ms |
769004 KB |
Output is correct |
12 |
Correct |
414 ms |
768812 KB |
Output is correct |
13 |
Correct |
419 ms |
768876 KB |
Output is correct |
14 |
Correct |
422 ms |
768876 KB |
Output is correct |
15 |
Correct |
417 ms |
768952 KB |
Output is correct |
16 |
Correct |
428 ms |
768876 KB |
Output is correct |
17 |
Correct |
420 ms |
768876 KB |
Output is correct |
18 |
Correct |
422 ms |
768876 KB |
Output is correct |
19 |
Correct |
425 ms |
768876 KB |
Output is correct |
20 |
Correct |
420 ms |
768876 KB |
Output is correct |
21 |
Correct |
420 ms |
768876 KB |
Output is correct |
22 |
Correct |
414 ms |
768908 KB |
Output is correct |
23 |
Correct |
421 ms |
768876 KB |
Output is correct |
24 |
Correct |
418 ms |
768876 KB |
Output is correct |
25 |
Correct |
421 ms |
768876 KB |
Output is correct |
26 |
Correct |
418 ms |
768876 KB |
Output is correct |
27 |
Correct |
418 ms |
768876 KB |
Output is correct |
28 |
Correct |
415 ms |
768876 KB |
Output is correct |
29 |
Correct |
422 ms |
768876 KB |
Output is correct |
30 |
Correct |
419 ms |
769132 KB |
Output is correct |
31 |
Correct |
421 ms |
768964 KB |
Output is correct |
32 |
Correct |
418 ms |
768876 KB |
Output is correct |
33 |
Correct |
436 ms |
768844 KB |
Output is correct |
34 |
Correct |
417 ms |
768876 KB |
Output is correct |
35 |
Correct |
418 ms |
768876 KB |
Output is correct |
36 |
Correct |
418 ms |
768876 KB |
Output is correct |
37 |
Correct |
432 ms |
768932 KB |
Output is correct |
38 |
Correct |
424 ms |
768876 KB |
Output is correct |
39 |
Correct |
427 ms |
768892 KB |
Output is correct |
40 |
Correct |
411 ms |
768876 KB |
Output is correct |
41 |
Correct |
435 ms |
768836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
431 ms |
768876 KB |
Output is correct |
2 |
Correct |
449 ms |
768864 KB |
Output is correct |
3 |
Correct |
430 ms |
768876 KB |
Output is correct |
4 |
Correct |
429 ms |
768884 KB |
Output is correct |
5 |
Correct |
423 ms |
768880 KB |
Output is correct |
6 |
Correct |
422 ms |
768876 KB |
Output is correct |
7 |
Correct |
419 ms |
768876 KB |
Output is correct |
8 |
Correct |
422 ms |
768792 KB |
Output is correct |
9 |
Correct |
420 ms |
768892 KB |
Output is correct |
10 |
Correct |
423 ms |
768876 KB |
Output is correct |
11 |
Correct |
418 ms |
768876 KB |
Output is correct |
12 |
Correct |
419 ms |
769132 KB |
Output is correct |
13 |
Correct |
429 ms |
768876 KB |
Output is correct |
14 |
Correct |
418 ms |
768876 KB |
Output is correct |
15 |
Correct |
419 ms |
769004 KB |
Output is correct |
16 |
Correct |
422 ms |
768876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
415 ms |
768876 KB |
Output is correct |
2 |
Correct |
420 ms |
768892 KB |
Output is correct |
3 |
Correct |
414 ms |
768876 KB |
Output is correct |
4 |
Correct |
422 ms |
768876 KB |
Output is correct |
5 |
Correct |
415 ms |
768876 KB |
Output is correct |
6 |
Correct |
413 ms |
768876 KB |
Output is correct |
7 |
Correct |
421 ms |
768932 KB |
Output is correct |
8 |
Correct |
413 ms |
768876 KB |
Output is correct |
9 |
Correct |
414 ms |
768912 KB |
Output is correct |
10 |
Correct |
428 ms |
768780 KB |
Output is correct |
11 |
Correct |
424 ms |
769004 KB |
Output is correct |
12 |
Correct |
414 ms |
768812 KB |
Output is correct |
13 |
Correct |
419 ms |
768876 KB |
Output is correct |
14 |
Correct |
422 ms |
768876 KB |
Output is correct |
15 |
Correct |
417 ms |
768952 KB |
Output is correct |
16 |
Correct |
428 ms |
768876 KB |
Output is correct |
17 |
Correct |
420 ms |
768876 KB |
Output is correct |
18 |
Correct |
422 ms |
768876 KB |
Output is correct |
19 |
Correct |
425 ms |
768876 KB |
Output is correct |
20 |
Correct |
420 ms |
768876 KB |
Output is correct |
21 |
Correct |
420 ms |
768876 KB |
Output is correct |
22 |
Correct |
414 ms |
768908 KB |
Output is correct |
23 |
Correct |
421 ms |
768876 KB |
Output is correct |
24 |
Correct |
418 ms |
768876 KB |
Output is correct |
25 |
Correct |
421 ms |
768876 KB |
Output is correct |
26 |
Correct |
418 ms |
768876 KB |
Output is correct |
27 |
Correct |
418 ms |
768876 KB |
Output is correct |
28 |
Correct |
415 ms |
768876 KB |
Output is correct |
29 |
Correct |
422 ms |
768876 KB |
Output is correct |
30 |
Correct |
419 ms |
769132 KB |
Output is correct |
31 |
Correct |
421 ms |
768964 KB |
Output is correct |
32 |
Correct |
418 ms |
768876 KB |
Output is correct |
33 |
Correct |
436 ms |
768844 KB |
Output is correct |
34 |
Correct |
417 ms |
768876 KB |
Output is correct |
35 |
Correct |
418 ms |
768876 KB |
Output is correct |
36 |
Correct |
418 ms |
768876 KB |
Output is correct |
37 |
Correct |
432 ms |
768932 KB |
Output is correct |
38 |
Correct |
424 ms |
768876 KB |
Output is correct |
39 |
Correct |
427 ms |
768892 KB |
Output is correct |
40 |
Correct |
411 ms |
768876 KB |
Output is correct |
41 |
Correct |
435 ms |
768836 KB |
Output is correct |
42 |
Correct |
431 ms |
768876 KB |
Output is correct |
43 |
Correct |
449 ms |
768864 KB |
Output is correct |
44 |
Correct |
430 ms |
768876 KB |
Output is correct |
45 |
Correct |
429 ms |
768884 KB |
Output is correct |
46 |
Correct |
423 ms |
768880 KB |
Output is correct |
47 |
Correct |
422 ms |
768876 KB |
Output is correct |
48 |
Correct |
419 ms |
768876 KB |
Output is correct |
49 |
Correct |
422 ms |
768792 KB |
Output is correct |
50 |
Correct |
420 ms |
768892 KB |
Output is correct |
51 |
Correct |
423 ms |
768876 KB |
Output is correct |
52 |
Correct |
418 ms |
768876 KB |
Output is correct |
53 |
Correct |
419 ms |
769132 KB |
Output is correct |
54 |
Correct |
429 ms |
768876 KB |
Output is correct |
55 |
Correct |
418 ms |
768876 KB |
Output is correct |
56 |
Correct |
419 ms |
769004 KB |
Output is correct |
57 |
Correct |
422 ms |
768876 KB |
Output is correct |
58 |
Correct |
458 ms |
769004 KB |
Output is correct |
59 |
Correct |
455 ms |
768876 KB |
Output is correct |
60 |
Correct |
455 ms |
768876 KB |
Output is correct |
61 |
Correct |
456 ms |
769004 KB |
Output is correct |
62 |
Correct |
422 ms |
768876 KB |
Output is correct |
63 |
Correct |
428 ms |
768948 KB |
Output is correct |
64 |
Correct |
437 ms |
768876 KB |
Output is correct |
65 |
Correct |
440 ms |
768876 KB |
Output is correct |
66 |
Correct |
452 ms |
768840 KB |
Output is correct |
67 |
Correct |
456 ms |
768816 KB |
Output is correct |
68 |
Correct |
456 ms |
769004 KB |
Output is correct |
69 |
Correct |
451 ms |
768876 KB |
Output is correct |
70 |
Correct |
451 ms |
768876 KB |
Output is correct |
71 |
Correct |
452 ms |
769004 KB |
Output is correct |
72 |
Correct |
417 ms |
768876 KB |
Output is correct |
73 |
Correct |
418 ms |
768876 KB |
Output is correct |