#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 400 + 12, MOD = (int)1e9 + 7;
int n,dp[N][N][N][3],pr[N],pg[N],py[N];
vector<int> r,g,y;
void test() {
cin >> n;
string s;cin >> s;
for(int i = 0;i < n;i++) {
if(i) {
pr[i] = pr[i - 1];
pg[i] = pg[i - 1];
py[i] = py[i - 1];
}
if(s[i] == 'R') {
pr[i]++;
r.push_back(i);
} else if(s[i] == 'Y') {
y.push_back(i);
py[i]++;
} else {
g.push_back(i);
pg[i]++;
}
}
int r_ = (int)r.size(),g_ = (int)g.size(),y_ = (int)y.size();
for(int i = 0;i <= n;i++) {
for(int j = 0;j <= n;j++) {
for(int k = 0;k <= n;k++) {
for(int t = 0;t < 3;t++) {
dp[i][j][k][t] = 1e9;
}
}
}
}
for(int i = 0;i < 3;i++) {
dp[0][0][0][i] = 0;
}
for(int i = 0;i <= r_;i++) {
for(int j = 0;j <= g_;j++) {
for(int k = 0;k <= y_;k++) {
for(int t = 0;t < 3;t++){
int all = i + j + k;
auto get = [&](int ind) -> int{
int ret =0 ;
ret += max(0,pr[ind] - i);
ret += max(0,pg[ind] - k);
ret += max(0,py[ind] - j);
return ret;
};
int ind;
if(i < r_ && t != 0) {
ind = r[i];
int val = max(0,pg[ind] - j) + max(0,py[ind] - k);
dp[i + 1][j][k][0] = min(dp[i + 1][j][k][0],dp[i][j][k][t] + val);
}
if(j < g_ && t != 1) {
ind = g[j];
int val = max(0,pr[ind] - i) + max(0,py[ind] - k);
dp[i][j + 1][k][1] = min(dp[i][j + 1][k][1],dp[i][j][k][t] + val);
}
if(k < y_ && t != 2) {
ind = y[k];
int val = max(0,pr[ind] - i) + max(0,pg[ind] - j);
dp[i][j][k + 1][2] = min(dp[i][j][k + 1][2],dp[i][j][k][t] +val);
}
}
}
}
}
int res = 1e9;
for(int i = 0;i < 3;i++) {
res = min(res,dp[r_][g_][y_][i]);
}
if(res == 1e9) {
cout << -1;
return;
}
cout << res ;
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'void test()':
joi2019_ho_t3.cpp:47:25: warning: unused variable 'all' [-Wunused-variable]
47 | int all = i + j + k;
| ^~~
joi2019_ho_t3.cpp:48:26: warning: variable 'get' set but not used [-Wunused-but-set-variable]
48 | auto get = [&](int ind) -> int{
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
31068 KB |
Output is correct |
6 |
Correct |
3 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
31068 KB |
Output is correct |
8 |
Correct |
3 ms |
31068 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
3 ms |
31304 KB |
Output is correct |
11 |
Correct |
3 ms |
31308 KB |
Output is correct |
12 |
Correct |
3 ms |
31068 KB |
Output is correct |
13 |
Correct |
3 ms |
31288 KB |
Output is correct |
14 |
Correct |
3 ms |
29020 KB |
Output is correct |
15 |
Correct |
3 ms |
31308 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
3 ms |
29240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
31068 KB |
Output is correct |
6 |
Correct |
3 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
31068 KB |
Output is correct |
8 |
Correct |
3 ms |
31068 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
3 ms |
31304 KB |
Output is correct |
11 |
Correct |
3 ms |
31308 KB |
Output is correct |
12 |
Correct |
3 ms |
31068 KB |
Output is correct |
13 |
Correct |
3 ms |
31288 KB |
Output is correct |
14 |
Correct |
3 ms |
29020 KB |
Output is correct |
15 |
Correct |
3 ms |
31308 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
3 ms |
29240 KB |
Output is correct |
18 |
Correct |
8 ms |
70748 KB |
Output is correct |
19 |
Correct |
8 ms |
70704 KB |
Output is correct |
20 |
Correct |
8 ms |
72540 KB |
Output is correct |
21 |
Correct |
8 ms |
70748 KB |
Output is correct |
22 |
Correct |
8 ms |
70716 KB |
Output is correct |
23 |
Correct |
8 ms |
72540 KB |
Output is correct |
24 |
Correct |
8 ms |
72668 KB |
Output is correct |
25 |
Correct |
9 ms |
72432 KB |
Output is correct |
26 |
Correct |
9 ms |
70748 KB |
Output is correct |
27 |
Correct |
9 ms |
70748 KB |
Output is correct |
28 |
Correct |
8 ms |
72624 KB |
Output is correct |
29 |
Correct |
8 ms |
70748 KB |
Output is correct |
30 |
Correct |
8 ms |
70868 KB |
Output is correct |
31 |
Correct |
8 ms |
72540 KB |
Output is correct |
32 |
Correct |
8 ms |
70748 KB |
Output is correct |
33 |
Correct |
8 ms |
70236 KB |
Output is correct |
34 |
Correct |
8 ms |
70236 KB |
Output is correct |
35 |
Correct |
8 ms |
69804 KB |
Output is correct |
36 |
Correct |
9 ms |
70236 KB |
Output is correct |
37 |
Correct |
8 ms |
69468 KB |
Output is correct |
38 |
Correct |
8 ms |
71004 KB |
Output is correct |
39 |
Correct |
9 ms |
70748 KB |
Output is correct |
40 |
Correct |
8 ms |
70236 KB |
Output is correct |
41 |
Correct |
8 ms |
70748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
244 ms |
781196 KB |
Output is correct |
3 |
Correct |
257 ms |
777536 KB |
Output is correct |
4 |
Correct |
267 ms |
781396 KB |
Output is correct |
5 |
Correct |
258 ms |
781468 KB |
Output is correct |
6 |
Correct |
244 ms |
781392 KB |
Output is correct |
7 |
Correct |
250 ms |
777552 KB |
Output is correct |
8 |
Correct |
260 ms |
777552 KB |
Output is correct |
9 |
Correct |
279 ms |
773716 KB |
Output is correct |
10 |
Correct |
248 ms |
781456 KB |
Output is correct |
11 |
Correct |
243 ms |
781396 KB |
Output is correct |
12 |
Correct |
65 ms |
240064 KB |
Output is correct |
13 |
Correct |
108 ms |
388180 KB |
Output is correct |
14 |
Correct |
253 ms |
543672 KB |
Output is correct |
15 |
Correct |
266 ms |
781392 KB |
Output is correct |
16 |
Correct |
272 ms |
781516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
20828 KB |
Output is correct |
5 |
Correct |
3 ms |
31068 KB |
Output is correct |
6 |
Correct |
3 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
31068 KB |
Output is correct |
8 |
Correct |
3 ms |
31068 KB |
Output is correct |
9 |
Correct |
4 ms |
31068 KB |
Output is correct |
10 |
Correct |
3 ms |
31304 KB |
Output is correct |
11 |
Correct |
3 ms |
31308 KB |
Output is correct |
12 |
Correct |
3 ms |
31068 KB |
Output is correct |
13 |
Correct |
3 ms |
31288 KB |
Output is correct |
14 |
Correct |
3 ms |
29020 KB |
Output is correct |
15 |
Correct |
3 ms |
31308 KB |
Output is correct |
16 |
Correct |
3 ms |
31068 KB |
Output is correct |
17 |
Correct |
3 ms |
29240 KB |
Output is correct |
18 |
Correct |
8 ms |
70748 KB |
Output is correct |
19 |
Correct |
8 ms |
70704 KB |
Output is correct |
20 |
Correct |
8 ms |
72540 KB |
Output is correct |
21 |
Correct |
8 ms |
70748 KB |
Output is correct |
22 |
Correct |
8 ms |
70716 KB |
Output is correct |
23 |
Correct |
8 ms |
72540 KB |
Output is correct |
24 |
Correct |
8 ms |
72668 KB |
Output is correct |
25 |
Correct |
9 ms |
72432 KB |
Output is correct |
26 |
Correct |
9 ms |
70748 KB |
Output is correct |
27 |
Correct |
9 ms |
70748 KB |
Output is correct |
28 |
Correct |
8 ms |
72624 KB |
Output is correct |
29 |
Correct |
8 ms |
70748 KB |
Output is correct |
30 |
Correct |
8 ms |
70868 KB |
Output is correct |
31 |
Correct |
8 ms |
72540 KB |
Output is correct |
32 |
Correct |
8 ms |
70748 KB |
Output is correct |
33 |
Correct |
8 ms |
70236 KB |
Output is correct |
34 |
Correct |
8 ms |
70236 KB |
Output is correct |
35 |
Correct |
8 ms |
69804 KB |
Output is correct |
36 |
Correct |
9 ms |
70236 KB |
Output is correct |
37 |
Correct |
8 ms |
69468 KB |
Output is correct |
38 |
Correct |
8 ms |
71004 KB |
Output is correct |
39 |
Correct |
9 ms |
70748 KB |
Output is correct |
40 |
Correct |
8 ms |
70236 KB |
Output is correct |
41 |
Correct |
8 ms |
70748 KB |
Output is correct |
42 |
Correct |
1 ms |
6488 KB |
Output is correct |
43 |
Correct |
244 ms |
781196 KB |
Output is correct |
44 |
Correct |
257 ms |
777536 KB |
Output is correct |
45 |
Correct |
267 ms |
781396 KB |
Output is correct |
46 |
Correct |
258 ms |
781468 KB |
Output is correct |
47 |
Correct |
244 ms |
781392 KB |
Output is correct |
48 |
Correct |
250 ms |
777552 KB |
Output is correct |
49 |
Correct |
260 ms |
777552 KB |
Output is correct |
50 |
Correct |
279 ms |
773716 KB |
Output is correct |
51 |
Correct |
248 ms |
781456 KB |
Output is correct |
52 |
Correct |
243 ms |
781396 KB |
Output is correct |
53 |
Correct |
65 ms |
240064 KB |
Output is correct |
54 |
Correct |
108 ms |
388180 KB |
Output is correct |
55 |
Correct |
253 ms |
543672 KB |
Output is correct |
56 |
Correct |
266 ms |
781392 KB |
Output is correct |
57 |
Correct |
272 ms |
781516 KB |
Output is correct |
58 |
Correct |
269 ms |
781396 KB |
Output is correct |
59 |
Correct |
273 ms |
781164 KB |
Output is correct |
60 |
Correct |
280 ms |
773968 KB |
Output is correct |
61 |
Correct |
275 ms |
777556 KB |
Output is correct |
62 |
Correct |
284 ms |
781396 KB |
Output is correct |
63 |
Correct |
254 ms |
781652 KB |
Output is correct |
64 |
Correct |
265 ms |
781396 KB |
Output is correct |
65 |
Correct |
269 ms |
777576 KB |
Output is correct |
66 |
Correct |
292 ms |
770132 KB |
Output is correct |
67 |
Correct |
299 ms |
781220 KB |
Output is correct |
68 |
Correct |
286 ms |
777440 KB |
Output is correct |
69 |
Correct |
286 ms |
781300 KB |
Output is correct |
70 |
Correct |
290 ms |
781288 KB |
Output is correct |
71 |
Correct |
271 ms |
777556 KB |
Output is correct |
72 |
Correct |
271 ms |
777556 KB |
Output is correct |
73 |
Correct |
264 ms |
781396 KB |
Output is correct |