//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define F first
#define S second
#define ull unsigned long long
#define db double
#define ldb long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int N = 401;
using namespace std;
using namespace __gnu_pbds;
ll n, m, dp[N][N][N][3], pr[N][3], a, b, c;
vector <ll> v[3];
string s;
signed main (){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
cin >> s;
s = " " + s;
for (int i = 1; i <= n; i++){
pr[i][0] = pr[i - 1][0];
pr[i][1] = pr[i - 1][1];
pr[i][2] = pr[i - 1][2];
if (s[i] == 'R'){
pr[i][0]++;
v[0].pb(i);
a++;
}
else if (s[i] == 'Y'){
pr[i][1]++;
v[1].pb(i);
b++;
}
else{
pr[i][2]++;
v[2].pb(i);
c++;
}
}
for (int i = 0; i <= n; i++){
for (int r = 0; r <= a; r++){
for (int y = 0; y <= b; y++){
dp[i][r][y][0] = 1e10;
dp[i][r][y][1] = 1e10;
dp[i][r][y][2] = 1e10;
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int i = 1; i <= n; i++){
for (int r = 0; r <= a; r++){
for (int y = 0; y <= b; y++){
ll g = i - r - y;
if (r + y > i){
break;
}
if(g>c)continue;
if (r > 0){
ll pos = v[0][r - 1];
ll cnt = max(0ll,y - pr[pos][1]) + max(0ll,g - pr[pos][2]);
dp[i][r][y][0] = min (dp[i - 1][r - 1][y][1], dp[i - 1][r - 1][y][2]) + max (0ll, pos + cnt - i);
}
if (y > 0){
ll pos = v[1][y - 1];
ll cnt = max(0ll,r - pr[pos][0]) + max(0ll,g - pr[pos][2]);
dp[i][r][y][1] = min (dp[i - 1][r][y - 1][0], dp[i - 1][r][y - 1][2]) + max (0ll, pos + cnt - i);
}
if (g > 0){
ll pos = v[2][g - 1];
ll cnt = max(0ll,y - pr[pos][1]) + max(0ll,r - pr[pos][0]);
dp[i][r][y][2] = min (dp[i - 1][r][y][1], dp[i - 1][r][y][0]) + max (0ll, pos + cnt - i);
}
}
}
}
ll ali = min ({dp[n][a][b][0], dp[n][a][b][1], dp[n][a][b][2]});
cout << ( ali >= 1e10 ? -1 : ali );
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
912 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
0 ms |
724 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 |
860 KB |
Output is correct |
16 |
Correct |
0 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 |
348 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 |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
912 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
0 ms |
724 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 |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
1116 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
7260 KB |
Output is correct |
19 |
Correct |
2 ms |
6236 KB |
Output is correct |
20 |
Correct |
3 ms |
7580 KB |
Output is correct |
21 |
Correct |
3 ms |
6212 KB |
Output is correct |
22 |
Correct |
3 ms |
7516 KB |
Output is correct |
23 |
Correct |
2 ms |
5468 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
4 ms |
10076 KB |
Output is correct |
26 |
Correct |
4 ms |
8536 KB |
Output is correct |
27 |
Correct |
3 ms |
8024 KB |
Output is correct |
28 |
Correct |
3 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
7004 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
2 ms |
4956 KB |
Output is correct |
32 |
Correct |
2 ms |
5724 KB |
Output is correct |
33 |
Correct |
3 ms |
7004 KB |
Output is correct |
34 |
Correct |
3 ms |
6236 KB |
Output is correct |
35 |
Correct |
3 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6236 KB |
Output is correct |
37 |
Correct |
2 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
4696 KB |
Output is correct |
39 |
Correct |
3 ms |
5980 KB |
Output is correct |
40 |
Correct |
0 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 |
344 KB |
Output is correct |
2 |
Correct |
97 ms |
327004 KB |
Output is correct |
3 |
Correct |
97 ms |
326136 KB |
Output is correct |
4 |
Correct |
96 ms |
326980 KB |
Output is correct |
5 |
Correct |
100 ms |
327252 KB |
Output is correct |
6 |
Correct |
96 ms |
326920 KB |
Output is correct |
7 |
Correct |
97 ms |
324392 KB |
Output is correct |
8 |
Correct |
96 ms |
324432 KB |
Output is correct |
9 |
Correct |
94 ms |
323840 KB |
Output is correct |
10 |
Correct |
99 ms |
326992 KB |
Output is correct |
11 |
Correct |
106 ms |
326992 KB |
Output is correct |
12 |
Correct |
29 ms |
88068 KB |
Output is correct |
13 |
Correct |
48 ms |
154448 KB |
Output is correct |
14 |
Correct |
72 ms |
223240 KB |
Output is correct |
15 |
Correct |
107 ms |
325420 KB |
Output is correct |
16 |
Correct |
104 ms |
325456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
912 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
0 ms |
724 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 |
860 KB |
Output is correct |
16 |
Correct |
0 ms |
1116 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
7260 KB |
Output is correct |
19 |
Correct |
2 ms |
6236 KB |
Output is correct |
20 |
Correct |
3 ms |
7580 KB |
Output is correct |
21 |
Correct |
3 ms |
6212 KB |
Output is correct |
22 |
Correct |
3 ms |
7516 KB |
Output is correct |
23 |
Correct |
2 ms |
5468 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
4 ms |
10076 KB |
Output is correct |
26 |
Correct |
4 ms |
8536 KB |
Output is correct |
27 |
Correct |
3 ms |
8024 KB |
Output is correct |
28 |
Correct |
3 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
7004 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
2 ms |
4956 KB |
Output is correct |
32 |
Correct |
2 ms |
5724 KB |
Output is correct |
33 |
Correct |
3 ms |
7004 KB |
Output is correct |
34 |
Correct |
3 ms |
6236 KB |
Output is correct |
35 |
Correct |
3 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6236 KB |
Output is correct |
37 |
Correct |
2 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
4696 KB |
Output is correct |
39 |
Correct |
3 ms |
5980 KB |
Output is correct |
40 |
Correct |
0 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
3164 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
97 ms |
327004 KB |
Output is correct |
44 |
Correct |
97 ms |
326136 KB |
Output is correct |
45 |
Correct |
96 ms |
326980 KB |
Output is correct |
46 |
Correct |
100 ms |
327252 KB |
Output is correct |
47 |
Correct |
96 ms |
326920 KB |
Output is correct |
48 |
Correct |
97 ms |
324392 KB |
Output is correct |
49 |
Correct |
96 ms |
324432 KB |
Output is correct |
50 |
Correct |
94 ms |
323840 KB |
Output is correct |
51 |
Correct |
99 ms |
326992 KB |
Output is correct |
52 |
Correct |
106 ms |
326992 KB |
Output is correct |
53 |
Correct |
29 ms |
88068 KB |
Output is correct |
54 |
Correct |
48 ms |
154448 KB |
Output is correct |
55 |
Correct |
72 ms |
223240 KB |
Output is correct |
56 |
Correct |
107 ms |
325420 KB |
Output is correct |
57 |
Correct |
104 ms |
325456 KB |
Output is correct |
58 |
Correct |
151 ms |
377376 KB |
Output is correct |
59 |
Correct |
150 ms |
397392 KB |
Output is correct |
60 |
Correct |
158 ms |
404560 KB |
Output is correct |
61 |
Correct |
150 ms |
387824 KB |
Output is correct |
62 |
Correct |
120 ms |
357652 KB |
Output is correct |
63 |
Correct |
115 ms |
333648 KB |
Output is correct |
64 |
Correct |
138 ms |
366164 KB |
Output is correct |
65 |
Correct |
125 ms |
345636 KB |
Output is correct |
66 |
Correct |
156 ms |
389212 KB |
Output is correct |
67 |
Correct |
150 ms |
329104 KB |
Output is correct |
68 |
Correct |
153 ms |
388652 KB |
Output is correct |
69 |
Correct |
161 ms |
397396 KB |
Output is correct |
70 |
Correct |
153 ms |
380416 KB |
Output is correct |
71 |
Correct |
178 ms |
438100 KB |
Output is correct |
72 |
Correct |
244 ms |
689268 KB |
Output is correct |
73 |
Correct |
10 ms |
21488 KB |
Output is correct |