#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll dp[3][405][405][405];
const ll big = 1e18;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n;
string s;
cin >> n >> s;
vector<ll> cnt(3,0);
vector<ll> a(n);
vector<vector<ll>> ps(3);
for(ll i = 0;i<n;i++) {
a[i] = s[i]=='R'?0:s[i]=='G'?1:2;
cnt[a[i]]++;
ps[a[i]].push_back(i+1);
}
for(ll i : cnt){
if (i>(n+1)/2){
cout << "-1\n";
return 0;
}
}
vector<vector<ll>> c(3,vector<ll>(n+1,0));
for(ll i = 0;i<n;i++){
for(ll j = 0;j<3;j++){
c[j][i+1] = c[j][i] + (a[i]==j);
}
}
dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0;
for(ll i = 0;i<=cnt[0];i++){
for(ll j = 0;j<=cnt[1];j++){
for(ll k = 0;k<=cnt[2];k++){
if (!(i|j|k)) continue;
if(i>0) dp[0][i][j][k] = min(dp[1][i-1][j][k],dp[2][i-1][j][k])+max(c[0][ps[0][i-1]]-i,0ll)+max(c[1][ps[0][i-1]]-j,0ll)+max(c[2][ps[0][i-1]]-k,0ll);
else dp[0][i][j][k] = big;
if(j>0) dp[1][i][j][k] = min(dp[0][i][j-1][k],dp[2][i][j-1][k])+max(c[0][ps[1][j-1]]-i,0ll)+max(c[1][ps[1][j-1]]-j,0ll)+max(c[2][ps[1][j-1]]-k,0ll);
else dp[1][i][j][k] = big;
if(k>0) dp[2][i][j][k] = min(dp[0][i][j][k-1],dp[1][i][j][k-1])+max(c[0][ps[2][k-1]]-i,0ll)+max(c[1][ps[2][k-1]]-j,0ll)+max(c[2][ps[2][k-1]]-k,0ll);
else dp[2][i][j][k] = big;
}
}
}
ll ans = big;
for(ll i = 0;i<3;i++) ans = min(ans,dp[i][cnt[0]][cnt[1]][cnt[2]]);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8536 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
16728 KB |
Output is correct |
5 |
Correct |
3 ms |
24924 KB |
Output is correct |
6 |
Correct |
3 ms |
29016 KB |
Output is correct |
7 |
Correct |
4 ms |
33116 KB |
Output is correct |
8 |
Correct |
4 ms |
29016 KB |
Output is correct |
9 |
Correct |
4 ms |
29020 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
24924 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
4 ms |
29020 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8536 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
16728 KB |
Output is correct |
5 |
Correct |
3 ms |
24924 KB |
Output is correct |
6 |
Correct |
3 ms |
29016 KB |
Output is correct |
7 |
Correct |
4 ms |
33116 KB |
Output is correct |
8 |
Correct |
4 ms |
29016 KB |
Output is correct |
9 |
Correct |
4 ms |
29020 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
24924 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
4 ms |
29020 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
13 ms |
94812 KB |
Output is correct |
19 |
Correct |
9 ms |
78300 KB |
Output is correct |
20 |
Correct |
11 ms |
100956 KB |
Output is correct |
21 |
Correct |
9 ms |
82524 KB |
Output is correct |
22 |
Correct |
13 ms |
96856 KB |
Output is correct |
23 |
Correct |
8 ms |
70448 KB |
Output is correct |
24 |
Correct |
6 ms |
45660 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
12 ms |
109148 KB |
Output is correct |
28 |
Correct |
10 ms |
86576 KB |
Output is correct |
29 |
Correct |
10 ms |
90716 KB |
Output is correct |
30 |
Correct |
9 ms |
70236 KB |
Output is correct |
31 |
Correct |
8 ms |
64092 KB |
Output is correct |
32 |
Correct |
9 ms |
76380 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
10 ms |
94808 KB |
Output is correct |
36 |
Correct |
9 ms |
82524 KB |
Output is correct |
37 |
Correct |
6 ms |
55900 KB |
Output is correct |
38 |
Correct |
7 ms |
59996 KB |
Output is correct |
39 |
Correct |
9 ms |
78428 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
160 ms |
475248 KB |
Output is correct |
3 |
Correct |
123 ms |
474960 KB |
Output is correct |
4 |
Correct |
122 ms |
469884 KB |
Output is correct |
5 |
Correct |
162 ms |
473040 KB |
Output is correct |
6 |
Correct |
117 ms |
468160 KB |
Output is correct |
7 |
Correct |
116 ms |
470052 KB |
Output is correct |
8 |
Correct |
120 ms |
468148 KB |
Output is correct |
9 |
Correct |
116 ms |
468908 KB |
Output is correct |
10 |
Correct |
117 ms |
471872 KB |
Output is correct |
11 |
Correct |
120 ms |
471928 KB |
Output is correct |
12 |
Correct |
45 ms |
219868 KB |
Output is correct |
13 |
Correct |
63 ms |
293380 KB |
Output is correct |
14 |
Correct |
84 ms |
361976 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8536 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
16728 KB |
Output is correct |
5 |
Correct |
3 ms |
24924 KB |
Output is correct |
6 |
Correct |
3 ms |
29016 KB |
Output is correct |
7 |
Correct |
4 ms |
33116 KB |
Output is correct |
8 |
Correct |
4 ms |
29016 KB |
Output is correct |
9 |
Correct |
4 ms |
29020 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
24924 KB |
Output is correct |
12 |
Correct |
4 ms |
24924 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
4 ms |
29020 KB |
Output is correct |
15 |
Correct |
3 ms |
20828 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
13 ms |
94812 KB |
Output is correct |
19 |
Correct |
9 ms |
78300 KB |
Output is correct |
20 |
Correct |
11 ms |
100956 KB |
Output is correct |
21 |
Correct |
9 ms |
82524 KB |
Output is correct |
22 |
Correct |
13 ms |
96856 KB |
Output is correct |
23 |
Correct |
8 ms |
70448 KB |
Output is correct |
24 |
Correct |
6 ms |
45660 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
12 ms |
109148 KB |
Output is correct |
28 |
Correct |
10 ms |
86576 KB |
Output is correct |
29 |
Correct |
10 ms |
90716 KB |
Output is correct |
30 |
Correct |
9 ms |
70236 KB |
Output is correct |
31 |
Correct |
8 ms |
64092 KB |
Output is correct |
32 |
Correct |
9 ms |
76380 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
10 ms |
94808 KB |
Output is correct |
36 |
Correct |
9 ms |
82524 KB |
Output is correct |
37 |
Correct |
6 ms |
55900 KB |
Output is correct |
38 |
Correct |
7 ms |
59996 KB |
Output is correct |
39 |
Correct |
9 ms |
78428 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
2 ms |
12636 KB |
Output is correct |
43 |
Correct |
160 ms |
475248 KB |
Output is correct |
44 |
Correct |
123 ms |
474960 KB |
Output is correct |
45 |
Correct |
122 ms |
469884 KB |
Output is correct |
46 |
Correct |
162 ms |
473040 KB |
Output is correct |
47 |
Correct |
117 ms |
468160 KB |
Output is correct |
48 |
Correct |
116 ms |
470052 KB |
Output is correct |
49 |
Correct |
120 ms |
468148 KB |
Output is correct |
50 |
Correct |
116 ms |
468908 KB |
Output is correct |
51 |
Correct |
117 ms |
471872 KB |
Output is correct |
52 |
Correct |
120 ms |
471928 KB |
Output is correct |
53 |
Correct |
45 ms |
219868 KB |
Output is correct |
54 |
Correct |
63 ms |
293380 KB |
Output is correct |
55 |
Correct |
84 ms |
361976 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
73 ms |
263688 KB |
Output is correct |
59 |
Correct |
87 ms |
306768 KB |
Output is correct |
60 |
Correct |
75 ms |
285404 KB |
Output is correct |
61 |
Correct |
75 ms |
258776 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
115 ms |
452024 KB |
Output is correct |
64 |
Correct |
113 ms |
419136 KB |
Output is correct |
65 |
Correct |
98 ms |
368212 KB |
Output is correct |
66 |
Correct |
94 ms |
258796 KB |
Output is correct |
67 |
Correct |
69 ms |
267060 KB |
Output is correct |
68 |
Correct |
103 ms |
268116 KB |
Output is correct |
69 |
Correct |
74 ms |
289620 KB |
Output is correct |
70 |
Correct |
81 ms |
298656 KB |
Output is correct |
71 |
Correct |
112 ms |
292928 KB |
Output is correct |
72 |
Correct |
0 ms |
344 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |