#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define pdd pair<double ,double>
#define pcc pair<char, char>
#define endl '\n'
#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif
const ll inf = (1ll<<60);
const int iinf=2147483647;
const ll mod = 998244353;
const ll maxn=405;
const double PI=acos(-1);
ll pw(ll x, ll p, ll m=mod){
ll ret=1;
while (p>0){
if (p&1){
ret*=x;
ret%=m;
}
x*=x;
x%=m;
p>>=1;
}
return ret;
}
ll inv(ll a, ll m=mod){
return pw(a,m-2);
}
int dp[maxn][maxn][maxn][3];
int pos[3][maxn];
int cnt[3][maxn];
signed main(){
IOS();
int n; cin>>n;
string str; cin>>str;
str='$'+str;
int tota=0, totb=0, totc=0;
REP1(i,n){
if (str[i]=='R') tota++;
else if (str[i]=='G') totb++;
else totc++;
}
REP(i,tota+1){
REP(j,totb+1){
REP(k,totc+1){
REP(l, 3){
dp[i][j][k][l]=inf;
}
}
}
}
REP1(i,n){
REP(l, 3) cnt[l][i]=cnt[l][i-1];
if (str[i]=='R') {
cnt[0][i]++;
pos[0][cnt[0][i]]=i;
}
else if (str[i]=='G') {
cnt[1][i]++;
pos[1][cnt[1][i]]=i;
}
else {
cnt[2][i]++;
pos[2][cnt[2][i]]=i;
}
}
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
// 0 is R, 1 is G, 2 is Y
REP(i, tota+1){
REP(j, totb+1){
REP(k, totc+1){
if (i+j+k==0) continue;
if (i>0){
int p=pos[0][i]-1;
dp[i][j][k][0]=min(dp[i-1][j][k][1], dp[i-1][j][k][2]);
dp[i][j][k][0]+=p-min(i-1, cnt[0][p])-min(j, cnt[1][p])-min(k, cnt[2][p]);
dp[i][j][k][0]=min(dp[i][j][k][0], inf);
}
if (j>0){
int p=pos[1][j]-1;
dp[i][j][k][1]=min(dp[i][j-1][k][0], dp[i][j-1][k][2]);
dp[i][j][k][1]+=p-min(i, cnt[0][p])-min(j-1, cnt[1][p])-min(k, cnt[2][p]);
dp[i][j][k][1]=min(dp[i][j][k][1], inf);
}
if (k>0){
int p=pos[2][k]-1;
dp[i][j][k][2]=min(dp[i][j][k-1][0], dp[i][j][k-1][1]);
dp[i][j][k][2]+=p-min(i, cnt[0][p])-min(j, cnt[1][p])-min(k-1, cnt[2][p]);
dp[i][j][k][2]=min(dp[i][j][k][2], inf);
}
}
}
}
int k=min({dp[tota][totb][totc][0], dp[tota][totb][totc][1], dp[tota][totb][totc][2]});
if (k==inf) k=-1;
cout<<k<<endl;
}
/*
3
YYG
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
444 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
444 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
2 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2388 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
1748 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 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 |
3156 KB |
Output is correct |
28 |
Correct |
1 ms |
2496 KB |
Output is correct |
29 |
Correct |
1 ms |
2388 KB |
Output is correct |
30 |
Correct |
1 ms |
2388 KB |
Output is correct |
31 |
Correct |
1 ms |
2132 KB |
Output is correct |
32 |
Correct |
1 ms |
2488 KB |
Output is correct |
33 |
Correct |
2 ms |
3924 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2772 KB |
Output is correct |
36 |
Correct |
1 ms |
2004 KB |
Output is correct |
37 |
Correct |
2 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2260 KB |
Output is correct |
39 |
Correct |
2 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
67 ms |
163940 KB |
Output is correct |
3 |
Correct |
68 ms |
163196 KB |
Output is correct |
4 |
Correct |
60 ms |
163948 KB |
Output is correct |
5 |
Correct |
58 ms |
164032 KB |
Output is correct |
6 |
Correct |
62 ms |
164032 KB |
Output is correct |
7 |
Correct |
62 ms |
163180 KB |
Output is correct |
8 |
Correct |
62 ms |
163212 KB |
Output is correct |
9 |
Correct |
61 ms |
162380 KB |
Output is correct |
10 |
Correct |
61 ms |
163980 KB |
Output is correct |
11 |
Correct |
66 ms |
163940 KB |
Output is correct |
12 |
Correct |
21 ms |
44240 KB |
Output is correct |
13 |
Correct |
31 ms |
77612 KB |
Output is correct |
14 |
Correct |
45 ms |
112044 KB |
Output is correct |
15 |
Correct |
62 ms |
163916 KB |
Output is correct |
16 |
Correct |
63 ms |
163912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
444 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
2 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2388 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
1748 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 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 |
3156 KB |
Output is correct |
28 |
Correct |
1 ms |
2496 KB |
Output is correct |
29 |
Correct |
1 ms |
2388 KB |
Output is correct |
30 |
Correct |
1 ms |
2388 KB |
Output is correct |
31 |
Correct |
1 ms |
2132 KB |
Output is correct |
32 |
Correct |
1 ms |
2488 KB |
Output is correct |
33 |
Correct |
2 ms |
3924 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2772 KB |
Output is correct |
36 |
Correct |
1 ms |
2004 KB |
Output is correct |
37 |
Correct |
2 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2260 KB |
Output is correct |
39 |
Correct |
2 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2260 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
67 ms |
163940 KB |
Output is correct |
44 |
Correct |
68 ms |
163196 KB |
Output is correct |
45 |
Correct |
60 ms |
163948 KB |
Output is correct |
46 |
Correct |
58 ms |
164032 KB |
Output is correct |
47 |
Correct |
62 ms |
164032 KB |
Output is correct |
48 |
Correct |
62 ms |
163180 KB |
Output is correct |
49 |
Correct |
62 ms |
163212 KB |
Output is correct |
50 |
Correct |
61 ms |
162380 KB |
Output is correct |
51 |
Correct |
61 ms |
163980 KB |
Output is correct |
52 |
Correct |
66 ms |
163940 KB |
Output is correct |
53 |
Correct |
21 ms |
44240 KB |
Output is correct |
54 |
Correct |
31 ms |
77612 KB |
Output is correct |
55 |
Correct |
45 ms |
112044 KB |
Output is correct |
56 |
Correct |
62 ms |
163916 KB |
Output is correct |
57 |
Correct |
63 ms |
163912 KB |
Output is correct |
58 |
Correct |
71 ms |
120628 KB |
Output is correct |
59 |
Correct |
82 ms |
137860 KB |
Output is correct |
60 |
Correct |
75 ms |
130000 KB |
Output is correct |
61 |
Correct |
76 ms |
128084 KB |
Output is correct |
62 |
Correct |
66 ms |
163592 KB |
Output is correct |
63 |
Correct |
77 ms |
164960 KB |
Output is correct |
64 |
Correct |
79 ms |
164472 KB |
Output is correct |
65 |
Correct |
79 ms |
158924 KB |
Output is correct |
66 |
Correct |
76 ms |
128364 KB |
Output is correct |
67 |
Correct |
74 ms |
125616 KB |
Output is correct |
68 |
Correct |
82 ms |
131276 KB |
Output is correct |
69 |
Correct |
83 ms |
128932 KB |
Output is correct |
70 |
Correct |
78 ms |
134860 KB |
Output is correct |
71 |
Correct |
76 ms |
126332 KB |
Output is correct |
72 |
Correct |
28 ms |
49700 KB |
Output is correct |
73 |
Correct |
4 ms |
5332 KB |
Output is correct |