#include<bits/stdc++.h>
using namespace std;
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define sz size()
#define x first
#define y second
#define pi pair <int, int>
#define pii pair <int, pi>
#define vi vector <int>
const ll mod = 1e9 + 7;
int n, dp[405][405][405][3], cnt[3];
string s;
vector<pii> v[3];
int get(char c){
if(c=='R') return 0;
else if(c=='G') return 1;
else return 2;
}
int dst(pii a, pii b){
return abs(a.x-b.x)+abs(a.y.x-b.y.x)+abs(a.y.y-b.y.y);
}
int32_t main(){
ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
cin>>n>>s;
for(int i=0; i<n; i++){
int x=get(s[i]);
cnt[x]++;
v[x].pb({cnt[0], {cnt[1], cnt[2]}});
}
int rr=v[0].sz, gg=v[1].sz, yy=v[2].sz;
if(max(rr, max(gg, yy))>(n+1)/2) return cout<<"-1", 0;
for(int r=0; r<=rr; r++){
for(int g=0; g<=gg; g++){
for(int y=0; y<=yy; y++){
int sum=r+g+y;
if(sum==0) continue;
dp[r][g][y][0]=dp[r][g][y][1]=dp[r][g][y][2]=1e9;
if(r) dp[r][g][y][0]=min(dp[r-1][g][y][1], dp[r-1][g][y][2])+dst(v[0][r-1], {r, {g, y}});
if(g) dp[r][g][y][1]=min(dp[r][g-1][y][2], dp[r][g-1][y][0])+dst(v[1][g-1], {r, {g, y}});
if(y) dp[r][g][y][2]=min(dp[r][g][y-1][0], dp[r][g][y-1][1])+dst(v[2][y-1], {r, {g, y}});
}
}
}
cout<<min(dp[rr][gg][yy][0], min(dp[rr][gg][yy][1], dp[rr][gg][yy][2]))/2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2296 KB |
Output is correct |
19 |
Correct |
3 ms |
1912 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1788 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
3 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
2 ms |
380 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
4 ms |
2732 KB |
Output is correct |
36 |
Correct |
3 ms |
1912 KB |
Output is correct |
37 |
Correct |
3 ms |
1784 KB |
Output is correct |
38 |
Correct |
3 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2296 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
123 ms |
163024 KB |
Output is correct |
3 |
Correct |
122 ms |
162160 KB |
Output is correct |
4 |
Correct |
124 ms |
163064 KB |
Output is correct |
5 |
Correct |
125 ms |
163136 KB |
Output is correct |
6 |
Correct |
122 ms |
163064 KB |
Output is correct |
7 |
Correct |
122 ms |
162168 KB |
Output is correct |
8 |
Correct |
122 ms |
162140 KB |
Output is correct |
9 |
Correct |
122 ms |
161400 KB |
Output is correct |
10 |
Correct |
122 ms |
163064 KB |
Output is correct |
11 |
Correct |
124 ms |
163016 KB |
Output is correct |
12 |
Correct |
35 ms |
44024 KB |
Output is correct |
13 |
Correct |
59 ms |
77176 KB |
Output is correct |
14 |
Correct |
92 ms |
111372 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2296 KB |
Output is correct |
19 |
Correct |
3 ms |
1912 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1788 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
3 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
2 ms |
380 KB |
Output is correct |
34 |
Correct |
2 ms |
376 KB |
Output is correct |
35 |
Correct |
4 ms |
2732 KB |
Output is correct |
36 |
Correct |
3 ms |
1912 KB |
Output is correct |
37 |
Correct |
3 ms |
1784 KB |
Output is correct |
38 |
Correct |
3 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2296 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
123 ms |
163024 KB |
Output is correct |
44 |
Correct |
122 ms |
162160 KB |
Output is correct |
45 |
Correct |
124 ms |
163064 KB |
Output is correct |
46 |
Correct |
125 ms |
163136 KB |
Output is correct |
47 |
Correct |
122 ms |
163064 KB |
Output is correct |
48 |
Correct |
122 ms |
162168 KB |
Output is correct |
49 |
Correct |
122 ms |
162140 KB |
Output is correct |
50 |
Correct |
122 ms |
161400 KB |
Output is correct |
51 |
Correct |
122 ms |
163064 KB |
Output is correct |
52 |
Correct |
124 ms |
163016 KB |
Output is correct |
53 |
Correct |
35 ms |
44024 KB |
Output is correct |
54 |
Correct |
59 ms |
77176 KB |
Output is correct |
55 |
Correct |
92 ms |
111372 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
84 ms |
76280 KB |
Output is correct |
59 |
Correct |
100 ms |
96760 KB |
Output is correct |
60 |
Correct |
93 ms |
87792 KB |
Output is correct |
61 |
Correct |
91 ms |
84940 KB |
Output is correct |
62 |
Correct |
2 ms |
376 KB |
Output is correct |
63 |
Correct |
124 ms |
160272 KB |
Output is correct |
64 |
Correct |
127 ms |
150168 KB |
Output is correct |
65 |
Correct |
124 ms |
138616 KB |
Output is correct |
66 |
Correct |
91 ms |
86136 KB |
Output is correct |
67 |
Correct |
89 ms |
83064 KB |
Output is correct |
68 |
Correct |
94 ms |
88696 KB |
Output is correct |
69 |
Correct |
92 ms |
85496 KB |
Output is correct |
70 |
Correct |
100 ms |
92668 KB |
Output is correct |
71 |
Correct |
92 ms |
84088 KB |
Output is correct |
72 |
Correct |
2 ms |
376 KB |
Output is correct |
73 |
Correct |
2 ms |
416 KB |
Output is correct |