//don't copy pls)
/*TAAK ZDES NADO RECURSIU PISAT*/
//I'm not in the danger i am the DANGER
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define sigma signed
using namespace std;
using namespace __gnu_pbds;
const int N = 400 + 1;
int mod = 1e9 + 7;
const int INF = 1e18;
int n,p[3][N];
vector <int> g[3];
int cnt1 , cnt2 , cnt3;
string s,t;
vector <string> q;
int dp[N][N][3], dp1[N][N][3];
void Gold(){
cin >> n >> s;
s = '+' + s;
t.pb('R');
t.pb('Y');
t.pb('G');
for(int i = 1 ; i <= n ; i++){
if(s[i] == 'R'){
cnt1++;
}
if(s[i] == 'Y'){
cnt2++;
}
if(s[i] == 'G'){
cnt3++;
}
for(int j = 0 ; j < 3 ; j++){
p[j][i] = p[j][i - 1] + (s[i] == t[j]);
}
}
for(int i = 1 ; i <= n ; i++){
for(int j = 0 ; j <= cnt1 ; j++){
for(int k = 0 ; k <= cnt2 ; k++){
for(int y = 0 ; y < 3 ; y++){
dp[j][k][y] = INF;
dp1[j][k][y] = INF;
}
}
}
}
for(int y = 0 ; y < 3 ; y++){
dp1[0][0][y] = 0;
}
for(int i = 1 ; i <= n ; i++){
for(int j = 0 ; j < 3 ; j++){
if(s[i] == t[j]){
g[j].pb(i);
}
}
}
// cout << "ok";
for(int i = 1 ; i <= n ; i++){
for(int a = 0 ; a <= cnt1 ; a++){
for(int b = 0 ; b <=cnt2 ; b++){
if(a+b>i) continue;
int c = i - (a + b);
if(c > cnt3) continue;
if(a){
int pos = g[0][a - 1];
int cnt = max(0ll,(b - p[1][pos])) + max((c - p[2][pos]) , 0ll);
dp[a][b][0] = min(dp1[a - 1][b][1] , dp1[a - 1][b][2]) + max(0ll , (pos + cnt) - i);
}
if(b){
int pos = g[1][b - 1];
int cnt = max(a - p[0][pos] , 0ll) + max(c - p[2][pos] , 0ll);
dp[a][b][1] = min(dp1[a][b - 1][0] , dp1[a][b - 1][2]) + max(0ll , (pos + cnt) - i);
}
if(c){
int pos = g[2][c - 1];
int cnt = max(0ll , a - p[0][pos]) + max(0ll , b - p[1][pos]);
dp[a][b][2] = min(dp1[a][b][0] , dp1[a][b][1]) + max(0ll , (pos + cnt) - i);
}
// cout << i << ' ' << a << ' ' << b << ' ' << c << '\n';
}
}
for(int a = 0 ; a <= cnt1 ; a++){
for(int b = 0 ; b <=cnt2 ; b++){
if(a+b>i) continue;
int c = i - (a + b);
if(c > cnt3) continue;
for( int y = 0; y <= 2; y ++ ) dp1[a][b][y] = dp[a][b][y];
}
}
}
int ans = INF;
for(int i = 0 ; i < 3 ; i++){
ans = min(ans , dp[cnt1][cnt2][i]);
}
if(ans == INF){
cout << "-1\n";
return;
}
cout << ans;
}
sigma main(){
//freopen("txt.in","r",stdin);
//freopen("txt.out","w",stdout);
// ios_base::sync_with_stdio(0);
// cin.tie(0);
int TT = 1;
// cin >> TT;
for(int i = 1 ; i <= TT ; i++){
//cout << "Case " << i << ": ";
Gold();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
448 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
448 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
680 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
448 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
576 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
608 KB |
Output is correct |
34 |
Correct |
0 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
600 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
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 |
2140 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
2 ms |
1968 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
2 ms |
1884 KB |
Output is correct |
8 |
Correct |
2 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
3 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
448 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
680 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
448 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
576 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
608 KB |
Output is correct |
34 |
Correct |
0 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
600 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
2140 KB |
Output is correct |
44 |
Correct |
1 ms |
1884 KB |
Output is correct |
45 |
Correct |
2 ms |
1968 KB |
Output is correct |
46 |
Correct |
1 ms |
1880 KB |
Output is correct |
47 |
Correct |
1 ms |
1884 KB |
Output is correct |
48 |
Correct |
2 ms |
1884 KB |
Output is correct |
49 |
Correct |
2 ms |
1884 KB |
Output is correct |
50 |
Correct |
1 ms |
1880 KB |
Output is correct |
51 |
Correct |
3 ms |
1884 KB |
Output is correct |
52 |
Correct |
2 ms |
1884 KB |
Output is correct |
53 |
Correct |
1 ms |
1116 KB |
Output is correct |
54 |
Correct |
1 ms |
1372 KB |
Output is correct |
55 |
Correct |
1 ms |
1628 KB |
Output is correct |
56 |
Correct |
2 ms |
1884 KB |
Output is correct |
57 |
Correct |
1 ms |
1884 KB |
Output is correct |
58 |
Correct |
50 ms |
2140 KB |
Output is correct |
59 |
Correct |
38 ms |
2396 KB |
Output is correct |
60 |
Correct |
41 ms |
2476 KB |
Output is correct |
61 |
Correct |
39 ms |
2392 KB |
Output is correct |
62 |
Correct |
3 ms |
2140 KB |
Output is correct |
63 |
Correct |
5 ms |
1884 KB |
Output is correct |
64 |
Correct |
17 ms |
2280 KB |
Output is correct |
65 |
Correct |
24 ms |
2140 KB |
Output is correct |
66 |
Correct |
47 ms |
2392 KB |
Output is correct |
67 |
Correct |
41 ms |
1884 KB |
Output is correct |
68 |
Correct |
39 ms |
2140 KB |
Output is correct |
69 |
Correct |
47 ms |
2428 KB |
Output is correct |
70 |
Correct |
38 ms |
2140 KB |
Output is correct |
71 |
Correct |
54 ms |
2640 KB |
Output is correct |
72 |
Correct |
48 ms |
3672 KB |
Output is correct |
73 |
Correct |
3 ms |
352 KB |
Output is correct |