#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int inf = 0x3f3f3f3f;
const int maxn = 401;
int n;
string s;
int a[3][maxn];
int dp[maxn][maxn][maxn][3];
vector<int> v[3];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for(int i = 0; i < n; i++){
int c = s[i] == 'R' ? 0 : 1 + (s[i] == 'G');
a[c][i + 1] = 1;
v[c].push_back(i + 1);
}
for(int t = 0; t < 3; t++)
for(int i = 1; i <= n; i++){
a[t][i] += a[t][i - 1];
}
memset(dp, inf, sizeof(dp));
for(int i[3] = {0}; i[0] <= a[0][n]; i[0]++)
for(i[1] = 0; i[1] <= a[1][n]; i[1]++)
for(i[2] = 0; i[2] <= a[2][n]; i[2]++)
for(int p = 0; p < 3; p++){
if(i[0] + i[1] + i[2] == 0){
dp[0][0][0][p] = 0;
}
if(dp[i[0]][i[1]][i[2]][p] == inf) continue;
for(int j = 0; j < 3; j++){
if(j == p || i[j] == a[j][n]) continue;
int l[3] = {i[0], i[1], i[2]};
l[j]++;
int cp = 0;
for(int k = 0; k < 3; k++){
if(k == j) continue;
cp += max(a[k][v[j][i[j]]] - i[k], 0);
}
dp[l[0]][l[1]][l[2]][j] = min(dp[l[0]][l[1]][l[2]][j],
dp[i[0]][i[1]][i[2]][p] + cp);
}
}
int ret = inf;
for(int i = 0; i < 3; i++){
ret = min(ret, dp[a[0][n]][a[1][n]][a[2][n]][i]);
}
cout << (ret == inf ? -1 : ret) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
757496 KB |
Output is correct |
2 |
Correct |
372 ms |
757384 KB |
Output is correct |
3 |
Correct |
370 ms |
757496 KB |
Output is correct |
4 |
Correct |
376 ms |
757452 KB |
Output is correct |
5 |
Correct |
367 ms |
757496 KB |
Output is correct |
6 |
Correct |
369 ms |
757492 KB |
Output is correct |
7 |
Correct |
366 ms |
757496 KB |
Output is correct |
8 |
Correct |
363 ms |
757496 KB |
Output is correct |
9 |
Correct |
372 ms |
757496 KB |
Output is correct |
10 |
Correct |
376 ms |
757440 KB |
Output is correct |
11 |
Correct |
367 ms |
757496 KB |
Output is correct |
12 |
Correct |
369 ms |
757496 KB |
Output is correct |
13 |
Correct |
368 ms |
757496 KB |
Output is correct |
14 |
Correct |
366 ms |
757444 KB |
Output is correct |
15 |
Correct |
369 ms |
757576 KB |
Output is correct |
16 |
Correct |
372 ms |
757496 KB |
Output is correct |
17 |
Correct |
366 ms |
757556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
757496 KB |
Output is correct |
2 |
Correct |
372 ms |
757384 KB |
Output is correct |
3 |
Correct |
370 ms |
757496 KB |
Output is correct |
4 |
Correct |
376 ms |
757452 KB |
Output is correct |
5 |
Correct |
367 ms |
757496 KB |
Output is correct |
6 |
Correct |
369 ms |
757492 KB |
Output is correct |
7 |
Correct |
366 ms |
757496 KB |
Output is correct |
8 |
Correct |
363 ms |
757496 KB |
Output is correct |
9 |
Correct |
372 ms |
757496 KB |
Output is correct |
10 |
Correct |
376 ms |
757440 KB |
Output is correct |
11 |
Correct |
367 ms |
757496 KB |
Output is correct |
12 |
Correct |
369 ms |
757496 KB |
Output is correct |
13 |
Correct |
368 ms |
757496 KB |
Output is correct |
14 |
Correct |
366 ms |
757444 KB |
Output is correct |
15 |
Correct |
369 ms |
757576 KB |
Output is correct |
16 |
Correct |
372 ms |
757496 KB |
Output is correct |
17 |
Correct |
366 ms |
757556 KB |
Output is correct |
18 |
Correct |
374 ms |
757496 KB |
Output is correct |
19 |
Correct |
367 ms |
757472 KB |
Output is correct |
20 |
Correct |
379 ms |
757496 KB |
Output is correct |
21 |
Correct |
369 ms |
757568 KB |
Output is correct |
22 |
Correct |
366 ms |
757496 KB |
Output is correct |
23 |
Correct |
368 ms |
757616 KB |
Output is correct |
24 |
Correct |
368 ms |
757652 KB |
Output is correct |
25 |
Correct |
370 ms |
757496 KB |
Output is correct |
26 |
Correct |
376 ms |
757496 KB |
Output is correct |
27 |
Correct |
374 ms |
757548 KB |
Output is correct |
28 |
Correct |
377 ms |
757496 KB |
Output is correct |
29 |
Correct |
374 ms |
757512 KB |
Output is correct |
30 |
Correct |
370 ms |
757496 KB |
Output is correct |
31 |
Correct |
371 ms |
757588 KB |
Output is correct |
32 |
Correct |
374 ms |
757624 KB |
Output is correct |
33 |
Correct |
375 ms |
757496 KB |
Output is correct |
34 |
Correct |
368 ms |
757496 KB |
Output is correct |
35 |
Correct |
364 ms |
757496 KB |
Output is correct |
36 |
Correct |
366 ms |
757496 KB |
Output is correct |
37 |
Correct |
368 ms |
757532 KB |
Output is correct |
38 |
Correct |
378 ms |
757496 KB |
Output is correct |
39 |
Correct |
370 ms |
757496 KB |
Output is correct |
40 |
Correct |
383 ms |
757496 KB |
Output is correct |
41 |
Correct |
371 ms |
757624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
366 ms |
757496 KB |
Output is correct |
2 |
Correct |
373 ms |
757496 KB |
Output is correct |
3 |
Correct |
367 ms |
757432 KB |
Output is correct |
4 |
Correct |
369 ms |
757496 KB |
Output is correct |
5 |
Correct |
367 ms |
757624 KB |
Output is correct |
6 |
Correct |
377 ms |
757496 KB |
Output is correct |
7 |
Correct |
370 ms |
757444 KB |
Output is correct |
8 |
Correct |
365 ms |
757496 KB |
Output is correct |
9 |
Correct |
371 ms |
757500 KB |
Output is correct |
10 |
Correct |
370 ms |
757496 KB |
Output is correct |
11 |
Correct |
369 ms |
757496 KB |
Output is correct |
12 |
Correct |
369 ms |
757496 KB |
Output is correct |
13 |
Correct |
368 ms |
757624 KB |
Output is correct |
14 |
Correct |
373 ms |
757624 KB |
Output is correct |
15 |
Correct |
376 ms |
757472 KB |
Output is correct |
16 |
Correct |
373 ms |
757624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
757496 KB |
Output is correct |
2 |
Correct |
372 ms |
757384 KB |
Output is correct |
3 |
Correct |
370 ms |
757496 KB |
Output is correct |
4 |
Correct |
376 ms |
757452 KB |
Output is correct |
5 |
Correct |
367 ms |
757496 KB |
Output is correct |
6 |
Correct |
369 ms |
757492 KB |
Output is correct |
7 |
Correct |
366 ms |
757496 KB |
Output is correct |
8 |
Correct |
363 ms |
757496 KB |
Output is correct |
9 |
Correct |
372 ms |
757496 KB |
Output is correct |
10 |
Correct |
376 ms |
757440 KB |
Output is correct |
11 |
Correct |
367 ms |
757496 KB |
Output is correct |
12 |
Correct |
369 ms |
757496 KB |
Output is correct |
13 |
Correct |
368 ms |
757496 KB |
Output is correct |
14 |
Correct |
366 ms |
757444 KB |
Output is correct |
15 |
Correct |
369 ms |
757576 KB |
Output is correct |
16 |
Correct |
372 ms |
757496 KB |
Output is correct |
17 |
Correct |
366 ms |
757556 KB |
Output is correct |
18 |
Correct |
374 ms |
757496 KB |
Output is correct |
19 |
Correct |
367 ms |
757472 KB |
Output is correct |
20 |
Correct |
379 ms |
757496 KB |
Output is correct |
21 |
Correct |
369 ms |
757568 KB |
Output is correct |
22 |
Correct |
366 ms |
757496 KB |
Output is correct |
23 |
Correct |
368 ms |
757616 KB |
Output is correct |
24 |
Correct |
368 ms |
757652 KB |
Output is correct |
25 |
Correct |
370 ms |
757496 KB |
Output is correct |
26 |
Correct |
376 ms |
757496 KB |
Output is correct |
27 |
Correct |
374 ms |
757548 KB |
Output is correct |
28 |
Correct |
377 ms |
757496 KB |
Output is correct |
29 |
Correct |
374 ms |
757512 KB |
Output is correct |
30 |
Correct |
370 ms |
757496 KB |
Output is correct |
31 |
Correct |
371 ms |
757588 KB |
Output is correct |
32 |
Correct |
374 ms |
757624 KB |
Output is correct |
33 |
Correct |
375 ms |
757496 KB |
Output is correct |
34 |
Correct |
368 ms |
757496 KB |
Output is correct |
35 |
Correct |
364 ms |
757496 KB |
Output is correct |
36 |
Correct |
366 ms |
757496 KB |
Output is correct |
37 |
Correct |
368 ms |
757532 KB |
Output is correct |
38 |
Correct |
378 ms |
757496 KB |
Output is correct |
39 |
Correct |
370 ms |
757496 KB |
Output is correct |
40 |
Correct |
383 ms |
757496 KB |
Output is correct |
41 |
Correct |
371 ms |
757624 KB |
Output is correct |
42 |
Correct |
366 ms |
757496 KB |
Output is correct |
43 |
Correct |
373 ms |
757496 KB |
Output is correct |
44 |
Correct |
367 ms |
757432 KB |
Output is correct |
45 |
Correct |
369 ms |
757496 KB |
Output is correct |
46 |
Correct |
367 ms |
757624 KB |
Output is correct |
47 |
Correct |
377 ms |
757496 KB |
Output is correct |
48 |
Correct |
370 ms |
757444 KB |
Output is correct |
49 |
Correct |
365 ms |
757496 KB |
Output is correct |
50 |
Correct |
371 ms |
757500 KB |
Output is correct |
51 |
Correct |
370 ms |
757496 KB |
Output is correct |
52 |
Correct |
369 ms |
757496 KB |
Output is correct |
53 |
Correct |
369 ms |
757496 KB |
Output is correct |
54 |
Correct |
368 ms |
757624 KB |
Output is correct |
55 |
Correct |
373 ms |
757624 KB |
Output is correct |
56 |
Correct |
376 ms |
757472 KB |
Output is correct |
57 |
Correct |
373 ms |
757624 KB |
Output is correct |
58 |
Correct |
450 ms |
757496 KB |
Output is correct |
59 |
Correct |
462 ms |
757528 KB |
Output is correct |
60 |
Correct |
452 ms |
757500 KB |
Output is correct |
61 |
Correct |
456 ms |
757516 KB |
Output is correct |
62 |
Correct |
367 ms |
757472 KB |
Output is correct |
63 |
Correct |
381 ms |
757468 KB |
Output is correct |
64 |
Correct |
388 ms |
757496 KB |
Output is correct |
65 |
Correct |
409 ms |
757496 KB |
Output is correct |
66 |
Correct |
453 ms |
757496 KB |
Output is correct |
67 |
Correct |
449 ms |
757572 KB |
Output is correct |
68 |
Correct |
459 ms |
757624 KB |
Output is correct |
69 |
Correct |
457 ms |
757496 KB |
Output is correct |
70 |
Correct |
469 ms |
757496 KB |
Output is correct |
71 |
Correct |
447 ms |
757500 KB |
Output is correct |
72 |
Correct |
379 ms |
757496 KB |
Output is correct |
73 |
Correct |
375 ms |
757496 KB |
Output is correct |