#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);
}
//cout << i[0] << " " << i[1] << " " << i[2] << " " << p << endl;
//cout << dp[i[0]][i[1]][i[2]][p] << endl;
}
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 |
379 ms |
757496 KB |
Output is correct |
2 |
Correct |
370 ms |
757496 KB |
Output is correct |
3 |
Correct |
374 ms |
757564 KB |
Output is correct |
4 |
Correct |
377 ms |
757496 KB |
Output is correct |
5 |
Correct |
370 ms |
757496 KB |
Output is correct |
6 |
Correct |
373 ms |
757496 KB |
Output is correct |
7 |
Correct |
371 ms |
757548 KB |
Output is correct |
8 |
Correct |
369 ms |
757496 KB |
Output is correct |
9 |
Correct |
374 ms |
757624 KB |
Output is correct |
10 |
Correct |
368 ms |
757496 KB |
Output is correct |
11 |
Correct |
375 ms |
757496 KB |
Output is correct |
12 |
Correct |
367 ms |
757496 KB |
Output is correct |
13 |
Correct |
371 ms |
757496 KB |
Output is correct |
14 |
Correct |
365 ms |
757496 KB |
Output is correct |
15 |
Correct |
367 ms |
757496 KB |
Output is correct |
16 |
Correct |
367 ms |
757500 KB |
Output is correct |
17 |
Correct |
366 ms |
757500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
379 ms |
757496 KB |
Output is correct |
2 |
Correct |
370 ms |
757496 KB |
Output is correct |
3 |
Correct |
374 ms |
757564 KB |
Output is correct |
4 |
Correct |
377 ms |
757496 KB |
Output is correct |
5 |
Correct |
370 ms |
757496 KB |
Output is correct |
6 |
Correct |
373 ms |
757496 KB |
Output is correct |
7 |
Correct |
371 ms |
757548 KB |
Output is correct |
8 |
Correct |
369 ms |
757496 KB |
Output is correct |
9 |
Correct |
374 ms |
757624 KB |
Output is correct |
10 |
Correct |
368 ms |
757496 KB |
Output is correct |
11 |
Correct |
375 ms |
757496 KB |
Output is correct |
12 |
Correct |
367 ms |
757496 KB |
Output is correct |
13 |
Correct |
371 ms |
757496 KB |
Output is correct |
14 |
Correct |
365 ms |
757496 KB |
Output is correct |
15 |
Correct |
367 ms |
757496 KB |
Output is correct |
16 |
Correct |
367 ms |
757500 KB |
Output is correct |
17 |
Correct |
366 ms |
757500 KB |
Output is correct |
18 |
Correct |
376 ms |
757496 KB |
Output is correct |
19 |
Correct |
374 ms |
757496 KB |
Output is correct |
20 |
Correct |
373 ms |
757496 KB |
Output is correct |
21 |
Correct |
372 ms |
757496 KB |
Output is correct |
22 |
Correct |
378 ms |
757536 KB |
Output is correct |
23 |
Correct |
370 ms |
757472 KB |
Output is correct |
24 |
Correct |
367 ms |
757496 KB |
Output is correct |
25 |
Correct |
369 ms |
757496 KB |
Output is correct |
26 |
Correct |
367 ms |
757496 KB |
Output is correct |
27 |
Correct |
371 ms |
757496 KB |
Output is correct |
28 |
Correct |
370 ms |
757496 KB |
Output is correct |
29 |
Correct |
370 ms |
757604 KB |
Output is correct |
30 |
Correct |
373 ms |
757496 KB |
Output is correct |
31 |
Correct |
369 ms |
757496 KB |
Output is correct |
32 |
Correct |
368 ms |
757496 KB |
Output is correct |
33 |
Correct |
380 ms |
757496 KB |
Output is correct |
34 |
Correct |
369 ms |
757496 KB |
Output is correct |
35 |
Correct |
373 ms |
757496 KB |
Output is correct |
36 |
Correct |
372 ms |
757496 KB |
Output is correct |
37 |
Correct |
367 ms |
757504 KB |
Output is correct |
38 |
Correct |
374 ms |
757752 KB |
Output is correct |
39 |
Correct |
367 ms |
757496 KB |
Output is correct |
40 |
Correct |
371 ms |
757624 KB |
Output is correct |
41 |
Correct |
372 ms |
757496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
368 ms |
757496 KB |
Output is correct |
2 |
Correct |
377 ms |
757496 KB |
Output is correct |
3 |
Correct |
373 ms |
757496 KB |
Output is correct |
4 |
Correct |
368 ms |
757624 KB |
Output is correct |
5 |
Correct |
369 ms |
757468 KB |
Output is correct |
6 |
Correct |
373 ms |
757456 KB |
Output is correct |
7 |
Correct |
370 ms |
757752 KB |
Output is correct |
8 |
Correct |
372 ms |
757496 KB |
Output is correct |
9 |
Correct |
378 ms |
757496 KB |
Output is correct |
10 |
Correct |
374 ms |
757496 KB |
Output is correct |
11 |
Correct |
374 ms |
757496 KB |
Output is correct |
12 |
Correct |
372 ms |
757628 KB |
Output is correct |
13 |
Correct |
373 ms |
757496 KB |
Output is correct |
14 |
Correct |
377 ms |
757472 KB |
Output is correct |
15 |
Correct |
375 ms |
757624 KB |
Output is correct |
16 |
Correct |
375 ms |
757496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
379 ms |
757496 KB |
Output is correct |
2 |
Correct |
370 ms |
757496 KB |
Output is correct |
3 |
Correct |
374 ms |
757564 KB |
Output is correct |
4 |
Correct |
377 ms |
757496 KB |
Output is correct |
5 |
Correct |
370 ms |
757496 KB |
Output is correct |
6 |
Correct |
373 ms |
757496 KB |
Output is correct |
7 |
Correct |
371 ms |
757548 KB |
Output is correct |
8 |
Correct |
369 ms |
757496 KB |
Output is correct |
9 |
Correct |
374 ms |
757624 KB |
Output is correct |
10 |
Correct |
368 ms |
757496 KB |
Output is correct |
11 |
Correct |
375 ms |
757496 KB |
Output is correct |
12 |
Correct |
367 ms |
757496 KB |
Output is correct |
13 |
Correct |
371 ms |
757496 KB |
Output is correct |
14 |
Correct |
365 ms |
757496 KB |
Output is correct |
15 |
Correct |
367 ms |
757496 KB |
Output is correct |
16 |
Correct |
367 ms |
757500 KB |
Output is correct |
17 |
Correct |
366 ms |
757500 KB |
Output is correct |
18 |
Correct |
376 ms |
757496 KB |
Output is correct |
19 |
Correct |
374 ms |
757496 KB |
Output is correct |
20 |
Correct |
373 ms |
757496 KB |
Output is correct |
21 |
Correct |
372 ms |
757496 KB |
Output is correct |
22 |
Correct |
378 ms |
757536 KB |
Output is correct |
23 |
Correct |
370 ms |
757472 KB |
Output is correct |
24 |
Correct |
367 ms |
757496 KB |
Output is correct |
25 |
Correct |
369 ms |
757496 KB |
Output is correct |
26 |
Correct |
367 ms |
757496 KB |
Output is correct |
27 |
Correct |
371 ms |
757496 KB |
Output is correct |
28 |
Correct |
370 ms |
757496 KB |
Output is correct |
29 |
Correct |
370 ms |
757604 KB |
Output is correct |
30 |
Correct |
373 ms |
757496 KB |
Output is correct |
31 |
Correct |
369 ms |
757496 KB |
Output is correct |
32 |
Correct |
368 ms |
757496 KB |
Output is correct |
33 |
Correct |
380 ms |
757496 KB |
Output is correct |
34 |
Correct |
369 ms |
757496 KB |
Output is correct |
35 |
Correct |
373 ms |
757496 KB |
Output is correct |
36 |
Correct |
372 ms |
757496 KB |
Output is correct |
37 |
Correct |
367 ms |
757504 KB |
Output is correct |
38 |
Correct |
374 ms |
757752 KB |
Output is correct |
39 |
Correct |
367 ms |
757496 KB |
Output is correct |
40 |
Correct |
371 ms |
757624 KB |
Output is correct |
41 |
Correct |
372 ms |
757496 KB |
Output is correct |
42 |
Correct |
368 ms |
757496 KB |
Output is correct |
43 |
Correct |
377 ms |
757496 KB |
Output is correct |
44 |
Correct |
373 ms |
757496 KB |
Output is correct |
45 |
Correct |
368 ms |
757624 KB |
Output is correct |
46 |
Correct |
369 ms |
757468 KB |
Output is correct |
47 |
Correct |
373 ms |
757456 KB |
Output is correct |
48 |
Correct |
370 ms |
757752 KB |
Output is correct |
49 |
Correct |
372 ms |
757496 KB |
Output is correct |
50 |
Correct |
378 ms |
757496 KB |
Output is correct |
51 |
Correct |
374 ms |
757496 KB |
Output is correct |
52 |
Correct |
374 ms |
757496 KB |
Output is correct |
53 |
Correct |
372 ms |
757628 KB |
Output is correct |
54 |
Correct |
373 ms |
757496 KB |
Output is correct |
55 |
Correct |
377 ms |
757472 KB |
Output is correct |
56 |
Correct |
375 ms |
757624 KB |
Output is correct |
57 |
Correct |
375 ms |
757496 KB |
Output is correct |
58 |
Correct |
457 ms |
757496 KB |
Output is correct |
59 |
Correct |
455 ms |
757496 KB |
Output is correct |
60 |
Correct |
456 ms |
757496 KB |
Output is correct |
61 |
Correct |
465 ms |
757424 KB |
Output is correct |
62 |
Correct |
376 ms |
757496 KB |
Output is correct |
63 |
Correct |
368 ms |
757496 KB |
Output is correct |
64 |
Correct |
406 ms |
757496 KB |
Output is correct |
65 |
Correct |
409 ms |
757496 KB |
Output is correct |
66 |
Correct |
456 ms |
757624 KB |
Output is correct |
67 |
Correct |
455 ms |
757624 KB |
Output is correct |
68 |
Correct |
460 ms |
757628 KB |
Output is correct |
69 |
Correct |
459 ms |
757624 KB |
Output is correct |
70 |
Correct |
461 ms |
757624 KB |
Output is correct |
71 |
Correct |
444 ms |
757624 KB |
Output is correct |
72 |
Correct |
376 ms |
757496 KB |
Output is correct |
73 |
Correct |
368 ms |
757496 KB |
Output is correct |