//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][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[i][j][k][y] = INF;
}
}
}
}
for(int y = 0 ; y < 3 ; y++){
dp[0][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[i][a][b][0] = min(dp[i - 1][a - 1][b][1] , dp[i - 1][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[i][a][b][1] = min(dp[i - 1][a][b - 1][0] , dp[i - 1][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[i][a][b][2] = min(dp[i - 1][a][b][0] , dp[i - 1][a][b][1]) + max(0ll , (pos + cnt) - i);
}
// cout << i << ' ' << a << ' ' << b << ' ' << c << '\n';
}
}
}
int ans = INF;
for(int i = 0 ; i < 3 ; i++){
ans = min(ans , dp[n][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 |
1 ms |
448 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 |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
704 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
448 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 |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
704 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
7000 KB |
Output is correct |
19 |
Correct |
2 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
7448 KB |
Output is correct |
21 |
Correct |
3 ms |
6236 KB |
Output is correct |
22 |
Correct |
3 ms |
7516 KB |
Output is correct |
23 |
Correct |
4 ms |
5568 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
8420 KB |
Output is correct |
27 |
Correct |
3 ms |
7772 KB |
Output is correct |
28 |
Correct |
3 ms |
6492 KB |
Output is correct |
29 |
Correct |
3 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
2 ms |
4960 KB |
Output is correct |
32 |
Correct |
2 ms |
5584 KB |
Output is correct |
33 |
Correct |
2 ms |
6808 KB |
Output is correct |
34 |
Correct |
3 ms |
6236 KB |
Output is correct |
35 |
Correct |
3 ms |
6576 KB |
Output is correct |
36 |
Correct |
3 ms |
6236 KB |
Output is correct |
37 |
Correct |
2 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
4700 KB |
Output is correct |
39 |
Correct |
2 ms |
5980 KB |
Output is correct |
40 |
Correct |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
105 ms |
326176 KB |
Output is correct |
3 |
Correct |
114 ms |
325204 KB |
Output is correct |
4 |
Correct |
115 ms |
326224 KB |
Output is correct |
5 |
Correct |
109 ms |
326228 KB |
Output is correct |
6 |
Correct |
112 ms |
326220 KB |
Output is correct |
7 |
Correct |
109 ms |
323584 KB |
Output is correct |
8 |
Correct |
116 ms |
323680 KB |
Output is correct |
9 |
Correct |
108 ms |
322900 KB |
Output is correct |
10 |
Correct |
112 ms |
326224 KB |
Output is correct |
11 |
Correct |
123 ms |
326224 KB |
Output is correct |
12 |
Correct |
34 ms |
87640 KB |
Output is correct |
13 |
Correct |
50 ms |
153808 KB |
Output is correct |
14 |
Correct |
85 ms |
222548 KB |
Output is correct |
15 |
Correct |
111 ms |
324488 KB |
Output is correct |
16 |
Correct |
113 ms |
324436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
448 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 |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
0 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
704 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
7000 KB |
Output is correct |
19 |
Correct |
2 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
7448 KB |
Output is correct |
21 |
Correct |
3 ms |
6236 KB |
Output is correct |
22 |
Correct |
3 ms |
7516 KB |
Output is correct |
23 |
Correct |
4 ms |
5568 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
8420 KB |
Output is correct |
27 |
Correct |
3 ms |
7772 KB |
Output is correct |
28 |
Correct |
3 ms |
6492 KB |
Output is correct |
29 |
Correct |
3 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
2 ms |
4960 KB |
Output is correct |
32 |
Correct |
2 ms |
5584 KB |
Output is correct |
33 |
Correct |
2 ms |
6808 KB |
Output is correct |
34 |
Correct |
3 ms |
6236 KB |
Output is correct |
35 |
Correct |
3 ms |
6576 KB |
Output is correct |
36 |
Correct |
3 ms |
6236 KB |
Output is correct |
37 |
Correct |
2 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
4700 KB |
Output is correct |
39 |
Correct |
2 ms |
5980 KB |
Output is correct |
40 |
Correct |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
3164 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
105 ms |
326176 KB |
Output is correct |
44 |
Correct |
114 ms |
325204 KB |
Output is correct |
45 |
Correct |
115 ms |
326224 KB |
Output is correct |
46 |
Correct |
109 ms |
326228 KB |
Output is correct |
47 |
Correct |
112 ms |
326220 KB |
Output is correct |
48 |
Correct |
109 ms |
323584 KB |
Output is correct |
49 |
Correct |
116 ms |
323680 KB |
Output is correct |
50 |
Correct |
108 ms |
322900 KB |
Output is correct |
51 |
Correct |
112 ms |
326224 KB |
Output is correct |
52 |
Correct |
123 ms |
326224 KB |
Output is correct |
53 |
Correct |
34 ms |
87640 KB |
Output is correct |
54 |
Correct |
50 ms |
153808 KB |
Output is correct |
55 |
Correct |
85 ms |
222548 KB |
Output is correct |
56 |
Correct |
111 ms |
324488 KB |
Output is correct |
57 |
Correct |
113 ms |
324436 KB |
Output is correct |
58 |
Correct |
162 ms |
376436 KB |
Output is correct |
59 |
Correct |
155 ms |
396368 KB |
Output is correct |
60 |
Correct |
158 ms |
403484 KB |
Output is correct |
61 |
Correct |
159 ms |
386900 KB |
Output is correct |
62 |
Correct |
125 ms |
356688 KB |
Output is correct |
63 |
Correct |
116 ms |
332880 KB |
Output is correct |
64 |
Correct |
136 ms |
365116 KB |
Output is correct |
65 |
Correct |
136 ms |
344844 KB |
Output is correct |
66 |
Correct |
160 ms |
388420 KB |
Output is correct |
67 |
Correct |
135 ms |
328272 KB |
Output is correct |
68 |
Correct |
153 ms |
387664 KB |
Output is correct |
69 |
Correct |
159 ms |
396372 KB |
Output is correct |
70 |
Correct |
154 ms |
379476 KB |
Output is correct |
71 |
Correct |
187 ms |
437048 KB |
Output is correct |
72 |
Correct |
244 ms |
687696 KB |
Output is correct |
73 |
Correct |
11 ms |
21340 KB |
Output is correct |