#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[5];
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(pii(g,b));
}
else if(s[i] == 'G') {
g++;
pos[1].pb(pii(r,b));
}
else if(s[i] == 'Y') {
b++;
pos[2].pb(pii(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<3; 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],min(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 |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
12632 KB |
Output is correct |
7 |
Correct |
2 ms |
14680 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 |
1 ms |
10724 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
12632 KB |
Output is correct |
7 |
Correct |
2 ms |
14680 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 |
1 ms |
10724 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
45660 KB |
Output is correct |
19 |
Correct |
4 ms |
37396 KB |
Output is correct |
20 |
Correct |
5 ms |
49752 KB |
Output is correct |
21 |
Correct |
4 ms |
39516 KB |
Output is correct |
22 |
Correct |
5 ms |
47676 KB |
Output is correct |
23 |
Correct |
4 ms |
33588 KB |
Output is correct |
24 |
Correct |
3 ms |
20824 KB |
Output is correct |
25 |
Correct |
8 ms |
72280 KB |
Output is correct |
26 |
Correct |
6 ms |
59996 KB |
Output is correct |
27 |
Correct |
6 ms |
53852 KB |
Output is correct |
28 |
Correct |
5 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43592 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
3 ms |
29020 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
5 ms |
49800 KB |
Output is correct |
34 |
Correct |
5 ms |
43612 KB |
Output is correct |
35 |
Correct |
5 ms |
45660 KB |
Output is correct |
36 |
Correct |
5 ms |
39260 KB |
Output is correct |
37 |
Correct |
3 ms |
26972 KB |
Output is correct |
38 |
Correct |
3 ms |
27224 KB |
Output is correct |
39 |
Correct |
4 ms |
37464 KB |
Output is correct |
40 |
Correct |
1 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 |
45 ms |
328272 KB |
Output is correct |
3 |
Correct |
41 ms |
331856 KB |
Output is correct |
4 |
Correct |
46 ms |
331856 KB |
Output is correct |
5 |
Correct |
44 ms |
330684 KB |
Output is correct |
6 |
Correct |
42 ms |
330572 KB |
Output is correct |
7 |
Correct |
42 ms |
329812 KB |
Output is correct |
8 |
Correct |
41 ms |
331092 KB |
Output is correct |
9 |
Correct |
41 ms |
329812 KB |
Output is correct |
10 |
Correct |
41 ms |
331856 KB |
Output is correct |
11 |
Correct |
41 ms |
331856 KB |
Output is correct |
12 |
Correct |
20 ms |
197456 KB |
Output is correct |
13 |
Correct |
27 ms |
263260 KB |
Output is correct |
14 |
Correct |
32 ms |
304468 KB |
Output is correct |
15 |
Correct |
41 ms |
332368 KB |
Output is correct |
16 |
Correct |
41 ms |
333652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
12632 KB |
Output is correct |
7 |
Correct |
2 ms |
14680 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 |
1 ms |
10724 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
45660 KB |
Output is correct |
19 |
Correct |
4 ms |
37396 KB |
Output is correct |
20 |
Correct |
5 ms |
49752 KB |
Output is correct |
21 |
Correct |
4 ms |
39516 KB |
Output is correct |
22 |
Correct |
5 ms |
47676 KB |
Output is correct |
23 |
Correct |
4 ms |
33588 KB |
Output is correct |
24 |
Correct |
3 ms |
20824 KB |
Output is correct |
25 |
Correct |
8 ms |
72280 KB |
Output is correct |
26 |
Correct |
6 ms |
59996 KB |
Output is correct |
27 |
Correct |
6 ms |
53852 KB |
Output is correct |
28 |
Correct |
5 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43592 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
3 ms |
29020 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
5 ms |
49800 KB |
Output is correct |
34 |
Correct |
5 ms |
43612 KB |
Output is correct |
35 |
Correct |
5 ms |
45660 KB |
Output is correct |
36 |
Correct |
5 ms |
39260 KB |
Output is correct |
37 |
Correct |
3 ms |
26972 KB |
Output is correct |
38 |
Correct |
3 ms |
27224 KB |
Output is correct |
39 |
Correct |
4 ms |
37464 KB |
Output is correct |
40 |
Correct |
1 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 |
45 ms |
328272 KB |
Output is correct |
44 |
Correct |
41 ms |
331856 KB |
Output is correct |
45 |
Correct |
46 ms |
331856 KB |
Output is correct |
46 |
Correct |
44 ms |
330684 KB |
Output is correct |
47 |
Correct |
42 ms |
330572 KB |
Output is correct |
48 |
Correct |
42 ms |
329812 KB |
Output is correct |
49 |
Correct |
41 ms |
331092 KB |
Output is correct |
50 |
Correct |
41 ms |
329812 KB |
Output is correct |
51 |
Correct |
41 ms |
331856 KB |
Output is correct |
52 |
Correct |
41 ms |
331856 KB |
Output is correct |
53 |
Correct |
20 ms |
197456 KB |
Output is correct |
54 |
Correct |
27 ms |
263260 KB |
Output is correct |
55 |
Correct |
32 ms |
304468 KB |
Output is correct |
56 |
Correct |
41 ms |
332368 KB |
Output is correct |
57 |
Correct |
41 ms |
333652 KB |
Output is correct |
58 |
Correct |
37 ms |
236376 KB |
Output is correct |
59 |
Correct |
43 ms |
277588 KB |
Output is correct |
60 |
Correct |
41 ms |
273492 KB |
Output is correct |
61 |
Correct |
40 ms |
254804 KB |
Output is correct |
62 |
Correct |
51 ms |
342900 KB |
Output is correct |
63 |
Correct |
48 ms |
331604 KB |
Output is correct |
64 |
Correct |
48 ms |
324432 KB |
Output is correct |
65 |
Correct |
44 ms |
302416 KB |
Output is correct |
66 |
Correct |
40 ms |
261204 KB |
Output is correct |
67 |
Correct |
35 ms |
215900 KB |
Output is correct |
68 |
Correct |
44 ms |
261200 KB |
Output is correct |
69 |
Correct |
44 ms |
261212 KB |
Output is correct |
70 |
Correct |
40 ms |
259156 KB |
Output is correct |
71 |
Correct |
44 ms |
290132 KB |
Output is correct |
72 |
Correct |
41 ms |
319756 KB |
Output is correct |
73 |
Correct |
2 ms |
8796 KB |
Output is correct |