#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));
}
}
if(r > (n+1)/2 || g > (n+1)/2 || b > (n+1)/2) {
cout << -1 << endl;
return 0;
}
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]));
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
12756 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
12756 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
45656 KB |
Output is correct |
19 |
Correct |
5 ms |
37208 KB |
Output is correct |
20 |
Correct |
6 ms |
49752 KB |
Output is correct |
21 |
Correct |
5 ms |
39516 KB |
Output is correct |
22 |
Correct |
5 ms |
47664 KB |
Output is correct |
23 |
Correct |
3 ms |
33372 KB |
Output is correct |
24 |
Correct |
2 ms |
20828 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
600 KB |
Output is correct |
27 |
Correct |
7 ms |
53864 KB |
Output is correct |
28 |
Correct |
4 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43584 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
4 ms |
29276 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
5 ms |
45636 KB |
Output is correct |
36 |
Correct |
4 ms |
39472 KB |
Output is correct |
37 |
Correct |
4 ms |
26972 KB |
Output is correct |
38 |
Correct |
3 ms |
27228 KB |
Output is correct |
39 |
Correct |
4 ms |
37464 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 |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
66 ms |
335412 KB |
Output is correct |
3 |
Correct |
42 ms |
338516 KB |
Output is correct |
4 |
Correct |
41 ms |
337500 KB |
Output is correct |
5 |
Correct |
41 ms |
337700 KB |
Output is correct |
6 |
Correct |
40 ms |
337492 KB |
Output is correct |
7 |
Correct |
41 ms |
336724 KB |
Output is correct |
8 |
Correct |
41 ms |
336724 KB |
Output is correct |
9 |
Correct |
39 ms |
337644 KB |
Output is correct |
10 |
Correct |
41 ms |
338772 KB |
Output is correct |
11 |
Correct |
41 ms |
337696 KB |
Output is correct |
12 |
Correct |
19 ms |
197468 KB |
Output is correct |
13 |
Correct |
25 ms |
263004 KB |
Output is correct |
14 |
Correct |
31 ms |
309584 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
1 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
12756 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
45656 KB |
Output is correct |
19 |
Correct |
5 ms |
37208 KB |
Output is correct |
20 |
Correct |
6 ms |
49752 KB |
Output is correct |
21 |
Correct |
5 ms |
39516 KB |
Output is correct |
22 |
Correct |
5 ms |
47664 KB |
Output is correct |
23 |
Correct |
3 ms |
33372 KB |
Output is correct |
24 |
Correct |
2 ms |
20828 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
600 KB |
Output is correct |
27 |
Correct |
7 ms |
53864 KB |
Output is correct |
28 |
Correct |
4 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43584 KB |
Output is correct |
30 |
Correct |
4 ms |
33372 KB |
Output is correct |
31 |
Correct |
4 ms |
29276 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
5 ms |
45636 KB |
Output is correct |
36 |
Correct |
4 ms |
39472 KB |
Output is correct |
37 |
Correct |
4 ms |
26972 KB |
Output is correct |
38 |
Correct |
3 ms |
27228 KB |
Output is correct |
39 |
Correct |
4 ms |
37464 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
66 ms |
335412 KB |
Output is correct |
44 |
Correct |
42 ms |
338516 KB |
Output is correct |
45 |
Correct |
41 ms |
337500 KB |
Output is correct |
46 |
Correct |
41 ms |
337700 KB |
Output is correct |
47 |
Correct |
40 ms |
337492 KB |
Output is correct |
48 |
Correct |
41 ms |
336724 KB |
Output is correct |
49 |
Correct |
41 ms |
336724 KB |
Output is correct |
50 |
Correct |
39 ms |
337644 KB |
Output is correct |
51 |
Correct |
41 ms |
338772 KB |
Output is correct |
52 |
Correct |
41 ms |
337696 KB |
Output is correct |
53 |
Correct |
19 ms |
197468 KB |
Output is correct |
54 |
Correct |
25 ms |
263004 KB |
Output is correct |
55 |
Correct |
31 ms |
309584 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
37 ms |
236372 KB |
Output is correct |
59 |
Correct |
42 ms |
277632 KB |
Output is correct |
60 |
Correct |
41 ms |
273484 KB |
Output is correct |
61 |
Correct |
38 ms |
255064 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
44 ms |
334972 KB |
Output is correct |
64 |
Correct |
48 ms |
327764 KB |
Output is correct |
65 |
Correct |
43 ms |
304460 KB |
Output is correct |
66 |
Correct |
40 ms |
261200 KB |
Output is correct |
67 |
Correct |
35 ms |
215900 KB |
Output is correct |
68 |
Correct |
41 ms |
261432 KB |
Output is correct |
69 |
Correct |
40 ms |
261212 KB |
Output is correct |
70 |
Correct |
40 ms |
259156 KB |
Output is correct |
71 |
Correct |
43 ms |
289872 KB |
Output is correct |
72 |
Correct |
0 ms |
344 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |