#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN=405;
const int INF=1e9+5;
int n;
string s;
int a[MAXN];
vector<int> pos[3];
int prevs[MAXN][3];
int dp[MAXN][MAXN][MAXN][3];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
cin>>s;
for(int i=0;i<n;i++)
{
a[i]=(s[i]=='R' ? 0 : (s[i]=='G' ? 1 : 2));
}
for(int i=0;i<n;i++)
pos[a[i]].push_back(i);
for(int i=0;i<=n;i++)
{
for(int j=0;j<3;j++)
if(i>0) prevs[i][j]+=prevs[i-1][j];
prevs[i+1][a[i]]++;
}
for(int i=0;i<=prevs[n][0];i++)
for(int j=0;j<=prevs[n][1];j++)
for(int k=0;k<=prevs[n][2];k++)
for(int t=0;t<3;t++) dp[i][j][k][t]=INF;
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for(int i=0;i<=prevs[n][0];i++)
{
for(int j=0;j<=prevs[n][1];j++)
{
for(int k=0;k<=prevs[n][2];k++)
{
if(i>0) dp[i][j][k][0]=min(dp[i][j][k][0], dp[i-1][j][k][1] + max(0, prevs[pos[0][i-1]][1]-j) + max(0, prevs[pos[0][i-1]][2]-k));
if(i>0) dp[i][j][k][0]=min(dp[i][j][k][0], dp[i-1][j][k][2] + max(0, prevs[pos[0][i-1]][1]-j) + max(0, prevs[pos[0][i-1]][2]-k));
if(j>0) dp[i][j][k][1]=min(dp[i][j][k][1], dp[i][j-1][k][0] + max(0, prevs[pos[1][j-1]][0]-i) + max(0, prevs[pos[1][j-1]][2]-k));
if(j>0) dp[i][j][k][1]=min(dp[i][j][k][1], dp[i][j-1][k][2] + max(0, prevs[pos[1][j-1]][0]-i) + max(0, prevs[pos[1][j-1]][2]-k));
if(k>0) dp[i][j][k][2]=min(dp[i][j][k][2], dp[i][j][k-1][0] + max(0, prevs[pos[2][k-1]][0]-i) + max(0, prevs[pos[2][k-1]][1]-j));
if(k>0) dp[i][j][k][2]=min(dp[i][j][k][2], dp[i][j][k-1][1] + max(0, prevs[pos[2][k-1]][0]-i) + max(0, prevs[pos[2][k-1]][1]-j));
}
}
}
int ans=min(dp[prevs[n][0]][prevs[n][1]][prevs[n][2]][0],
min(dp[prevs[n][0]][prevs[n][1]][prevs[n][2]][1],
dp[prevs[n][0]][prevs[n][1]][prevs[n][2]][2]));
if(ans==INF) cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1888 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
1604 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2248 KB |
Output is correct |
30 |
Correct |
1 ms |
2248 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3784 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
75 ms |
162940 KB |
Output is correct |
3 |
Correct |
56 ms |
162164 KB |
Output is correct |
4 |
Correct |
56 ms |
162928 KB |
Output is correct |
5 |
Correct |
55 ms |
162924 KB |
Output is correct |
6 |
Correct |
58 ms |
162912 KB |
Output is correct |
7 |
Correct |
55 ms |
162168 KB |
Output is correct |
8 |
Correct |
54 ms |
162156 KB |
Output is correct |
9 |
Correct |
58 ms |
161320 KB |
Output is correct |
10 |
Correct |
59 ms |
162924 KB |
Output is correct |
11 |
Correct |
56 ms |
162892 KB |
Output is correct |
12 |
Correct |
18 ms |
43988 KB |
Output is correct |
13 |
Correct |
34 ms |
77100 KB |
Output is correct |
14 |
Correct |
40 ms |
111300 KB |
Output is correct |
15 |
Correct |
56 ms |
162868 KB |
Output is correct |
16 |
Correct |
56 ms |
162980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
1 ms |
1888 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
1604 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2248 KB |
Output is correct |
30 |
Correct |
1 ms |
2248 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3784 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
75 ms |
162940 KB |
Output is correct |
44 |
Correct |
56 ms |
162164 KB |
Output is correct |
45 |
Correct |
56 ms |
162928 KB |
Output is correct |
46 |
Correct |
55 ms |
162924 KB |
Output is correct |
47 |
Correct |
58 ms |
162912 KB |
Output is correct |
48 |
Correct |
55 ms |
162168 KB |
Output is correct |
49 |
Correct |
54 ms |
162156 KB |
Output is correct |
50 |
Correct |
58 ms |
161320 KB |
Output is correct |
51 |
Correct |
59 ms |
162924 KB |
Output is correct |
52 |
Correct |
56 ms |
162892 KB |
Output is correct |
53 |
Correct |
18 ms |
43988 KB |
Output is correct |
54 |
Correct |
34 ms |
77100 KB |
Output is correct |
55 |
Correct |
40 ms |
111300 KB |
Output is correct |
56 |
Correct |
56 ms |
162868 KB |
Output is correct |
57 |
Correct |
56 ms |
162980 KB |
Output is correct |
58 |
Correct |
57 ms |
76240 KB |
Output is correct |
59 |
Correct |
65 ms |
96608 KB |
Output is correct |
60 |
Correct |
60 ms |
87732 KB |
Output is correct |
61 |
Correct |
70 ms |
84808 KB |
Output is correct |
62 |
Correct |
59 ms |
160628 KB |
Output is correct |
63 |
Correct |
62 ms |
160176 KB |
Output is correct |
64 |
Correct |
69 ms |
150116 KB |
Output is correct |
65 |
Correct |
85 ms |
138480 KB |
Output is correct |
66 |
Correct |
61 ms |
86012 KB |
Output is correct |
67 |
Correct |
61 ms |
82840 KB |
Output is correct |
68 |
Correct |
65 ms |
88712 KB |
Output is correct |
69 |
Correct |
61 ms |
85464 KB |
Output is correct |
70 |
Correct |
71 ms |
92544 KB |
Output is correct |
71 |
Correct |
60 ms |
83920 KB |
Output is correct |
72 |
Correct |
26 ms |
41576 KB |
Output is correct |
73 |
Correct |
3 ms |
2900 KB |
Output is correct |