#include <bits/stdc++.h>
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define all(v) v.begin(),v.end()
#define pb push_back
#define sz size()
#define ft first
#define sd second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long ull;
const int N = 2e6 + 5;
const ll M = 1e8;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const double Pi = acos(-1);
ll binpow(ll x, ll ti) { ll res = 1;while (ti){if(ti & 1)res *= x;x *= x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll binmul(ll x, ll ti) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll nok(ll a, ll b) { return (a*b)/__gcd(abs(a),abs(b)) * (a*b > 0 ? 1 : -1); }
bool odd(ll n) { return (n % 2 == 1); }
bool even(ll n) { return (n % 2 == 0); }
ll n, r, g, y, pref[5][405], pos[5][405];
string s;
unordered_map <ll, ll> dp[5][405][405];
const void solve(/*Armashka*/) {
cin >> n >> s;
s = " " + s;
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= 3; ++ j) pref[j][i] = pref[j][i - 1];
if (s[i] == 'R') ++ pref[1][i], pos[1][++ r] = i;
if (s[i] == 'G') ++ pref[2][i], pos[2][++ g] = i;
if (s[i] == 'Y') ++ pref[3][i], pos[3][++ y] = i;
}
for (int i = 0; i <= r; ++ i) {
for (int j = 0; j <= g; ++ j) {
for (int k = 0; k <= y; ++ k) {
for (int c = 1; c <= 3; ++ c) dp[c][i][j][k] = inf;
}
}
}
dp[1][0][0][0] = dp[2][0][0][0] = dp[3][0][0][0] = 0;
for (int i = 0; i <= r; ++ i) {
for (int j = 0; j <= g; ++ j) {
for (int k = 0; k <= y; ++ k) {
if (i) {
ll x = max(0ll, pref[2][pos[1][i]] - j) + max(0ll, pref[3][pos[1][i]] - k);
dp[1][i][j][k] = min({dp[1][i][j][k], dp[2][i - 1][j][k] + x, dp[3][i - 1][j][k] + x});
}
if (j) {
ll x = max(0ll, pref[1][pos[2][j]] - i) + max(0ll, pref[3][pos[2][j]] - k);
dp[2][i][j][k] = min({dp[2][i][j][k], dp[1][i][j - 1][k] + x, dp[3][i][j - 1][k] + x});
}
if (k) {
ll x = max(0ll, pref[1][pos[3][k]] - i) + max(0ll, pref[2][pos[3][k]] - j);
dp[3][i][j][k] = min({dp[3][i][j][k], dp[1][i][j][k - 1] + x, dp[2][i][j][k - 1] + x});
}
}
}
}
ll ans = min({dp[1][r][g][y], dp[2][r][g][y], dp[3][r][g][y]});
if (ans == inf) ans = -1;
cout << ans << "\n";
}
signed main() {
fast;
//freopen("triangles.in", "r", stdin);
//freopen("triangles.out", "w", stdout);
int tt = 1;
//cin >> tt;
while (tt --) {
solve();
}
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
45196 KB |
Output is correct |
2 |
Correct |
32 ms |
45260 KB |
Output is correct |
3 |
Correct |
31 ms |
45252 KB |
Output is correct |
4 |
Correct |
24 ms |
45260 KB |
Output is correct |
5 |
Correct |
25 ms |
45260 KB |
Output is correct |
6 |
Correct |
24 ms |
45260 KB |
Output is correct |
7 |
Correct |
23 ms |
45280 KB |
Output is correct |
8 |
Correct |
26 ms |
45260 KB |
Output is correct |
9 |
Correct |
24 ms |
45260 KB |
Output is correct |
10 |
Correct |
24 ms |
45232 KB |
Output is correct |
11 |
Correct |
26 ms |
45172 KB |
Output is correct |
12 |
Correct |
24 ms |
45220 KB |
Output is correct |
13 |
Correct |
24 ms |
45260 KB |
Output is correct |
14 |
Correct |
25 ms |
45260 KB |
Output is correct |
15 |
Correct |
25 ms |
45260 KB |
Output is correct |
16 |
Correct |
25 ms |
45212 KB |
Output is correct |
17 |
Correct |
24 ms |
45260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
45196 KB |
Output is correct |
2 |
Correct |
32 ms |
45260 KB |
Output is correct |
3 |
Correct |
31 ms |
45252 KB |
Output is correct |
4 |
Correct |
24 ms |
45260 KB |
Output is correct |
5 |
Correct |
25 ms |
45260 KB |
Output is correct |
6 |
Correct |
24 ms |
45260 KB |
Output is correct |
7 |
Correct |
23 ms |
45280 KB |
Output is correct |
8 |
Correct |
26 ms |
45260 KB |
Output is correct |
9 |
Correct |
24 ms |
45260 KB |
Output is correct |
10 |
Correct |
24 ms |
45232 KB |
Output is correct |
11 |
Correct |
26 ms |
45172 KB |
Output is correct |
12 |
Correct |
24 ms |
45220 KB |
Output is correct |
13 |
Correct |
24 ms |
45260 KB |
Output is correct |
14 |
Correct |
25 ms |
45260 KB |
Output is correct |
15 |
Correct |
25 ms |
45260 KB |
Output is correct |
16 |
Correct |
25 ms |
45212 KB |
Output is correct |
17 |
Correct |
24 ms |
45260 KB |
Output is correct |
18 |
Correct |
31 ms |
46380 KB |
Output is correct |
19 |
Correct |
28 ms |
46380 KB |
Output is correct |
20 |
Correct |
32 ms |
46384 KB |
Output is correct |
21 |
Correct |
29 ms |
46540 KB |
Output is correct |
22 |
Correct |
35 ms |
46132 KB |
Output is correct |
23 |
Correct |
31 ms |
46352 KB |
Output is correct |
24 |
Correct |
28 ms |
46152 KB |
Output is correct |
25 |
Correct |
26 ms |
45680 KB |
Output is correct |
26 |
Correct |
25 ms |
45640 KB |
Output is correct |
27 |
Correct |
27 ms |
46152 KB |
Output is correct |
28 |
Correct |
29 ms |
46476 KB |
Output is correct |
29 |
Correct |
30 ms |
46400 KB |
Output is correct |
30 |
Correct |
28 ms |
46412 KB |
Output is correct |
31 |
Correct |
30 ms |
46280 KB |
Output is correct |
32 |
Correct |
31 ms |
46412 KB |
Output is correct |
33 |
Correct |
25 ms |
45772 KB |
Output is correct |
34 |
Correct |
26 ms |
45832 KB |
Output is correct |
35 |
Correct |
29 ms |
46316 KB |
Output is correct |
36 |
Correct |
28 ms |
46428 KB |
Output is correct |
37 |
Correct |
29 ms |
46148 KB |
Output is correct |
38 |
Correct |
28 ms |
46272 KB |
Output is correct |
39 |
Correct |
30 ms |
46412 KB |
Output is correct |
40 |
Correct |
26 ms |
45368 KB |
Output is correct |
41 |
Correct |
26 ms |
45644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
45200 KB |
Output is correct |
2 |
Correct |
52 ms |
62236 KB |
Output is correct |
3 |
Correct |
51 ms |
62156 KB |
Output is correct |
4 |
Correct |
56 ms |
62288 KB |
Output is correct |
5 |
Correct |
55 ms |
62304 KB |
Output is correct |
6 |
Correct |
62 ms |
62272 KB |
Output is correct |
7 |
Correct |
52 ms |
62248 KB |
Output is correct |
8 |
Correct |
53 ms |
62248 KB |
Output is correct |
9 |
Correct |
53 ms |
62068 KB |
Output is correct |
10 |
Correct |
55 ms |
62348 KB |
Output is correct |
11 |
Correct |
63 ms |
62256 KB |
Output is correct |
12 |
Correct |
33 ms |
49740 KB |
Output is correct |
13 |
Correct |
38 ms |
53296 KB |
Output is correct |
14 |
Correct |
44 ms |
56880 KB |
Output is correct |
15 |
Correct |
53 ms |
62216 KB |
Output is correct |
16 |
Correct |
56 ms |
62336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
45196 KB |
Output is correct |
2 |
Correct |
32 ms |
45260 KB |
Output is correct |
3 |
Correct |
31 ms |
45252 KB |
Output is correct |
4 |
Correct |
24 ms |
45260 KB |
Output is correct |
5 |
Correct |
25 ms |
45260 KB |
Output is correct |
6 |
Correct |
24 ms |
45260 KB |
Output is correct |
7 |
Correct |
23 ms |
45280 KB |
Output is correct |
8 |
Correct |
26 ms |
45260 KB |
Output is correct |
9 |
Correct |
24 ms |
45260 KB |
Output is correct |
10 |
Correct |
24 ms |
45232 KB |
Output is correct |
11 |
Correct |
26 ms |
45172 KB |
Output is correct |
12 |
Correct |
24 ms |
45220 KB |
Output is correct |
13 |
Correct |
24 ms |
45260 KB |
Output is correct |
14 |
Correct |
25 ms |
45260 KB |
Output is correct |
15 |
Correct |
25 ms |
45260 KB |
Output is correct |
16 |
Correct |
25 ms |
45212 KB |
Output is correct |
17 |
Correct |
24 ms |
45260 KB |
Output is correct |
18 |
Correct |
31 ms |
46380 KB |
Output is correct |
19 |
Correct |
28 ms |
46380 KB |
Output is correct |
20 |
Correct |
32 ms |
46384 KB |
Output is correct |
21 |
Correct |
29 ms |
46540 KB |
Output is correct |
22 |
Correct |
35 ms |
46132 KB |
Output is correct |
23 |
Correct |
31 ms |
46352 KB |
Output is correct |
24 |
Correct |
28 ms |
46152 KB |
Output is correct |
25 |
Correct |
26 ms |
45680 KB |
Output is correct |
26 |
Correct |
25 ms |
45640 KB |
Output is correct |
27 |
Correct |
27 ms |
46152 KB |
Output is correct |
28 |
Correct |
29 ms |
46476 KB |
Output is correct |
29 |
Correct |
30 ms |
46400 KB |
Output is correct |
30 |
Correct |
28 ms |
46412 KB |
Output is correct |
31 |
Correct |
30 ms |
46280 KB |
Output is correct |
32 |
Correct |
31 ms |
46412 KB |
Output is correct |
33 |
Correct |
25 ms |
45772 KB |
Output is correct |
34 |
Correct |
26 ms |
45832 KB |
Output is correct |
35 |
Correct |
29 ms |
46316 KB |
Output is correct |
36 |
Correct |
28 ms |
46428 KB |
Output is correct |
37 |
Correct |
29 ms |
46148 KB |
Output is correct |
38 |
Correct |
28 ms |
46272 KB |
Output is correct |
39 |
Correct |
30 ms |
46412 KB |
Output is correct |
40 |
Correct |
26 ms |
45368 KB |
Output is correct |
41 |
Correct |
26 ms |
45644 KB |
Output is correct |
42 |
Correct |
27 ms |
45200 KB |
Output is correct |
43 |
Correct |
52 ms |
62236 KB |
Output is correct |
44 |
Correct |
51 ms |
62156 KB |
Output is correct |
45 |
Correct |
56 ms |
62288 KB |
Output is correct |
46 |
Correct |
55 ms |
62304 KB |
Output is correct |
47 |
Correct |
62 ms |
62272 KB |
Output is correct |
48 |
Correct |
52 ms |
62248 KB |
Output is correct |
49 |
Correct |
53 ms |
62248 KB |
Output is correct |
50 |
Correct |
53 ms |
62068 KB |
Output is correct |
51 |
Correct |
55 ms |
62348 KB |
Output is correct |
52 |
Correct |
63 ms |
62256 KB |
Output is correct |
53 |
Correct |
33 ms |
49740 KB |
Output is correct |
54 |
Correct |
38 ms |
53296 KB |
Output is correct |
55 |
Correct |
44 ms |
56880 KB |
Output is correct |
56 |
Correct |
53 ms |
62216 KB |
Output is correct |
57 |
Correct |
56 ms |
62336 KB |
Output is correct |
58 |
Execution timed out |
951 ms |
366604 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |