#include <bits/stdc++.h>
#define EmirBey ios_base::sync_with_stdio(NULL);cin.tie(0);cout.tie(0);
#define fopen(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define int long long
using namespace std;
const int N = 3e5+2;
const int INF = 1e9 + 1;
const int mod = 1e9+7;
int n, pref[3][N], cnt[3], ans = 1e18, dp[405][405][405][3];
vector<int> pos[3];
string s;
void solve() {
cin >> n >> s;
int x=0;
s='2'+s;
for (auto i : s) {
if (i == '2') continue;
x++;
for (int j = 0; j < 3; j++)pref[j][x] += pref[j][x - 1];
if (i == 'R') {
cnt[0]++;
pos[0].pb(x);
pref[0][x]++;
}else if(i == 'G') {
cnt[2]++;
pos[2].pb(x);
pref[2][x]++;
}else if(i == 'Y') {
cnt[1]++;
pos[1].pb(x);
pref[1][x]++;
}
}
for (int i = 0; i <= n; i++) {
for (int a = 0; a <= cnt[0]; a++) {
for (int b = 0; b <= cnt[1]; b++) {
for (int c = 0; c < 3; c++) dp[i][a][b][c] = 1e18;
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int i = 1; i <= n; i++) {
for (int a = 0; a <= cnt[0]; a++) {
for (int b = 0; b <= cnt[1]; b++) {
if (a + b > i) continue;
int c = i - (a + b);
if (c > cnt[2]) continue;
for (int last = 0; last < 3; last++) {
if (last != 0 && a) {
int j = pos[0][a - 1];
int adj = max(0ll, b - pref[1][j]) + max(0ll, c - pref[2][j]);
int val = max(0ll, j + adj - i);
dp[i][a][b][0] = min(dp[i][a][b][0], dp[i - 1][a - 1][b][last] + val);
}if (last != 1 && b) {
int j = pos[1][b - 1];
int adj = max(0ll, a - pref[0][j]) + max(0ll, c - pref[2][j]);
int val = max(0ll, j + adj - i);
dp[i][a][b][1] = min(dp[i][a][b][1], dp[i - 1][a][b - 1][last] + val);
}if (last != 2 && c) {
int j = pos[2][c - 1];
int adj = max(0ll, b - pref[1][j]) + max(0ll, a - pref[0][j]);
int val = max(0ll, j + adj - i);
dp[i][a][b][2] = min(dp[i][a][b][2], dp[i - 1][a][b][last] + val);
}
}
if (i == n)ans = min({ans, dp[i][a][b][0], dp[i][a][b][1], dp[i][a][b][2]});
}
}
}
if (ans == 1e18) cout << "-1\n";
else cout << ans << '\n';
}
signed main() {
EmirBey;
int tt = 1;
while (tt--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
916 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
720 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
916 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
720 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
3 ms |
7256 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
8024 KB |
Output is correct |
21 |
Correct |
2 ms |
6232 KB |
Output is correct |
22 |
Correct |
3 ms |
7772 KB |
Output is correct |
23 |
Correct |
3 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
3676 KB |
Output is correct |
25 |
Correct |
3 ms |
9904 KB |
Output is correct |
26 |
Correct |
3 ms |
8540 KB |
Output is correct |
27 |
Correct |
3 ms |
8028 KB |
Output is correct |
28 |
Correct |
3 ms |
6492 KB |
Output is correct |
29 |
Correct |
3 ms |
7004 KB |
Output is correct |
30 |
Correct |
2 ms |
5532 KB |
Output is correct |
31 |
Correct |
2 ms |
5108 KB |
Output is correct |
32 |
Correct |
3 ms |
5724 KB |
Output is correct |
33 |
Correct |
3 ms |
7004 KB |
Output is correct |
34 |
Correct |
3 ms |
6236 KB |
Output is correct |
35 |
Correct |
3 ms |
6688 KB |
Output is correct |
36 |
Correct |
3 ms |
6492 KB |
Output is correct |
37 |
Correct |
2 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
4700 KB |
Output is correct |
39 |
Correct |
3 ms |
5980 KB |
Output is correct |
40 |
Correct |
0 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
3164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
107 ms |
327000 KB |
Output is correct |
3 |
Correct |
106 ms |
326164 KB |
Output is correct |
4 |
Correct |
108 ms |
326992 KB |
Output is correct |
5 |
Correct |
109 ms |
326992 KB |
Output is correct |
6 |
Correct |
115 ms |
327264 KB |
Output is correct |
7 |
Correct |
104 ms |
324692 KB |
Output is correct |
8 |
Correct |
114 ms |
324704 KB |
Output is correct |
9 |
Correct |
104 ms |
323920 KB |
Output is correct |
10 |
Correct |
106 ms |
326992 KB |
Output is correct |
11 |
Correct |
117 ms |
327032 KB |
Output is correct |
12 |
Correct |
32 ms |
87944 KB |
Output is correct |
13 |
Correct |
55 ms |
154544 KB |
Output is correct |
14 |
Correct |
74 ms |
223312 KB |
Output is correct |
15 |
Correct |
119 ms |
325456 KB |
Output is correct |
16 |
Correct |
112 ms |
325460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
916 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
0 ms |
860 KB |
Output is correct |
12 |
Correct |
0 ms |
860 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
720 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
3 ms |
7256 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
8024 KB |
Output is correct |
21 |
Correct |
2 ms |
6232 KB |
Output is correct |
22 |
Correct |
3 ms |
7772 KB |
Output is correct |
23 |
Correct |
3 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
3676 KB |
Output is correct |
25 |
Correct |
3 ms |
9904 KB |
Output is correct |
26 |
Correct |
3 ms |
8540 KB |
Output is correct |
27 |
Correct |
3 ms |
8028 KB |
Output is correct |
28 |
Correct |
3 ms |
6492 KB |
Output is correct |
29 |
Correct |
3 ms |
7004 KB |
Output is correct |
30 |
Correct |
2 ms |
5532 KB |
Output is correct |
31 |
Correct |
2 ms |
5108 KB |
Output is correct |
32 |
Correct |
3 ms |
5724 KB |
Output is correct |
33 |
Correct |
3 ms |
7004 KB |
Output is correct |
34 |
Correct |
3 ms |
6236 KB |
Output is correct |
35 |
Correct |
3 ms |
6688 KB |
Output is correct |
36 |
Correct |
3 ms |
6492 KB |
Output is correct |
37 |
Correct |
2 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
4700 KB |
Output is correct |
39 |
Correct |
3 ms |
5980 KB |
Output is correct |
40 |
Correct |
0 ms |
860 KB |
Output is correct |
41 |
Correct |
1 ms |
3164 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
107 ms |
327000 KB |
Output is correct |
44 |
Correct |
106 ms |
326164 KB |
Output is correct |
45 |
Correct |
108 ms |
326992 KB |
Output is correct |
46 |
Correct |
109 ms |
326992 KB |
Output is correct |
47 |
Correct |
115 ms |
327264 KB |
Output is correct |
48 |
Correct |
104 ms |
324692 KB |
Output is correct |
49 |
Correct |
114 ms |
324704 KB |
Output is correct |
50 |
Correct |
104 ms |
323920 KB |
Output is correct |
51 |
Correct |
106 ms |
326992 KB |
Output is correct |
52 |
Correct |
117 ms |
327032 KB |
Output is correct |
53 |
Correct |
32 ms |
87944 KB |
Output is correct |
54 |
Correct |
55 ms |
154544 KB |
Output is correct |
55 |
Correct |
74 ms |
223312 KB |
Output is correct |
56 |
Correct |
119 ms |
325456 KB |
Output is correct |
57 |
Correct |
112 ms |
325460 KB |
Output is correct |
58 |
Correct |
174 ms |
377432 KB |
Output is correct |
59 |
Correct |
182 ms |
397260 KB |
Output is correct |
60 |
Correct |
181 ms |
404816 KB |
Output is correct |
61 |
Correct |
175 ms |
387660 KB |
Output is correct |
62 |
Correct |
134 ms |
357716 KB |
Output is correct |
63 |
Correct |
118 ms |
333652 KB |
Output is correct |
64 |
Correct |
157 ms |
366160 KB |
Output is correct |
65 |
Correct |
158 ms |
345760 KB |
Output is correct |
66 |
Correct |
177 ms |
389308 KB |
Output is correct |
67 |
Correct |
157 ms |
329296 KB |
Output is correct |
68 |
Correct |
172 ms |
388728 KB |
Output is correct |
69 |
Correct |
172 ms |
397392 KB |
Output is correct |
70 |
Correct |
168 ms |
380496 KB |
Output is correct |
71 |
Correct |
187 ms |
438100 KB |
Output is correct |
72 |
Correct |
229 ms |
689552 KB |
Output is correct |
73 |
Correct |
11 ms |
21592 KB |
Output is correct |