#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, pos[405][3], cnt[405][3];
string str;
cin >> n >> str;
for (int i=0; i<n; i++)
{
for (int j=0; j<3; j++)
cnt[i][j]=i?cnt[i-1][j]:0;
if (str[i]=='R')
pos[++cnt[i][0]][0]=i;
else if (str[i]=='G')
pos[++cnt[i][1]][1]=i;
else
pos[++cnt[i][2]][2]=i;
}
int dp[cnt[n-1][0]+5][cnt[n-1][1]+5][cnt[n-1][2]+5][3], ans=1e9;
for (int i=0; i<=cnt[n-1][0]; i++)
{
for (int j=0; j<=cnt[n-1][1]; j++)
{
for (int k=0; k<=cnt[n-1][2]; k++)
{
if (i==0 && j==0 && k==0)
{
for (int l=0; l<3; l++)
dp[i][j][k][l]=0;
continue;
}
dp[i][j][k][0]=i?min(dp[i-1][j][k][1], dp[i-1][j][k][2])+max(0, cnt[pos[i][0]][0]-i)+max(0, cnt[pos[i][0]][1]-j)+max(0, cnt[pos[i][0]][2]-k):1e9;
dp[i][j][k][1]=j?min(dp[i][j-1][k][0], dp[i][j-1][k][2])+max(0, cnt[pos[j][1]][0]-i)+max(0, cnt[pos[j][1]][1]-j)+max(0, cnt[pos[j][1]][2]-k):1e9;
dp[i][j][k][2]=k?min(dp[i][j][k-1][0], dp[i][j][k-1][1])+max(0, cnt[pos[k][2]][0]-i)+max(0, cnt[pos[k][2]][1]-j)+max(0, cnt[pos[k][2]][2]-k):1e9;
}
}
}
for (int i=0; i<3; i++)
ans=min(ans, dp[cnt[n-1][0]][cnt[n-1][1]][cnt[n-1][2]][i]);
cout << (ans>=1e8?-1:ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
464 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
464 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
0 ms |
604 KB |
Output is correct |
38 |
Correct |
0 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
3160 KB |
Output is correct |
3 |
Correct |
3 ms |
2760 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2768 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2764 KB |
Output is correct |
11 |
Correct |
2 ms |
2904 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
2140 KB |
Output is correct |
15 |
Correct |
2 ms |
2904 KB |
Output is correct |
16 |
Correct |
2 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
464 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
464 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
0 ms |
604 KB |
Output is correct |
38 |
Correct |
0 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
3160 KB |
Output is correct |
44 |
Correct |
3 ms |
2760 KB |
Output is correct |
45 |
Correct |
2 ms |
2908 KB |
Output is correct |
46 |
Correct |
2 ms |
2768 KB |
Output is correct |
47 |
Correct |
2 ms |
2908 KB |
Output is correct |
48 |
Correct |
2 ms |
2908 KB |
Output is correct |
49 |
Correct |
2 ms |
2908 KB |
Output is correct |
50 |
Correct |
2 ms |
2652 KB |
Output is correct |
51 |
Correct |
2 ms |
2764 KB |
Output is correct |
52 |
Correct |
2 ms |
2904 KB |
Output is correct |
53 |
Correct |
1 ms |
1116 KB |
Output is correct |
54 |
Correct |
1 ms |
1628 KB |
Output is correct |
55 |
Correct |
1 ms |
2140 KB |
Output is correct |
56 |
Correct |
2 ms |
2904 KB |
Output is correct |
57 |
Correct |
2 ms |
2904 KB |
Output is correct |
58 |
Correct |
32 ms |
31160 KB |
Output is correct |
59 |
Correct |
32 ms |
31068 KB |
Output is correct |
60 |
Correct |
32 ms |
30812 KB |
Output is correct |
61 |
Correct |
33 ms |
31292 KB |
Output is correct |
62 |
Correct |
4 ms |
4700 KB |
Output is correct |
63 |
Correct |
6 ms |
6492 KB |
Output is correct |
64 |
Correct |
16 ms |
16412 KB |
Output is correct |
65 |
Correct |
23 ms |
22908 KB |
Output is correct |
66 |
Correct |
31 ms |
30812 KB |
Output is correct |
67 |
Correct |
34 ms |
30808 KB |
Output is correct |
68 |
Correct |
31 ms |
31064 KB |
Output is correct |
69 |
Correct |
32 ms |
31324 KB |
Output is correct |
70 |
Correct |
32 ms |
31400 KB |
Output is correct |
71 |
Correct |
34 ms |
30556 KB |
Output is correct |
72 |
Correct |
9 ms |
9052 KB |
Output is correct |
73 |
Correct |
3 ms |
3676 KB |
Output is correct |