#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
const int MAXN = 405;
const int INF = 1e9;
int dp[MAXN][MAXN][MAXN][3];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
for (int i = 0; i < MAXN; ++i) {
for (int j = 0; j < MAXN; ++j) {
for (int k = 0; k < MAXN; ++k) {
for (int last = 0; last < 3; ++last) {
dp[i][j][k][last] = INF;
}
}
}
}
int n;
cin >> n;
string s;
cin >> s;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
a[i] = (s[i] == 'R' ? 0 : (s[i] == 'G' ? 1 : 2));
}
vector<vector<int>> pref(3, vector<int>(n + 1));
array<vector<int>, 3> inds;
for (int i = 0; i < n; ++i) {
inds[a[i]].push_back(i);
pref[a[i]][i + 1] = 1;
pref[0][i + 1] += pref[0][i];
pref[1][i + 1] += pref[1][i];
pref[2][i + 1] += pref[2][i];
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int r = 0; r <= pref[0].back(); ++r) {
for (int g = 0; g <= pref[1].back(); ++g) {
for (int y = 0; y <= pref[2].back(); ++y) {
for (int last = 0; last < 3; ++last) {
if (last == 0 && r > 0) {
int pos = inds[0][r - 1];
dp[r][g][y][last] = min(dp[r][g][y][last], min(dp[r - 1][g][y][1], dp[r - 1][g][y][2]) + max(0, pref[1][pos] - g) + max(0, pref[2][pos] - y));
}
if (last == 1 && g > 0) {
int pos = inds[1][g - 1];
dp[r][g][y][last] = min(dp[r][g][y][last], min(dp[r][g - 1][y][0], dp[r][g - 1][y][2]) + max(0, pref[0][pos] - r) + max(0, pref[2][pos] - y));
}
if (last == 2 && y > 0) {
int pos = inds[2][y - 1];
dp[r][g][y][last] = min(dp[r][g][y][last], min(dp[r][g][y - 1][0], dp[r][g][y - 1][1]) + max(0, pref[0][pos] - r) + max(0, pref[1][pos] - g));
}
}
}
}
}
int r = pref[0].back();
int g = pref[1].back();
int y = pref[2].back();
int ans = min(dp[r][g][y][0], min(dp[r][g][y][1], dp[r][g][y][2]));
cout << (ans == INF ? -1 : ans) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
780372 KB |
Output is correct |
2 |
Correct |
181 ms |
780376 KB |
Output is correct |
3 |
Correct |
175 ms |
780372 KB |
Output is correct |
4 |
Correct |
176 ms |
780372 KB |
Output is correct |
5 |
Correct |
174 ms |
780240 KB |
Output is correct |
6 |
Correct |
177 ms |
780400 KB |
Output is correct |
7 |
Correct |
190 ms |
780372 KB |
Output is correct |
8 |
Correct |
170 ms |
780388 KB |
Output is correct |
9 |
Correct |
176 ms |
780372 KB |
Output is correct |
10 |
Correct |
170 ms |
780368 KB |
Output is correct |
11 |
Correct |
170 ms |
780420 KB |
Output is correct |
12 |
Correct |
181 ms |
780572 KB |
Output is correct |
13 |
Correct |
169 ms |
780208 KB |
Output is correct |
14 |
Correct |
168 ms |
780308 KB |
Output is correct |
15 |
Correct |
174 ms |
780376 KB |
Output is correct |
16 |
Correct |
170 ms |
780196 KB |
Output is correct |
17 |
Correct |
172 ms |
780368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
780372 KB |
Output is correct |
2 |
Correct |
181 ms |
780376 KB |
Output is correct |
3 |
Correct |
175 ms |
780372 KB |
Output is correct |
4 |
Correct |
176 ms |
780372 KB |
Output is correct |
5 |
Correct |
174 ms |
780240 KB |
Output is correct |
6 |
Correct |
177 ms |
780400 KB |
Output is correct |
7 |
Correct |
190 ms |
780372 KB |
Output is correct |
8 |
Correct |
170 ms |
780388 KB |
Output is correct |
9 |
Correct |
176 ms |
780372 KB |
Output is correct |
10 |
Correct |
170 ms |
780368 KB |
Output is correct |
11 |
Correct |
170 ms |
780420 KB |
Output is correct |
12 |
Correct |
181 ms |
780572 KB |
Output is correct |
13 |
Correct |
169 ms |
780208 KB |
Output is correct |
14 |
Correct |
168 ms |
780308 KB |
Output is correct |
15 |
Correct |
174 ms |
780376 KB |
Output is correct |
16 |
Correct |
170 ms |
780196 KB |
Output is correct |
17 |
Correct |
172 ms |
780368 KB |
Output is correct |
18 |
Correct |
169 ms |
780280 KB |
Output is correct |
19 |
Correct |
172 ms |
780432 KB |
Output is correct |
20 |
Correct |
168 ms |
780368 KB |
Output is correct |
21 |
Correct |
197 ms |
780496 KB |
Output is correct |
22 |
Correct |
175 ms |
780300 KB |
Output is correct |
23 |
Correct |
169 ms |
780196 KB |
Output is correct |
24 |
Correct |
174 ms |
780376 KB |
Output is correct |
25 |
Correct |
175 ms |
780568 KB |
Output is correct |
26 |
Correct |
171 ms |
780272 KB |
Output is correct |
27 |
Correct |
174 ms |
780372 KB |
Output is correct |
28 |
Correct |
172 ms |
780372 KB |
Output is correct |
29 |
Correct |
169 ms |
780268 KB |
Output is correct |
30 |
Correct |
169 ms |
780260 KB |
Output is correct |
31 |
Correct |
167 ms |
780368 KB |
Output is correct |
32 |
Correct |
169 ms |
780368 KB |
Output is correct |
33 |
Correct |
171 ms |
780372 KB |
Output is correct |
34 |
Correct |
170 ms |
780344 KB |
Output is correct |
35 |
Correct |
173 ms |
780428 KB |
Output is correct |
36 |
Correct |
173 ms |
780368 KB |
Output is correct |
37 |
Correct |
170 ms |
780368 KB |
Output is correct |
38 |
Correct |
171 ms |
780372 KB |
Output is correct |
39 |
Correct |
171 ms |
780368 KB |
Output is correct |
40 |
Correct |
171 ms |
780368 KB |
Output is correct |
41 |
Correct |
171 ms |
780492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
175 ms |
780348 KB |
Output is correct |
2 |
Correct |
173 ms |
780252 KB |
Output is correct |
3 |
Correct |
178 ms |
780372 KB |
Output is correct |
4 |
Correct |
175 ms |
780420 KB |
Output is correct |
5 |
Correct |
171 ms |
780372 KB |
Output is correct |
6 |
Correct |
193 ms |
780420 KB |
Output is correct |
7 |
Correct |
171 ms |
780312 KB |
Output is correct |
8 |
Correct |
171 ms |
780400 KB |
Output is correct |
9 |
Correct |
171 ms |
780372 KB |
Output is correct |
10 |
Correct |
176 ms |
780636 KB |
Output is correct |
11 |
Correct |
173 ms |
780316 KB |
Output is correct |
12 |
Correct |
169 ms |
780368 KB |
Output is correct |
13 |
Correct |
170 ms |
780372 KB |
Output is correct |
14 |
Correct |
170 ms |
780368 KB |
Output is correct |
15 |
Correct |
172 ms |
780376 KB |
Output is correct |
16 |
Correct |
171 ms |
780216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
780372 KB |
Output is correct |
2 |
Correct |
181 ms |
780376 KB |
Output is correct |
3 |
Correct |
175 ms |
780372 KB |
Output is correct |
4 |
Correct |
176 ms |
780372 KB |
Output is correct |
5 |
Correct |
174 ms |
780240 KB |
Output is correct |
6 |
Correct |
177 ms |
780400 KB |
Output is correct |
7 |
Correct |
190 ms |
780372 KB |
Output is correct |
8 |
Correct |
170 ms |
780388 KB |
Output is correct |
9 |
Correct |
176 ms |
780372 KB |
Output is correct |
10 |
Correct |
170 ms |
780368 KB |
Output is correct |
11 |
Correct |
170 ms |
780420 KB |
Output is correct |
12 |
Correct |
181 ms |
780572 KB |
Output is correct |
13 |
Correct |
169 ms |
780208 KB |
Output is correct |
14 |
Correct |
168 ms |
780308 KB |
Output is correct |
15 |
Correct |
174 ms |
780376 KB |
Output is correct |
16 |
Correct |
170 ms |
780196 KB |
Output is correct |
17 |
Correct |
172 ms |
780368 KB |
Output is correct |
18 |
Correct |
169 ms |
780280 KB |
Output is correct |
19 |
Correct |
172 ms |
780432 KB |
Output is correct |
20 |
Correct |
168 ms |
780368 KB |
Output is correct |
21 |
Correct |
197 ms |
780496 KB |
Output is correct |
22 |
Correct |
175 ms |
780300 KB |
Output is correct |
23 |
Correct |
169 ms |
780196 KB |
Output is correct |
24 |
Correct |
174 ms |
780376 KB |
Output is correct |
25 |
Correct |
175 ms |
780568 KB |
Output is correct |
26 |
Correct |
171 ms |
780272 KB |
Output is correct |
27 |
Correct |
174 ms |
780372 KB |
Output is correct |
28 |
Correct |
172 ms |
780372 KB |
Output is correct |
29 |
Correct |
169 ms |
780268 KB |
Output is correct |
30 |
Correct |
169 ms |
780260 KB |
Output is correct |
31 |
Correct |
167 ms |
780368 KB |
Output is correct |
32 |
Correct |
169 ms |
780368 KB |
Output is correct |
33 |
Correct |
171 ms |
780372 KB |
Output is correct |
34 |
Correct |
170 ms |
780344 KB |
Output is correct |
35 |
Correct |
173 ms |
780428 KB |
Output is correct |
36 |
Correct |
173 ms |
780368 KB |
Output is correct |
37 |
Correct |
170 ms |
780368 KB |
Output is correct |
38 |
Correct |
171 ms |
780372 KB |
Output is correct |
39 |
Correct |
171 ms |
780368 KB |
Output is correct |
40 |
Correct |
171 ms |
780368 KB |
Output is correct |
41 |
Correct |
171 ms |
780492 KB |
Output is correct |
42 |
Correct |
175 ms |
780348 KB |
Output is correct |
43 |
Correct |
173 ms |
780252 KB |
Output is correct |
44 |
Correct |
178 ms |
780372 KB |
Output is correct |
45 |
Correct |
175 ms |
780420 KB |
Output is correct |
46 |
Correct |
171 ms |
780372 KB |
Output is correct |
47 |
Correct |
193 ms |
780420 KB |
Output is correct |
48 |
Correct |
171 ms |
780312 KB |
Output is correct |
49 |
Correct |
171 ms |
780400 KB |
Output is correct |
50 |
Correct |
171 ms |
780372 KB |
Output is correct |
51 |
Correct |
176 ms |
780636 KB |
Output is correct |
52 |
Correct |
173 ms |
780316 KB |
Output is correct |
53 |
Correct |
169 ms |
780368 KB |
Output is correct |
54 |
Correct |
170 ms |
780372 KB |
Output is correct |
55 |
Correct |
170 ms |
780368 KB |
Output is correct |
56 |
Correct |
172 ms |
780376 KB |
Output is correct |
57 |
Correct |
171 ms |
780216 KB |
Output is correct |
58 |
Correct |
196 ms |
780264 KB |
Output is correct |
59 |
Correct |
193 ms |
780364 KB |
Output is correct |
60 |
Correct |
195 ms |
780372 KB |
Output is correct |
61 |
Correct |
202 ms |
780360 KB |
Output is correct |
62 |
Correct |
176 ms |
780356 KB |
Output is correct |
63 |
Correct |
183 ms |
780316 KB |
Output is correct |
64 |
Correct |
185 ms |
780628 KB |
Output is correct |
65 |
Correct |
189 ms |
780268 KB |
Output is correct |
66 |
Correct |
192 ms |
780388 KB |
Output is correct |
67 |
Correct |
192 ms |
780368 KB |
Output is correct |
68 |
Correct |
199 ms |
780296 KB |
Output is correct |
69 |
Correct |
193 ms |
780372 KB |
Output is correct |
70 |
Correct |
200 ms |
780368 KB |
Output is correct |
71 |
Correct |
194 ms |
780372 KB |
Output is correct |
72 |
Correct |
178 ms |
780372 KB |
Output is correct |
73 |
Correct |
170 ms |
780356 KB |
Output is correct |