#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;
int cnt[N][3];
vector <int> 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].push_back(i);
}
else if(s[i] == 'G') {
g++;
pos[1].push_back(i);
}
else if(s[i] == 'Y') {
b++;
pos[2].push_back(i);
}
cnt[i][0] = r;
cnt[i][1] = g;
cnt[i][2] = b;
}
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,cnt[pos[0][i-1]][1]-j) + max(0,cnt[pos[0][i-1]][2]-k);
if(j) dp[i][j][k][1] = min(dp[i][j-1][k][0],dp[i][j-1][k][2]) + max(0,cnt[pos[1][j-1]][0]-i) + max(0,cnt[pos[1][j-1]][2]-k);
if(k) dp[i][j][k][2] = min(dp[i][j][k-1][0],dp[i][j][k-1][1]) + max(0,cnt[pos[2][k-1]][0]-i) + max(0,cnt[pos[2][k-1]][1]-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 |
1 ms |
2396 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 |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12832 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10716 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 |
8640 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 |
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 |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12832 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10716 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 |
8640 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 |
37388 KB |
Output is correct |
20 |
Correct |
5 ms |
49752 KB |
Output is correct |
21 |
Correct |
4 ms |
39496 KB |
Output is correct |
22 |
Correct |
5 ms |
47448 KB |
Output is correct |
23 |
Correct |
4 ms |
33368 KB |
Output is correct |
24 |
Correct |
2 ms |
21084 KB |
Output is correct |
25 |
Correct |
8 ms |
72252 KB |
Output is correct |
26 |
Correct |
6 ms |
59996 KB |
Output is correct |
27 |
Correct |
6 ms |
54104 KB |
Output is correct |
28 |
Correct |
5 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43460 KB |
Output is correct |
30 |
Correct |
4 ms |
33368 KB |
Output is correct |
31 |
Correct |
3 ms |
29276 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
5 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43608 KB |
Output is correct |
35 |
Correct |
5 ms |
45508 KB |
Output is correct |
36 |
Correct |
4 ms |
39440 KB |
Output is correct |
37 |
Correct |
3 ms |
27228 KB |
Output is correct |
38 |
Correct |
3 ms |
27228 KB |
Output is correct |
39 |
Correct |
4 ms |
37468 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
3 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
73 ms |
321044 KB |
Output is correct |
3 |
Correct |
42 ms |
318548 KB |
Output is correct |
4 |
Correct |
43 ms |
320084 KB |
Output is correct |
5 |
Correct |
42 ms |
320084 KB |
Output is correct |
6 |
Correct |
42 ms |
320080 KB |
Output is correct |
7 |
Correct |
42 ms |
318040 KB |
Output is correct |
8 |
Correct |
46 ms |
320340 KB |
Output is correct |
9 |
Correct |
44 ms |
323412 KB |
Output is correct |
10 |
Correct |
44 ms |
323412 KB |
Output is correct |
11 |
Correct |
42 ms |
324436 KB |
Output is correct |
12 |
Correct |
21 ms |
197460 KB |
Output is correct |
13 |
Correct |
26 ms |
263004 KB |
Output is correct |
14 |
Correct |
32 ms |
292188 KB |
Output is correct |
15 |
Correct |
41 ms |
323932 KB |
Output is correct |
16 |
Correct |
41 ms |
322644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 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 |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12832 KB |
Output is correct |
10 |
Correct |
2 ms |
20828 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10716 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 |
8640 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 |
37388 KB |
Output is correct |
20 |
Correct |
5 ms |
49752 KB |
Output is correct |
21 |
Correct |
4 ms |
39496 KB |
Output is correct |
22 |
Correct |
5 ms |
47448 KB |
Output is correct |
23 |
Correct |
4 ms |
33368 KB |
Output is correct |
24 |
Correct |
2 ms |
21084 KB |
Output is correct |
25 |
Correct |
8 ms |
72252 KB |
Output is correct |
26 |
Correct |
6 ms |
59996 KB |
Output is correct |
27 |
Correct |
6 ms |
54104 KB |
Output is correct |
28 |
Correct |
5 ms |
41564 KB |
Output is correct |
29 |
Correct |
5 ms |
43460 KB |
Output is correct |
30 |
Correct |
4 ms |
33368 KB |
Output is correct |
31 |
Correct |
3 ms |
29276 KB |
Output is correct |
32 |
Correct |
4 ms |
35420 KB |
Output is correct |
33 |
Correct |
5 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
43608 KB |
Output is correct |
35 |
Correct |
5 ms |
45508 KB |
Output is correct |
36 |
Correct |
4 ms |
39440 KB |
Output is correct |
37 |
Correct |
3 ms |
27228 KB |
Output is correct |
38 |
Correct |
3 ms |
27228 KB |
Output is correct |
39 |
Correct |
4 ms |
37468 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
3 ms |
19036 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
73 ms |
321044 KB |
Output is correct |
44 |
Correct |
42 ms |
318548 KB |
Output is correct |
45 |
Correct |
43 ms |
320084 KB |
Output is correct |
46 |
Correct |
42 ms |
320084 KB |
Output is correct |
47 |
Correct |
42 ms |
320080 KB |
Output is correct |
48 |
Correct |
42 ms |
318040 KB |
Output is correct |
49 |
Correct |
46 ms |
320340 KB |
Output is correct |
50 |
Correct |
44 ms |
323412 KB |
Output is correct |
51 |
Correct |
44 ms |
323412 KB |
Output is correct |
52 |
Correct |
42 ms |
324436 KB |
Output is correct |
53 |
Correct |
21 ms |
197460 KB |
Output is correct |
54 |
Correct |
26 ms |
263004 KB |
Output is correct |
55 |
Correct |
32 ms |
292188 KB |
Output is correct |
56 |
Correct |
41 ms |
323932 KB |
Output is correct |
57 |
Correct |
41 ms |
322644 KB |
Output is correct |
58 |
Correct |
47 ms |
236372 KB |
Output is correct |
59 |
Correct |
49 ms |
277628 KB |
Output is correct |
60 |
Correct |
46 ms |
273492 KB |
Output is correct |
61 |
Correct |
42 ms |
255056 KB |
Output is correct |
62 |
Correct |
48 ms |
330392 KB |
Output is correct |
63 |
Correct |
46 ms |
319568 KB |
Output is correct |
64 |
Correct |
51 ms |
313428 KB |
Output is correct |
65 |
Correct |
46 ms |
290896 KB |
Output is correct |
66 |
Correct |
43 ms |
261200 KB |
Output is correct |
67 |
Correct |
45 ms |
215904 KB |
Output is correct |
68 |
Correct |
44 ms |
261056 KB |
Output is correct |
69 |
Correct |
43 ms |
261204 KB |
Output is correct |
70 |
Correct |
46 ms |
259160 KB |
Output is correct |
71 |
Correct |
46 ms |
282416 KB |
Output is correct |
72 |
Correct |
39 ms |
302616 KB |
Output is correct |
73 |
Correct |
2 ms |
8796 KB |
Output is correct |