/* In the name of God, aka Allah */
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 405;
int n, dp[N][N][N][3];
string s;
vector<int> vc[3];
pii vt[3][N];
void solve() {
cin >> n;
cin >> s;
s = '.'+s;
for (int i = 1; i <= n; i++) {
int c = (s[i] != 'R') + (s[i] == 'Y');
vc[c].push_back(i);
}
int a, b, c;
b = 0, c = 0;
for (int A = 0; A < (int)vc[0].size(); A++) {
while (b < (int)vc[1].size() && vc[1][b] < vc[0][A]) b++;
while (c < (int)vc[2].size() && vc[2][c] < vc[0][A]) c++;
vt[0][A] = {b, c};
}
a = 0, c = 0;
for (int B = 0; B < (int)vc[1].size(); B++) {
while (a < (int)vc[0].size() && vc[0][a] < vc[1][B]) a++;
while (c < (int)vc[2].size() && vc[2][c] < vc[1][B]) c++;
vt[1][B] = {a, c};
}
a = 0, b = 0;
for (int C = 0; C < (int)vc[2].size(); C++) {
while (a < (int)vc[0].size() && vc[0][a] < vc[2][C]) a++;
while (b < (int)vc[1].size() && vc[1][b] < vc[2][C]) b++;
vt[2][C] = {a, b};
}
memset(dp, 64, sizeof dp);
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int sum = 0; sum < n; sum++) {
for (int A = 0; A <= min(sum, (int)vc[0].size()); A++) {
for (int B = 0; B <= min(sum-A, (int)vc[1].size()); B++) {
int C = sum-A-B;
if (A < (int)vc[0].size()) {
int b = vt[0][A].F;
int c = vt[0][A].S;
dp[A+1][B][C][0] = min(dp[A+1][B][C][0], min(dp[A][B][C][1], dp[A][B][C][2]) + vc[0][A]+max(B-b, 0)+max(C-c, 0)-(A+B+C+1));
}
if (B < (int)vc[1].size()) {
int a = vt[1][B].F;
int c = vt[1][B].S;
dp[A][B+1][C][1] = min(dp[A][B+1][C][1], min(dp[A][B][C][0], dp[A][B][C][2]) + vc[1][B]+max(A-a, 0)+max(C-c, 0)-(A+B+C+1));
}
if (C < (int)vc[2].size()) {
int a = vt[2][C].F;
int b = vt[2][C].S;
dp[A][B][C+1][2] = min(dp[A][B][C+1][2], min(dp[A][B][C][0], dp[A][B][C][1]) + vc[2][C]+max(A-a,0)+max(B-b, 0)-(A+B+C+1));
}
}
}
}
int ans = 1e9;
ans = min(dp[vc[0].size()][vc[1].size()][vc[2].size()][0], ans);
ans = min(dp[vc[0].size()][vc[1].size()][vc[2].size()][1], ans);
ans = min(dp[vc[0].size()][vc[1].size()][vc[2].size()][2], ans);
cout << (ans >= 1e9? -1 : ans) << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
282 ms |
780304 KB |
Output is correct |
2 |
Correct |
277 ms |
780336 KB |
Output is correct |
3 |
Correct |
283 ms |
780200 KB |
Output is correct |
4 |
Correct |
277 ms |
780260 KB |
Output is correct |
5 |
Correct |
299 ms |
780232 KB |
Output is correct |
6 |
Correct |
277 ms |
780232 KB |
Output is correct |
7 |
Correct |
299 ms |
780360 KB |
Output is correct |
8 |
Correct |
286 ms |
780236 KB |
Output is correct |
9 |
Correct |
287 ms |
780204 KB |
Output is correct |
10 |
Correct |
293 ms |
780324 KB |
Output is correct |
11 |
Correct |
295 ms |
780244 KB |
Output is correct |
12 |
Correct |
281 ms |
780292 KB |
Output is correct |
13 |
Correct |
286 ms |
780232 KB |
Output is correct |
14 |
Correct |
287 ms |
780316 KB |
Output is correct |
15 |
Correct |
283 ms |
780268 KB |
Output is correct |
16 |
Correct |
276 ms |
780292 KB |
Output is correct |
17 |
Correct |
281 ms |
780256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
282 ms |
780304 KB |
Output is correct |
2 |
Correct |
277 ms |
780336 KB |
Output is correct |
3 |
Correct |
283 ms |
780200 KB |
Output is correct |
4 |
Correct |
277 ms |
780260 KB |
Output is correct |
5 |
Correct |
299 ms |
780232 KB |
Output is correct |
6 |
Correct |
277 ms |
780232 KB |
Output is correct |
7 |
Correct |
299 ms |
780360 KB |
Output is correct |
8 |
Correct |
286 ms |
780236 KB |
Output is correct |
9 |
Correct |
287 ms |
780204 KB |
Output is correct |
10 |
Correct |
293 ms |
780324 KB |
Output is correct |
11 |
Correct |
295 ms |
780244 KB |
Output is correct |
12 |
Correct |
281 ms |
780292 KB |
Output is correct |
13 |
Correct |
286 ms |
780232 KB |
Output is correct |
14 |
Correct |
287 ms |
780316 KB |
Output is correct |
15 |
Correct |
283 ms |
780268 KB |
Output is correct |
16 |
Correct |
276 ms |
780292 KB |
Output is correct |
17 |
Correct |
281 ms |
780256 KB |
Output is correct |
18 |
Correct |
295 ms |
780300 KB |
Output is correct |
19 |
Correct |
327 ms |
780280 KB |
Output is correct |
20 |
Correct |
326 ms |
780232 KB |
Output is correct |
21 |
Correct |
353 ms |
780260 KB |
Output is correct |
22 |
Correct |
324 ms |
780308 KB |
Output is correct |
23 |
Correct |
380 ms |
780240 KB |
Output is correct |
24 |
Correct |
354 ms |
780332 KB |
Output is correct |
25 |
Correct |
288 ms |
780200 KB |
Output is correct |
26 |
Correct |
288 ms |
780236 KB |
Output is correct |
27 |
Correct |
285 ms |
780236 KB |
Output is correct |
28 |
Correct |
277 ms |
780184 KB |
Output is correct |
29 |
Correct |
281 ms |
780232 KB |
Output is correct |
30 |
Correct |
278 ms |
780280 KB |
Output is correct |
31 |
Correct |
282 ms |
780248 KB |
Output is correct |
32 |
Correct |
278 ms |
780216 KB |
Output is correct |
33 |
Correct |
284 ms |
780276 KB |
Output is correct |
34 |
Correct |
274 ms |
780224 KB |
Output is correct |
35 |
Correct |
316 ms |
780288 KB |
Output is correct |
36 |
Correct |
282 ms |
780244 KB |
Output is correct |
37 |
Correct |
275 ms |
780384 KB |
Output is correct |
38 |
Correct |
283 ms |
780232 KB |
Output is correct |
39 |
Correct |
276 ms |
780196 KB |
Output is correct |
40 |
Correct |
273 ms |
780224 KB |
Output is correct |
41 |
Correct |
282 ms |
780308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
281 ms |
780240 KB |
Output is correct |
2 |
Correct |
393 ms |
780344 KB |
Output is correct |
3 |
Correct |
387 ms |
780204 KB |
Output is correct |
4 |
Correct |
376 ms |
780328 KB |
Output is correct |
5 |
Correct |
392 ms |
780236 KB |
Output is correct |
6 |
Correct |
372 ms |
780312 KB |
Output is correct |
7 |
Correct |
368 ms |
780308 KB |
Output is correct |
8 |
Correct |
381 ms |
780452 KB |
Output is correct |
9 |
Correct |
378 ms |
780316 KB |
Output is correct |
10 |
Correct |
382 ms |
780364 KB |
Output is correct |
11 |
Correct |
373 ms |
780316 KB |
Output is correct |
12 |
Correct |
294 ms |
780320 KB |
Output is correct |
13 |
Correct |
296 ms |
780316 KB |
Output is correct |
14 |
Correct |
326 ms |
780372 KB |
Output is correct |
15 |
Correct |
371 ms |
780364 KB |
Output is correct |
16 |
Correct |
366 ms |
780284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
282 ms |
780304 KB |
Output is correct |
2 |
Correct |
277 ms |
780336 KB |
Output is correct |
3 |
Correct |
283 ms |
780200 KB |
Output is correct |
4 |
Correct |
277 ms |
780260 KB |
Output is correct |
5 |
Correct |
299 ms |
780232 KB |
Output is correct |
6 |
Correct |
277 ms |
780232 KB |
Output is correct |
7 |
Correct |
299 ms |
780360 KB |
Output is correct |
8 |
Correct |
286 ms |
780236 KB |
Output is correct |
9 |
Correct |
287 ms |
780204 KB |
Output is correct |
10 |
Correct |
293 ms |
780324 KB |
Output is correct |
11 |
Correct |
295 ms |
780244 KB |
Output is correct |
12 |
Correct |
281 ms |
780292 KB |
Output is correct |
13 |
Correct |
286 ms |
780232 KB |
Output is correct |
14 |
Correct |
287 ms |
780316 KB |
Output is correct |
15 |
Correct |
283 ms |
780268 KB |
Output is correct |
16 |
Correct |
276 ms |
780292 KB |
Output is correct |
17 |
Correct |
281 ms |
780256 KB |
Output is correct |
18 |
Correct |
295 ms |
780300 KB |
Output is correct |
19 |
Correct |
327 ms |
780280 KB |
Output is correct |
20 |
Correct |
326 ms |
780232 KB |
Output is correct |
21 |
Correct |
353 ms |
780260 KB |
Output is correct |
22 |
Correct |
324 ms |
780308 KB |
Output is correct |
23 |
Correct |
380 ms |
780240 KB |
Output is correct |
24 |
Correct |
354 ms |
780332 KB |
Output is correct |
25 |
Correct |
288 ms |
780200 KB |
Output is correct |
26 |
Correct |
288 ms |
780236 KB |
Output is correct |
27 |
Correct |
285 ms |
780236 KB |
Output is correct |
28 |
Correct |
277 ms |
780184 KB |
Output is correct |
29 |
Correct |
281 ms |
780232 KB |
Output is correct |
30 |
Correct |
278 ms |
780280 KB |
Output is correct |
31 |
Correct |
282 ms |
780248 KB |
Output is correct |
32 |
Correct |
278 ms |
780216 KB |
Output is correct |
33 |
Correct |
284 ms |
780276 KB |
Output is correct |
34 |
Correct |
274 ms |
780224 KB |
Output is correct |
35 |
Correct |
316 ms |
780288 KB |
Output is correct |
36 |
Correct |
282 ms |
780244 KB |
Output is correct |
37 |
Correct |
275 ms |
780384 KB |
Output is correct |
38 |
Correct |
283 ms |
780232 KB |
Output is correct |
39 |
Correct |
276 ms |
780196 KB |
Output is correct |
40 |
Correct |
273 ms |
780224 KB |
Output is correct |
41 |
Correct |
282 ms |
780308 KB |
Output is correct |
42 |
Correct |
281 ms |
780240 KB |
Output is correct |
43 |
Correct |
393 ms |
780344 KB |
Output is correct |
44 |
Correct |
387 ms |
780204 KB |
Output is correct |
45 |
Correct |
376 ms |
780328 KB |
Output is correct |
46 |
Correct |
392 ms |
780236 KB |
Output is correct |
47 |
Correct |
372 ms |
780312 KB |
Output is correct |
48 |
Correct |
368 ms |
780308 KB |
Output is correct |
49 |
Correct |
381 ms |
780452 KB |
Output is correct |
50 |
Correct |
378 ms |
780316 KB |
Output is correct |
51 |
Correct |
382 ms |
780364 KB |
Output is correct |
52 |
Correct |
373 ms |
780316 KB |
Output is correct |
53 |
Correct |
294 ms |
780320 KB |
Output is correct |
54 |
Correct |
296 ms |
780316 KB |
Output is correct |
55 |
Correct |
326 ms |
780372 KB |
Output is correct |
56 |
Correct |
371 ms |
780364 KB |
Output is correct |
57 |
Correct |
366 ms |
780284 KB |
Output is correct |
58 |
Correct |
318 ms |
780312 KB |
Output is correct |
59 |
Correct |
325 ms |
780296 KB |
Output is correct |
60 |
Correct |
347 ms |
780492 KB |
Output is correct |
61 |
Correct |
322 ms |
780288 KB |
Output is correct |
62 |
Correct |
366 ms |
780424 KB |
Output is correct |
63 |
Correct |
378 ms |
780428 KB |
Output is correct |
64 |
Correct |
361 ms |
780260 KB |
Output is correct |
65 |
Correct |
353 ms |
780420 KB |
Output is correct |
66 |
Correct |
346 ms |
780268 KB |
Output is correct |
67 |
Correct |
318 ms |
780228 KB |
Output is correct |
68 |
Correct |
363 ms |
780208 KB |
Output is correct |
69 |
Correct |
329 ms |
780340 KB |
Output is correct |
70 |
Correct |
329 ms |
780224 KB |
Output is correct |
71 |
Correct |
333 ms |
780288 KB |
Output is correct |
72 |
Correct |
296 ms |
780220 KB |
Output is correct |
73 |
Correct |
280 ms |
780368 KB |
Output is correct |