#include <bits/stdc++.h>
//ako ovaj vaso daso misli da me pobedjuje.....
using namespace std;
#define int long long
#define f first
#define s second
const int N = 410;
const int inf = 1e9 + 69;
int dp[N][N][N][3], n, pr[3][N];
string s;
vector<int> pos[3];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> s;
int a = 0, b = 0, c = 0;
for (int i = 0;i < n;i++) {
if (s[i] == 'R')
s[i] = 'A';
else if (s[i] == 'G')
s[i] = 'B';
else
s[i] = 'C';
a += (s[i] == 'A');
b += (s[i] == 'B');
c += (s[i] == 'C');
pos[s[i] - 'A'].push_back(i);
}
for (int i = 0;i < n;i++) {
if (i > 0) {
pr[0][i] += pr[0][i - 1];
pr[1][i] += pr[1][i - 1];
pr[2][i] += pr[2][i - 1];
}
pr[0][i] += (s[i] == 'A');
pr[1][i] += (s[i] == 'B');
pr[2][i] += (s[i] == 'C');
}
for (int i = 0;i <= a;i++) {
for (int j = 0;j <= b;j++) {
for (int k = 0;k <= c;k++)
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = inf;
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
auto C = [&](int L, int R) {
if (L == 0)
return pr[2][R];
else if (R < L)
return 0ll;
return pr[2][R] - pr[2][L - 1];
};
auto B = [&](int L, int R) {
if (L == 0)
return pr[1][R];
else if (R < L)
return 0ll;
return pr[1][R] - pr[1][L - 1];
};
auto A = [&](int L, int R) {
if (L == 0)
return pr[0][R];
else if (R < L)
return 0ll;
return pr[0][R] - pr[0][L - 1];
};
for (int i = 0;i <= a;i++) {
for (int j = 0;j <= b;j++) {
for (int k = 0;k <= c;k++) {
int p = i + j + k - 1;
if (i > 0) {
int L = p, R = pos[0][i - 1] + 1;
int p1 = 0, p2 = 0;
if (j > 0)
p1 = pos[1][j - 1];
if (k > 0)
p2 = pos[2][k - 1];
dp[i][j][k][0] = min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + (R - 1 + C(R, p2) + B(R, p1) - L);
}
if (j > 0) {
int L = p, R = pos[1][j - 1] + 1;
int p1 = 0, p2 = 0;
if (i > 0)
p1 = pos[0][i - 1];
if (k > 0)
p2 = pos[2][k - 1];
dp[i][j][k][1] = min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + (R - 1 + A(R, p1) + C(R, p2) - L);
}
if (k > 0) {
int L = p, R = pos[2][k - 1] + 1;
int p1 = 0, p2 = 0;
if (i > 0)
p1 = pos[0][i - 1];
if (j > 0)
p2 = pos[1][j - 1];
dp[i][j][k][2] = min(dp[i][j][k - 1][1], dp[i][j][k - 1][0]) + (R - 1 + A(R, p1) + B(R, p2) - L);
}
}
}
}
int ans = inf;
for (int i = 0;i < 3;i++)
ans = min(ans, dp[a][b][c][i]);
if (ans >= inf)
ans = -1;
cout << ans;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
5 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
3 ms |
12632 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
5 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
3 ms |
12632 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
8 ms |
51804 KB |
Output is correct |
19 |
Correct |
7 ms |
41564 KB |
Output is correct |
20 |
Correct |
9 ms |
53852 KB |
Output is correct |
21 |
Correct |
7 ms |
45660 KB |
Output is correct |
22 |
Correct |
8 ms |
49752 KB |
Output is correct |
23 |
Correct |
7 ms |
39516 KB |
Output is correct |
24 |
Correct |
4 ms |
22876 KB |
Output is correct |
25 |
Correct |
14 ms |
82524 KB |
Output is correct |
26 |
Correct |
11 ms |
70232 KB |
Output is correct |
27 |
Correct |
10 ms |
62296 KB |
Output is correct |
28 |
Correct |
8 ms |
47708 KB |
Output is correct |
29 |
Correct |
8 ms |
49756 KB |
Output is correct |
30 |
Correct |
6 ms |
39516 KB |
Output is correct |
31 |
Correct |
5 ms |
35420 KB |
Output is correct |
32 |
Correct |
6 ms |
41564 KB |
Output is correct |
33 |
Correct |
8 ms |
57948 KB |
Output is correct |
34 |
Correct |
8 ms |
51804 KB |
Output is correct |
35 |
Correct |
8 ms |
51804 KB |
Output is correct |
36 |
Correct |
6 ms |
43612 KB |
Output is correct |
37 |
Correct |
5 ms |
31320 KB |
Output is correct |
38 |
Correct |
5 ms |
33320 KB |
Output is correct |
39 |
Correct |
6 ms |
43612 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
4 ms |
23120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
108 ms |
698288 KB |
Output is correct |
3 |
Correct |
106 ms |
701216 KB |
Output is correct |
4 |
Correct |
104 ms |
701300 KB |
Output is correct |
5 |
Correct |
107 ms |
703060 KB |
Output is correct |
6 |
Correct |
108 ms |
704520 KB |
Output is correct |
7 |
Correct |
110 ms |
703592 KB |
Output is correct |
8 |
Correct |
109 ms |
703568 KB |
Output is correct |
9 |
Correct |
105 ms |
703568 KB |
Output is correct |
10 |
Correct |
104 ms |
706384 KB |
Output is correct |
11 |
Correct |
105 ms |
704464 KB |
Output is correct |
12 |
Correct |
42 ms |
312404 KB |
Output is correct |
13 |
Correct |
65 ms |
468376 KB |
Output is correct |
14 |
Correct |
85 ms |
607832 KB |
Output is correct |
15 |
Correct |
109 ms |
699220 KB |
Output is correct |
16 |
Correct |
105 ms |
699096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
5 ms |
20828 KB |
Output is correct |
11 |
Correct |
2 ms |
10584 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
3 ms |
12632 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
8 ms |
51804 KB |
Output is correct |
19 |
Correct |
7 ms |
41564 KB |
Output is correct |
20 |
Correct |
9 ms |
53852 KB |
Output is correct |
21 |
Correct |
7 ms |
45660 KB |
Output is correct |
22 |
Correct |
8 ms |
49752 KB |
Output is correct |
23 |
Correct |
7 ms |
39516 KB |
Output is correct |
24 |
Correct |
4 ms |
22876 KB |
Output is correct |
25 |
Correct |
14 ms |
82524 KB |
Output is correct |
26 |
Correct |
11 ms |
70232 KB |
Output is correct |
27 |
Correct |
10 ms |
62296 KB |
Output is correct |
28 |
Correct |
8 ms |
47708 KB |
Output is correct |
29 |
Correct |
8 ms |
49756 KB |
Output is correct |
30 |
Correct |
6 ms |
39516 KB |
Output is correct |
31 |
Correct |
5 ms |
35420 KB |
Output is correct |
32 |
Correct |
6 ms |
41564 KB |
Output is correct |
33 |
Correct |
8 ms |
57948 KB |
Output is correct |
34 |
Correct |
8 ms |
51804 KB |
Output is correct |
35 |
Correct |
8 ms |
51804 KB |
Output is correct |
36 |
Correct |
6 ms |
43612 KB |
Output is correct |
37 |
Correct |
5 ms |
31320 KB |
Output is correct |
38 |
Correct |
5 ms |
33320 KB |
Output is correct |
39 |
Correct |
6 ms |
43612 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
4 ms |
23120 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
108 ms |
698288 KB |
Output is correct |
44 |
Correct |
106 ms |
701216 KB |
Output is correct |
45 |
Correct |
104 ms |
701300 KB |
Output is correct |
46 |
Correct |
107 ms |
703060 KB |
Output is correct |
47 |
Correct |
108 ms |
704520 KB |
Output is correct |
48 |
Correct |
110 ms |
703592 KB |
Output is correct |
49 |
Correct |
109 ms |
703568 KB |
Output is correct |
50 |
Correct |
105 ms |
703568 KB |
Output is correct |
51 |
Correct |
104 ms |
706384 KB |
Output is correct |
52 |
Correct |
105 ms |
704464 KB |
Output is correct |
53 |
Correct |
42 ms |
312404 KB |
Output is correct |
54 |
Correct |
65 ms |
468376 KB |
Output is correct |
55 |
Correct |
85 ms |
607832 KB |
Output is correct |
56 |
Correct |
109 ms |
699220 KB |
Output is correct |
57 |
Correct |
105 ms |
699096 KB |
Output is correct |
58 |
Correct |
85 ms |
410964 KB |
Output is correct |
59 |
Correct |
96 ms |
488808 KB |
Output is correct |
60 |
Correct |
91 ms |
466256 KB |
Output is correct |
61 |
Correct |
84 ms |
445672 KB |
Output is correct |
62 |
Correct |
118 ms |
744956 KB |
Output is correct |
63 |
Correct |
117 ms |
738028 KB |
Output is correct |
64 |
Correct |
117 ms |
710536 KB |
Output is correct |
65 |
Correct |
109 ms |
609844 KB |
Output is correct |
66 |
Correct |
89 ms |
454216 KB |
Output is correct |
67 |
Correct |
78 ms |
400728 KB |
Output is correct |
68 |
Correct |
88 ms |
462164 KB |
Output is correct |
69 |
Correct |
87 ms |
454072 KB |
Output is correct |
70 |
Correct |
89 ms |
464552 KB |
Output is correct |
71 |
Correct |
91 ms |
482788 KB |
Output is correct |
72 |
Correct |
104 ms |
693948 KB |
Output is correct |
73 |
Correct |
5 ms |
19032 KB |
Output is correct |