#include<bits/stdc++.h>
using namespace std;
#define ll int
#define pii pair<ll,ll>
#define pb push_back
#define fi first
#define se second
const ll N = 403;
const ll INF = 1e9;
ll n,r,g,b;
string s;
vector<pii> pos[3];
ll dp[N][N][N][3];
int main() {
cin >> n >> s;
s = '!' + s;
for(int i=1; i<=n; i++) {
if(s[i] == 'R') {
r++;
pos[0].pb({g,b});
}
else if(s[i] == 'G') {
g++;
pos[1].pb({r,b});
}
else if(s[i] == 'Y') {
b++;
pos[2].pb({r,g});
}
}
for(int i=0; i<=r; i++) {
for(int j=0; j<=g; j++) {
for(int k=0; k<=b; k++) {
for(int l=0; l<=2; l++) dp[i][j][k][l] = INF;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for(int i=0; i<=r; i++) {
for(int j=0; j<=g; j++) {
for(int k=0; k<=b; k++) {
if(i) {
dp[i][j][k][0] = min(dp[i-1][j][k][1],dp[i-1][j][k][2])
+ max(0,pos[0][i-1].fi - j) + max(0,pos[0][i-1].se - k);
}
if(j) {
dp[i][j][k][1] = min(dp[i][j-1][k][0],dp[i][j-1][k][2])
+ max(0,pos[1][j-1].fi - i) + max(0,pos[1][j-1].se - k);
}
if(k) {
dp[i][j][k][2] = min(dp[i][j][k-1][0],dp[i][j][k-1][1])
+ max(0,pos[2][k-1].fi - i) + max(0,pos[2][k-1].se - j);
}
}
}
}
ll ans = min({dp[r][g][b][0],dp[r][g][b][1],dp[r][g][b][2]});
if(ans >= INF) cout << -1 << endl;
else cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10720 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10720 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
45660 KB |
Output is correct |
19 |
Correct |
4 ms |
37212 KB |
Output is correct |
20 |
Correct |
6 ms |
49748 KB |
Output is correct |
21 |
Correct |
4 ms |
39516 KB |
Output is correct |
22 |
Correct |
5 ms |
47452 KB |
Output is correct |
23 |
Correct |
4 ms |
33372 KB |
Output is correct |
24 |
Correct |
3 ms |
21068 KB |
Output is correct |
25 |
Correct |
10 ms |
72284 KB |
Output is correct |
26 |
Correct |
7 ms |
59996 KB |
Output is correct |
27 |
Correct |
6 ms |
53876 KB |
Output is correct |
28 |
Correct |
5 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43612 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
4 ms |
29276 KB |
Output is correct |
32 |
Correct |
5 ms |
35420 KB |
Output is correct |
33 |
Correct |
6 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43632 KB |
Output is correct |
35 |
Correct |
5 ms |
45656 KB |
Output is correct |
36 |
Correct |
4 ms |
39260 KB |
Output is correct |
37 |
Correct |
3 ms |
27192 KB |
Output is correct |
38 |
Correct |
3 ms |
27228 KB |
Output is correct |
39 |
Correct |
5 ms |
37468 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
51 ms |
210172 KB |
Output is correct |
3 |
Correct |
55 ms |
225060 KB |
Output is correct |
4 |
Correct |
54 ms |
228000 KB |
Output is correct |
5 |
Correct |
48 ms |
240972 KB |
Output is correct |
6 |
Correct |
47 ms |
247728 KB |
Output is correct |
7 |
Correct |
47 ms |
246868 KB |
Output is correct |
8 |
Correct |
47 ms |
246920 KB |
Output is correct |
9 |
Correct |
49 ms |
246808 KB |
Output is correct |
10 |
Correct |
47 ms |
247832 KB |
Output is correct |
11 |
Correct |
47 ms |
247632 KB |
Output is correct |
12 |
Correct |
19 ms |
159168 KB |
Output is correct |
13 |
Correct |
26 ms |
181688 KB |
Output is correct |
14 |
Correct |
34 ms |
207196 KB |
Output is correct |
15 |
Correct |
54 ms |
247400 KB |
Output is correct |
16 |
Correct |
46 ms |
247432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10720 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
45660 KB |
Output is correct |
19 |
Correct |
4 ms |
37212 KB |
Output is correct |
20 |
Correct |
6 ms |
49748 KB |
Output is correct |
21 |
Correct |
4 ms |
39516 KB |
Output is correct |
22 |
Correct |
5 ms |
47452 KB |
Output is correct |
23 |
Correct |
4 ms |
33372 KB |
Output is correct |
24 |
Correct |
3 ms |
21068 KB |
Output is correct |
25 |
Correct |
10 ms |
72284 KB |
Output is correct |
26 |
Correct |
7 ms |
59996 KB |
Output is correct |
27 |
Correct |
6 ms |
53876 KB |
Output is correct |
28 |
Correct |
5 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43612 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
4 ms |
29276 KB |
Output is correct |
32 |
Correct |
5 ms |
35420 KB |
Output is correct |
33 |
Correct |
6 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43632 KB |
Output is correct |
35 |
Correct |
5 ms |
45656 KB |
Output is correct |
36 |
Correct |
4 ms |
39260 KB |
Output is correct |
37 |
Correct |
3 ms |
27192 KB |
Output is correct |
38 |
Correct |
3 ms |
27228 KB |
Output is correct |
39 |
Correct |
5 ms |
37468 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
19036 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
51 ms |
210172 KB |
Output is correct |
44 |
Correct |
55 ms |
225060 KB |
Output is correct |
45 |
Correct |
54 ms |
228000 KB |
Output is correct |
46 |
Correct |
48 ms |
240972 KB |
Output is correct |
47 |
Correct |
47 ms |
247728 KB |
Output is correct |
48 |
Correct |
47 ms |
246868 KB |
Output is correct |
49 |
Correct |
47 ms |
246920 KB |
Output is correct |
50 |
Correct |
49 ms |
246808 KB |
Output is correct |
51 |
Correct |
47 ms |
247832 KB |
Output is correct |
52 |
Correct |
47 ms |
247632 KB |
Output is correct |
53 |
Correct |
19 ms |
159168 KB |
Output is correct |
54 |
Correct |
26 ms |
181688 KB |
Output is correct |
55 |
Correct |
34 ms |
207196 KB |
Output is correct |
56 |
Correct |
54 ms |
247400 KB |
Output is correct |
57 |
Correct |
46 ms |
247432 KB |
Output is correct |
58 |
Correct |
39 ms |
176396 KB |
Output is correct |
59 |
Correct |
44 ms |
192596 KB |
Output is correct |
60 |
Correct |
43 ms |
187728 KB |
Output is correct |
61 |
Correct |
41 ms |
183384 KB |
Output is correct |
62 |
Correct |
51 ms |
250388 KB |
Output is correct |
63 |
Correct |
50 ms |
244548 KB |
Output is correct |
64 |
Correct |
59 ms |
235864 KB |
Output is correct |
65 |
Correct |
54 ms |
221512 KB |
Output is correct |
66 |
Correct |
42 ms |
187880 KB |
Output is correct |
67 |
Correct |
38 ms |
176252 KB |
Output is correct |
68 |
Correct |
42 ms |
189016 KB |
Output is correct |
69 |
Correct |
41 ms |
187480 KB |
Output is correct |
70 |
Correct |
43 ms |
190172 KB |
Output is correct |
71 |
Correct |
45 ms |
193192 KB |
Output is correct |
72 |
Correct |
31 ms |
182608 KB |
Output is correct |
73 |
Correct |
2 ms |
8796 KB |
Output is correct |