#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define all(x) x.begin(), x.end()
template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 & y) {if (x > y) x = y;}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 & y) {if (x < y) x = y;}
const int MAXN = 410;
int n;
int a[MAXN];
int pos[MAXN][3];
int cnt[MAXN][3];
void read() {
cin >> n;
for (int i = 1; i <= n; i++) {
char x;
cin >> x;
if (x == 'R') a[i] = 0;
else if (x == 'G') a[i] = 1;
else a[i] = 2;
for (int j = 0; j < 3; j++)
cnt[i][j] = cnt[i - 1][j];
cnt[i][a[i]]++;
pos[cnt[i][a[i]]][a[i]] = i;
}
}
const int INF = 1e9 + 10;
vector<vector<vector<vector<int>>>> dp;
void build() {
dp.assign(cnt[n][0] + 1, vector<vector<vector<int>>> (cnt[n][1] + 1, vector<vector<int>> (cnt[n][2] + 1, vector<int> (3, INF))));
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
}
void calc() {
for (int i = 0; i <= cnt[n][0]; i++) {
for (int j = 0; j <= cnt[n][1]; j++) {
for (int k = 0; k <= cnt[n][2]; k++) {
for (int ch = 0; ch < 3; ch++) {
if (ch != 0 && i + 1 <= cnt[n][0]) {
int p = pos[i + 1][0];
chkmin(dp[i + 1][j][k][0], dp[i][j][k][ch] + max(0, cnt[p][1] - j) + max(0, cnt[p][2] - k));
}
if (ch != 1 && j + 1 <= cnt[n][1]) {
int p = pos[j + 1][1];
chkmin(dp[i][j + 1][k][1], dp[i][j][k][ch] + max(0, cnt[p][0] - i) + max(0, cnt[p][2] - k));
}
if (ch != 2 && k + 1 <= cnt[n][2]) {
int p = pos[k + 1][2];
chkmin(dp[i][j][k + 1][2], dp[i][j][k][ch] + max(0, cnt[p][0] - i) + max(0, cnt[p][1] - j));
}
}
}
}
}
}
int ans;
void make_ans() {
ans = INF;
for (int i = 0; i < 3; i++) {
chkmin(ans, dp[cnt[n][0]][cnt[n][1]][cnt[n][2]][i]);
}
if (ans == INF) ans = -1;
}
void run() {
build();
calc();
make_ans();
}
void write() {
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
run();
write();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
888 KB |
Output is correct |
19 |
Correct |
6 ms |
888 KB |
Output is correct |
20 |
Correct |
6 ms |
888 KB |
Output is correct |
21 |
Correct |
6 ms |
888 KB |
Output is correct |
22 |
Correct |
5 ms |
760 KB |
Output is correct |
23 |
Correct |
6 ms |
888 KB |
Output is correct |
24 |
Correct |
5 ms |
760 KB |
Output is correct |
25 |
Correct |
5 ms |
504 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
6 ms |
756 KB |
Output is correct |
28 |
Correct |
6 ms |
888 KB |
Output is correct |
29 |
Correct |
7 ms |
888 KB |
Output is correct |
30 |
Correct |
7 ms |
888 KB |
Output is correct |
31 |
Correct |
6 ms |
888 KB |
Output is correct |
32 |
Correct |
5 ms |
888 KB |
Output is correct |
33 |
Correct |
5 ms |
504 KB |
Output is correct |
34 |
Correct |
5 ms |
632 KB |
Output is correct |
35 |
Correct |
6 ms |
760 KB |
Output is correct |
36 |
Correct |
6 ms |
888 KB |
Output is correct |
37 |
Correct |
6 ms |
760 KB |
Output is correct |
38 |
Correct |
6 ms |
888 KB |
Output is correct |
39 |
Correct |
6 ms |
888 KB |
Output is correct |
40 |
Correct |
6 ms |
376 KB |
Output is correct |
41 |
Correct |
5 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
3832 KB |
Output is correct |
3 |
Correct |
10 ms |
3832 KB |
Output is correct |
4 |
Correct |
11 ms |
3832 KB |
Output is correct |
5 |
Correct |
10 ms |
3832 KB |
Output is correct |
6 |
Correct |
14 ms |
3832 KB |
Output is correct |
7 |
Correct |
13 ms |
3832 KB |
Output is correct |
8 |
Correct |
11 ms |
3832 KB |
Output is correct |
9 |
Correct |
10 ms |
3836 KB |
Output is correct |
10 |
Correct |
10 ms |
3832 KB |
Output is correct |
11 |
Correct |
10 ms |
3832 KB |
Output is correct |
12 |
Correct |
6 ms |
1276 KB |
Output is correct |
13 |
Correct |
7 ms |
2040 KB |
Output is correct |
14 |
Correct |
8 ms |
2680 KB |
Output is correct |
15 |
Correct |
12 ms |
3832 KB |
Output is correct |
16 |
Correct |
10 ms |
3832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
888 KB |
Output is correct |
19 |
Correct |
6 ms |
888 KB |
Output is correct |
20 |
Correct |
6 ms |
888 KB |
Output is correct |
21 |
Correct |
6 ms |
888 KB |
Output is correct |
22 |
Correct |
5 ms |
760 KB |
Output is correct |
23 |
Correct |
6 ms |
888 KB |
Output is correct |
24 |
Correct |
5 ms |
760 KB |
Output is correct |
25 |
Correct |
5 ms |
504 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
6 ms |
756 KB |
Output is correct |
28 |
Correct |
6 ms |
888 KB |
Output is correct |
29 |
Correct |
7 ms |
888 KB |
Output is correct |
30 |
Correct |
7 ms |
888 KB |
Output is correct |
31 |
Correct |
6 ms |
888 KB |
Output is correct |
32 |
Correct |
5 ms |
888 KB |
Output is correct |
33 |
Correct |
5 ms |
504 KB |
Output is correct |
34 |
Correct |
5 ms |
632 KB |
Output is correct |
35 |
Correct |
6 ms |
760 KB |
Output is correct |
36 |
Correct |
6 ms |
888 KB |
Output is correct |
37 |
Correct |
6 ms |
760 KB |
Output is correct |
38 |
Correct |
6 ms |
888 KB |
Output is correct |
39 |
Correct |
6 ms |
888 KB |
Output is correct |
40 |
Correct |
6 ms |
376 KB |
Output is correct |
41 |
Correct |
5 ms |
632 KB |
Output is correct |
42 |
Correct |
5 ms |
376 KB |
Output is correct |
43 |
Correct |
11 ms |
3832 KB |
Output is correct |
44 |
Correct |
10 ms |
3832 KB |
Output is correct |
45 |
Correct |
11 ms |
3832 KB |
Output is correct |
46 |
Correct |
10 ms |
3832 KB |
Output is correct |
47 |
Correct |
14 ms |
3832 KB |
Output is correct |
48 |
Correct |
13 ms |
3832 KB |
Output is correct |
49 |
Correct |
11 ms |
3832 KB |
Output is correct |
50 |
Correct |
10 ms |
3836 KB |
Output is correct |
51 |
Correct |
10 ms |
3832 KB |
Output is correct |
52 |
Correct |
10 ms |
3832 KB |
Output is correct |
53 |
Correct |
6 ms |
1276 KB |
Output is correct |
54 |
Correct |
7 ms |
2040 KB |
Output is correct |
55 |
Correct |
8 ms |
2680 KB |
Output is correct |
56 |
Correct |
12 ms |
3832 KB |
Output is correct |
57 |
Correct |
10 ms |
3832 KB |
Output is correct |
58 |
Correct |
235 ms |
133240 KB |
Output is correct |
59 |
Correct |
278 ms |
133244 KB |
Output is correct |
60 |
Correct |
239 ms |
132396 KB |
Output is correct |
61 |
Correct |
236 ms |
133880 KB |
Output is correct |
62 |
Correct |
25 ms |
12412 KB |
Output is correct |
63 |
Correct |
37 ms |
20728 KB |
Output is correct |
64 |
Correct |
118 ms |
66680 KB |
Output is correct |
65 |
Correct |
164 ms |
95608 KB |
Output is correct |
66 |
Correct |
223 ms |
131832 KB |
Output is correct |
67 |
Correct |
230 ms |
131832 KB |
Output is correct |
68 |
Correct |
244 ms |
133880 KB |
Output is correct |
69 |
Correct |
237 ms |
134776 KB |
Output is correct |
70 |
Correct |
233 ms |
134136 KB |
Output is correct |
71 |
Correct |
234 ms |
131064 KB |
Output is correct |
72 |
Correct |
60 ms |
34168 KB |
Output is correct |
73 |
Correct |
20 ms |
10616 KB |
Output is correct |