#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define pb push_back
#define MAXN 410
#define MAXX 1000000010LL
using namespace std;
int n;
ll cnt1, cnt2, cnt3, cnt[5][MAXN][5];
string s;
ll koliko(int a, int i, int j, int k)
{
if (a==1)
return max(0LL, cnt[1][i][2]-j)+max(0LL, cnt[1][i][3]-k);
else if (a==2)
return max(0LL, cnt[2][j][1]-i)+max(0LL, cnt[2][j][3]-k);
else
return max(0LL, cnt[3][k][1]-i)+max(0LL, cnt[3][k][2]-j);
}
int main(){
///R=1; G=2; Y=3;
cin>>n>>s;
for (int i=0; i<n; i++)
{
if (s[i]=='R') { cnt1++; }
else if (s[i]=='G') { cnt2++; }
else {cnt3++; }
if (s[i]=='R') { cnt[1][cnt1][2]=cnt2; cnt[1][cnt1][3]=cnt3; }
else if (s[i]=='G') { cnt[2][cnt2][1]=cnt1; cnt[2][cnt2][3]=cnt3; }
else { cnt[3][cnt3][1]=cnt1; cnt[3][cnt3][2]=cnt2; }
}
/*for (int i=0; i<=cnt1; i++) cout<<cnt[1][i][2]<<" "<<cnt[1][i][3]<<endl;
cout<<endl;
for (int i=0; i<=cnt2; i++) cout<<cnt[2][i][1]<<" "<<cnt[2][i][3]<<endl;
cout<<endl;
for (int i=0; i<=cnt3; i++) cout<<cnt[3][i][1]<<" "<<cnt[3][i][2]<<endl;
cout<<endl;*/
ll dp[3+2][cnt1+2][cnt2+2][cnt3+2];
for (int i=0; i<=cnt1; i++) for (int j=0; j<=cnt2; j++) for (int k=0; k<=cnt3; k++) dp[1][i][j][k]=dp[2][i][j][k]=dp[3][i][j][k]=MAXX;
dp[1][1][0][0]=cnt[1][1][2]+cnt[1][1][3]; //cout<<dp[1][1][0][0]<<endl;
dp[2][0][1][0]=cnt[2][1][1]+cnt[2][1][3]; //cout<<dp[2][0][1][0]<<endl;
dp[3][0][0][1]=cnt[3][1][1]+cnt[3][1][2]; //cout<<dp[3][0][0][1]<<endl;
//cout<<endl;
for (int sum=2; sum<=n; sum++)
{
for (int i=0; i<=min((ll)sum, cnt1); i++)
{
for (int j=0; j<=min(cnt2, (ll)(sum-i)); j++)
{
int k=sum-i-j;
if (k>cnt3) continue;
if (i==0) dp[1][i][j][k]=MAXX;
else dp[1][i][j][k]=min(dp[2][i-1][j][k]+koliko(1, i, j, k), dp[3][i-1][j][k]+koliko(1, i, j, k));
if (j==0) dp[2][i][j][k]=MAXX;
else dp[2][i][j][k]=min(dp[1][i][j-1][k]+koliko(2, i, j, k), dp[3][i][j-1][k]+koliko(2, i, j, k));
if (k==0) dp[3][i][j][k]=MAXX;
else dp[3][i][j][k]=min(dp[1][i][j][k-1]+koliko(3, i, j, k), dp[2][i][j][k-1]+koliko(3, i, j, k));
//cout<<i<<" "<<j<<" "<<k<<":"<<endl;
//cout<<dp[1][i][j][k]<<" "<<dp[2][i][j][k]<<" "<<dp[3][i][j][k]<<endl;
}
}
}
ll ress=min(dp[1][cnt1][cnt2][cnt3], min(dp[2][cnt1][cnt2][cnt3], dp[3][cnt1][cnt2][cnt3]));
if (ress>=MAXX) cout<<-1;
else cout<<ress;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
692 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
564 KB |
Output is correct |
23 |
Correct |
1 ms |
688 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
556 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
628 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
692 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
10 ms |
3412 KB |
Output is correct |
3 |
Correct |
10 ms |
3412 KB |
Output is correct |
4 |
Correct |
10 ms |
3412 KB |
Output is correct |
5 |
Correct |
10 ms |
3412 KB |
Output is correct |
6 |
Correct |
10 ms |
3512 KB |
Output is correct |
7 |
Correct |
10 ms |
3412 KB |
Output is correct |
8 |
Correct |
10 ms |
3496 KB |
Output is correct |
9 |
Correct |
9 ms |
3484 KB |
Output is correct |
10 |
Correct |
10 ms |
3412 KB |
Output is correct |
11 |
Correct |
10 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
4 ms |
1748 KB |
Output is correct |
14 |
Correct |
6 ms |
2480 KB |
Output is correct |
15 |
Correct |
10 ms |
3504 KB |
Output is correct |
16 |
Correct |
10 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
692 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
564 KB |
Output is correct |
23 |
Correct |
1 ms |
688 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
556 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
628 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
692 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
300 KB |
Output is correct |
43 |
Correct |
10 ms |
3412 KB |
Output is correct |
44 |
Correct |
10 ms |
3412 KB |
Output is correct |
45 |
Correct |
10 ms |
3412 KB |
Output is correct |
46 |
Correct |
10 ms |
3412 KB |
Output is correct |
47 |
Correct |
10 ms |
3512 KB |
Output is correct |
48 |
Correct |
10 ms |
3412 KB |
Output is correct |
49 |
Correct |
10 ms |
3496 KB |
Output is correct |
50 |
Correct |
9 ms |
3484 KB |
Output is correct |
51 |
Correct |
10 ms |
3412 KB |
Output is correct |
52 |
Correct |
10 ms |
3412 KB |
Output is correct |
53 |
Correct |
2 ms |
1108 KB |
Output is correct |
54 |
Correct |
4 ms |
1748 KB |
Output is correct |
55 |
Correct |
6 ms |
2480 KB |
Output is correct |
56 |
Correct |
10 ms |
3504 KB |
Output is correct |
57 |
Correct |
10 ms |
3412 KB |
Output is correct |
58 |
Correct |
75 ms |
96164 KB |
Output is correct |
59 |
Correct |
80 ms |
96308 KB |
Output is correct |
60 |
Correct |
70 ms |
95548 KB |
Output is correct |
61 |
Correct |
72 ms |
96572 KB |
Output is correct |
62 |
Correct |
18 ms |
9652 KB |
Output is correct |
63 |
Correct |
18 ms |
15664 KB |
Output is correct |
64 |
Correct |
39 ms |
48512 KB |
Output is correct |
65 |
Correct |
58 ms |
69080 KB |
Output is correct |
66 |
Correct |
73 ms |
95124 KB |
Output is correct |
67 |
Correct |
70 ms |
95052 KB |
Output is correct |
68 |
Correct |
81 ms |
96528 KB |
Output is correct |
69 |
Correct |
80 ms |
97244 KB |
Output is correct |
70 |
Correct |
74 ms |
96844 KB |
Output is correct |
71 |
Correct |
77 ms |
94776 KB |
Output is correct |
72 |
Correct |
18 ms |
25348 KB |
Output is correct |
73 |
Correct |
5 ms |
7636 KB |
Output is correct |