#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template<class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) cerr << *i << " "; cerr << "]\n";}
void _print() {cerr << "]\n";}
template<class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = [", _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
const int mx = 405;
int dp[mx][mx][mx][3], pref[mx][3], pos[3][mx], ct[3];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n; string s;
cin >> n >> s;
for (int q = 1; q <= n; q++) {
for (int w = 0; w < 3; w++) pref[q][w] = pref[q-1][w];
if (s[q-1] == 'R') {
pref[q][0]++;
ct[0]++;
pos[0][ct[0]] = q;
} else if (s[q-1] == 'G') {
pref[q][1]++;
ct[1]++;
pos[1][ct[1]] = q;
} else {
pref[q][2]++;
ct[2]++;
pos[2][ct[2]] = q;
}
}
int inf = 1e8;
for (int q = 0; q <= ct[0]; q++) {
for (int w = 0; w <= ct[1]; w++) {
for (int e = 0; e <= ct[2]; e++) {
for (int r = 0; r < 3; r++) {
dp[q][w][e][r] = inf;
}
}
}
}
for (int q = 0; q < 3; q++) dp[0][0][0][q] = 0;
for (int q = 0; q <= ct[0]; q++) {
for (int w = 0; w <= ct[1]; w++) {
for (int e = 0; e <= ct[2]; e++) {
for (int r = 0; r < 3; r++) {
if (r == 0 && q == 0) continue;
if (r == 1 && w == 0) continue;
if (r == 2 && e == 0) continue;
int tot = 0;
if (r == 0) {
tot += max(0, pref[pos[0][q]][1]-pref[pos[1][w]][1]);
tot += max(0, pref[pos[0][q]][2]-pref[pos[2][e]][2]);
tot += min(dp[q-1][w][e][1], dp[q-1][w][e][2]);
} else if (r == 1) {
tot += max(0, pref[pos[1][w]][0]-pref[pos[0][q]][0]);
tot += max(0, pref[pos[1][w]][2]-pref[pos[2][e]][2]);
tot += min(dp[q][w-1][e][0], dp[q][w-1][e][2]);
} else {
tot += max(0, pref[pos[2][e]][0]-pref[pos[0][q]][0]);
tot += max(0, pref[pos[2][e]][1]-pref[pos[1][w]][1]);
tot += min(dp[q][w][e-1][0], dp[q][w][e-1][1]);
}
dp[q][w][e][r] = min(dp[q][w][e][r], tot);
}
}
}
}
int ans = inf;
for (int q = 0; q < 3; q++) ans = min(ans, dp[ct[0]][ct[1]][ct[2]][q]);
if (ans == inf) cout << "-1\n";
else cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4044 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2256 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
2 ms |
2256 KB |
Output is correct |
31 |
Correct |
2 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3788 KB |
Output is correct |
34 |
Correct |
2 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2632 KB |
Output is correct |
36 |
Correct |
2 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2244 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
71 ms |
162912 KB |
Output is correct |
3 |
Correct |
69 ms |
162116 KB |
Output is correct |
4 |
Correct |
70 ms |
162908 KB |
Output is correct |
5 |
Correct |
70 ms |
162952 KB |
Output is correct |
6 |
Correct |
68 ms |
162884 KB |
Output is correct |
7 |
Correct |
71 ms |
162108 KB |
Output is correct |
8 |
Correct |
70 ms |
162116 KB |
Output is correct |
9 |
Correct |
70 ms |
161296 KB |
Output is correct |
10 |
Correct |
70 ms |
162960 KB |
Output is correct |
11 |
Correct |
72 ms |
162868 KB |
Output is correct |
12 |
Correct |
22 ms |
44080 KB |
Output is correct |
13 |
Correct |
36 ms |
77108 KB |
Output is correct |
14 |
Correct |
50 ms |
111300 KB |
Output is correct |
15 |
Correct |
71 ms |
162912 KB |
Output is correct |
16 |
Correct |
72 ms |
162988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
2252 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
1 ms |
1612 KB |
Output is correct |
25 |
Correct |
2 ms |
3916 KB |
Output is correct |
26 |
Correct |
2 ms |
4044 KB |
Output is correct |
27 |
Correct |
2 ms |
3020 KB |
Output is correct |
28 |
Correct |
2 ms |
2256 KB |
Output is correct |
29 |
Correct |
2 ms |
2252 KB |
Output is correct |
30 |
Correct |
2 ms |
2256 KB |
Output is correct |
31 |
Correct |
2 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
2380 KB |
Output is correct |
33 |
Correct |
2 ms |
3788 KB |
Output is correct |
34 |
Correct |
2 ms |
3532 KB |
Output is correct |
35 |
Correct |
2 ms |
2632 KB |
Output is correct |
36 |
Correct |
2 ms |
1868 KB |
Output is correct |
37 |
Correct |
1 ms |
1740 KB |
Output is correct |
38 |
Correct |
2 ms |
2124 KB |
Output is correct |
39 |
Correct |
2 ms |
2244 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
2124 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
71 ms |
162912 KB |
Output is correct |
44 |
Correct |
69 ms |
162116 KB |
Output is correct |
45 |
Correct |
70 ms |
162908 KB |
Output is correct |
46 |
Correct |
70 ms |
162952 KB |
Output is correct |
47 |
Correct |
68 ms |
162884 KB |
Output is correct |
48 |
Correct |
71 ms |
162108 KB |
Output is correct |
49 |
Correct |
70 ms |
162116 KB |
Output is correct |
50 |
Correct |
70 ms |
161296 KB |
Output is correct |
51 |
Correct |
70 ms |
162960 KB |
Output is correct |
52 |
Correct |
72 ms |
162868 KB |
Output is correct |
53 |
Correct |
22 ms |
44080 KB |
Output is correct |
54 |
Correct |
36 ms |
77108 KB |
Output is correct |
55 |
Correct |
50 ms |
111300 KB |
Output is correct |
56 |
Correct |
71 ms |
162912 KB |
Output is correct |
57 |
Correct |
72 ms |
162988 KB |
Output is correct |
58 |
Correct |
78 ms |
76228 KB |
Output is correct |
59 |
Correct |
86 ms |
96584 KB |
Output is correct |
60 |
Correct |
85 ms |
87740 KB |
Output is correct |
61 |
Correct |
82 ms |
84804 KB |
Output is correct |
62 |
Correct |
76 ms |
160600 KB |
Output is correct |
63 |
Correct |
80 ms |
160124 KB |
Output is correct |
64 |
Correct |
91 ms |
150212 KB |
Output is correct |
65 |
Correct |
93 ms |
138508 KB |
Output is correct |
66 |
Correct |
86 ms |
86020 KB |
Output is correct |
67 |
Correct |
80 ms |
82824 KB |
Output is correct |
68 |
Correct |
84 ms |
88668 KB |
Output is correct |
69 |
Correct |
83 ms |
85508 KB |
Output is correct |
70 |
Correct |
85 ms |
92576 KB |
Output is correct |
71 |
Correct |
82 ms |
83972 KB |
Output is correct |
72 |
Correct |
32 ms |
41508 KB |
Output is correct |
73 |
Correct |
4 ms |
2892 KB |
Output is correct |