#include <bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl '\n'
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
template <class T> istream &operator>>(istream& in, vector<T> &v) {for (auto& i : v) in >> i; return in;}
#ifdef LOCAL
#include "../../../lib/debug.h"
#else
#define dbg(...)
#endif
const int N=405,oo=1e18;
int trans[3][N][3];
int cnt[3];
map<char,int>m;
void ckmin(int&a,int b) {a=min(a,b);}
signed main() {
ios::sync_with_stdio(0);cin.tie(0);m['R']=0;m['G']=1;m['Y']=2;
int n;cin>>n;string s;cin>>s;
FOR(i,0,n)cnt[m[s[i]]]++;
vi y={0,0,0};
FOR(i,0,n){
y[m[s[i]]]++;
FOR(j,0,3)trans[m[s[i]]][y[m[s[i]]]][j]=y[j];
}
int dp[cnt[0]+1][cnt[1]+1][cnt[2]+1][3];FOR(i,0,cnt[0]+1)FOR(j,0,cnt[1]+1)FOR(k,0,cnt[2]+1)FOR(l,0,3)dp[i][j][k][l]=oo;FOR(i,0,3)dp[0][0][0][i]=1;
FOR(i,0,cnt[0]+1)FOR(j,0,cnt[1]+1)FOR(k,0,cnt[2]+1)FOR(last,0,3)FOR(next,0,3)if(last!=next){
vi x={i,j,k};if(x[next]==cnt[next])continue;
int nxt=dp[i][j][k][last];
x[next]++;
FOR(p,0,3)nxt+=max(0ll,trans[next][x[next]][p]-x[p]);
ckmin(dp[x[0]][x[1]][x[2]][next],nxt);
}
int ans=min({dp[cnt[0]][cnt[1]][cnt[2]][0], dp[cnt[0]][cnt[1]][cnt[2]][1], dp[cnt[0]][cnt[1]][cnt[2]][2]});
if(ans==oo)cout<<-1<<endl;
else cout<<ans-1<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
344 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 |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
344 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 |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
464 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
632 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
644 KB |
Output is correct |
40 |
Correct |
0 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
1368 KB |
Output is correct |
3 |
Correct |
6 ms |
1368 KB |
Output is correct |
4 |
Correct |
6 ms |
1372 KB |
Output is correct |
5 |
Correct |
6 ms |
1372 KB |
Output is correct |
6 |
Correct |
6 ms |
1372 KB |
Output is correct |
7 |
Correct |
6 ms |
1372 KB |
Output is correct |
8 |
Correct |
6 ms |
1412 KB |
Output is correct |
9 |
Correct |
6 ms |
1368 KB |
Output is correct |
10 |
Correct |
6 ms |
1372 KB |
Output is correct |
11 |
Correct |
6 ms |
1372 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
6 ms |
1372 KB |
Output is correct |
16 |
Correct |
5 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
344 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 |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
464 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
632 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
644 KB |
Output is correct |
40 |
Correct |
0 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
6 ms |
1368 KB |
Output is correct |
44 |
Correct |
6 ms |
1368 KB |
Output is correct |
45 |
Correct |
6 ms |
1372 KB |
Output is correct |
46 |
Correct |
6 ms |
1372 KB |
Output is correct |
47 |
Correct |
6 ms |
1372 KB |
Output is correct |
48 |
Correct |
6 ms |
1372 KB |
Output is correct |
49 |
Correct |
6 ms |
1412 KB |
Output is correct |
50 |
Correct |
6 ms |
1368 KB |
Output is correct |
51 |
Correct |
6 ms |
1372 KB |
Output is correct |
52 |
Correct |
6 ms |
1372 KB |
Output is correct |
53 |
Correct |
2 ms |
604 KB |
Output is correct |
54 |
Correct |
3 ms |
860 KB |
Output is correct |
55 |
Correct |
4 ms |
860 KB |
Output is correct |
56 |
Correct |
6 ms |
1372 KB |
Output is correct |
57 |
Correct |
5 ms |
1368 KB |
Output is correct |
58 |
Correct |
346 ms |
56736 KB |
Output is correct |
59 |
Correct |
350 ms |
56668 KB |
Output is correct |
60 |
Correct |
343 ms |
56324 KB |
Output is correct |
61 |
Correct |
353 ms |
56936 KB |
Output is correct |
62 |
Correct |
28 ms |
4956 KB |
Output is correct |
63 |
Correct |
50 ms |
8648 KB |
Output is correct |
64 |
Correct |
170 ms |
28252 KB |
Output is correct |
65 |
Correct |
253 ms |
40788 KB |
Output is correct |
66 |
Correct |
348 ms |
55896 KB |
Output is correct |
67 |
Correct |
347 ms |
55892 KB |
Output is correct |
68 |
Correct |
348 ms |
56912 KB |
Output is correct |
69 |
Correct |
355 ms |
57328 KB |
Output is correct |
70 |
Correct |
349 ms |
57132 KB |
Output is correct |
71 |
Correct |
344 ms |
55864 KB |
Output is correct |
72 |
Correct |
87 ms |
14684 KB |
Output is correct |
73 |
Correct |
23 ms |
3932 KB |
Output is correct |