#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;
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
38732 KB |
Output is correct |
2 |
Correct |
19 ms |
38828 KB |
Output is correct |
3 |
Correct |
20 ms |
38732 KB |
Output is correct |
4 |
Correct |
21 ms |
38804 KB |
Output is correct |
5 |
Correct |
20 ms |
38768 KB |
Output is correct |
6 |
Correct |
20 ms |
38756 KB |
Output is correct |
7 |
Correct |
21 ms |
38860 KB |
Output is correct |
8 |
Correct |
21 ms |
38848 KB |
Output is correct |
9 |
Correct |
20 ms |
38828 KB |
Output is correct |
10 |
Correct |
20 ms |
38836 KB |
Output is correct |
11 |
Correct |
20 ms |
38832 KB |
Output is correct |
12 |
Correct |
20 ms |
38836 KB |
Output is correct |
13 |
Correct |
21 ms |
38788 KB |
Output is correct |
14 |
Correct |
20 ms |
38860 KB |
Output is correct |
15 |
Correct |
22 ms |
38768 KB |
Output is correct |
16 |
Correct |
19 ms |
38832 KB |
Output is correct |
17 |
Correct |
20 ms |
38832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
38732 KB |
Output is correct |
2 |
Correct |
19 ms |
38828 KB |
Output is correct |
3 |
Correct |
20 ms |
38732 KB |
Output is correct |
4 |
Correct |
21 ms |
38804 KB |
Output is correct |
5 |
Correct |
20 ms |
38768 KB |
Output is correct |
6 |
Correct |
20 ms |
38756 KB |
Output is correct |
7 |
Correct |
21 ms |
38860 KB |
Output is correct |
8 |
Correct |
21 ms |
38848 KB |
Output is correct |
9 |
Correct |
20 ms |
38828 KB |
Output is correct |
10 |
Correct |
20 ms |
38836 KB |
Output is correct |
11 |
Correct |
20 ms |
38832 KB |
Output is correct |
12 |
Correct |
20 ms |
38836 KB |
Output is correct |
13 |
Correct |
21 ms |
38788 KB |
Output is correct |
14 |
Correct |
20 ms |
38860 KB |
Output is correct |
15 |
Correct |
22 ms |
38768 KB |
Output is correct |
16 |
Correct |
19 ms |
38832 KB |
Output is correct |
17 |
Correct |
20 ms |
38832 KB |
Output is correct |
18 |
Correct |
24 ms |
40572 KB |
Output is correct |
19 |
Correct |
23 ms |
40500 KB |
Output is correct |
20 |
Correct |
24 ms |
40504 KB |
Output is correct |
21 |
Correct |
24 ms |
40524 KB |
Output is correct |
22 |
Correct |
25 ms |
40244 KB |
Output is correct |
23 |
Correct |
23 ms |
40568 KB |
Output is correct |
24 |
Correct |
22 ms |
40168 KB |
Output is correct |
25 |
Correct |
20 ms |
39108 KB |
Output is correct |
26 |
Correct |
21 ms |
39116 KB |
Output is correct |
27 |
Correct |
22 ms |
40292 KB |
Output is correct |
28 |
Correct |
23 ms |
40636 KB |
Output is correct |
29 |
Correct |
25 ms |
40556 KB |
Output is correct |
30 |
Correct |
23 ms |
40524 KB |
Output is correct |
31 |
Correct |
23 ms |
40512 KB |
Output is correct |
32 |
Correct |
24 ms |
40604 KB |
Output is correct |
33 |
Correct |
21 ms |
39240 KB |
Output is correct |
34 |
Correct |
21 ms |
39568 KB |
Output is correct |
35 |
Correct |
23 ms |
40248 KB |
Output is correct |
36 |
Correct |
26 ms |
40448 KB |
Output is correct |
37 |
Correct |
23 ms |
40124 KB |
Output is correct |
38 |
Correct |
27 ms |
40380 KB |
Output is correct |
39 |
Correct |
25 ms |
40524 KB |
Output is correct |
40 |
Correct |
20 ms |
38988 KB |
Output is correct |
41 |
Correct |
21 ms |
39424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
38732 KB |
Output is correct |
2 |
Correct |
30 ms |
46448 KB |
Output is correct |
3 |
Correct |
30 ms |
46396 KB |
Output is correct |
4 |
Correct |
36 ms |
46436 KB |
Output is correct |
5 |
Correct |
31 ms |
46428 KB |
Output is correct |
6 |
Correct |
30 ms |
46388 KB |
Output is correct |
7 |
Correct |
30 ms |
46292 KB |
Output is correct |
8 |
Correct |
32 ms |
46272 KB |
Output is correct |
9 |
Correct |
30 ms |
46296 KB |
Output is correct |
10 |
Correct |
31 ms |
46500 KB |
Output is correct |
11 |
Correct |
30 ms |
46420 KB |
Output is correct |
12 |
Correct |
23 ms |
40848 KB |
Output is correct |
13 |
Correct |
23 ms |
42416 KB |
Output is correct |
14 |
Correct |
27 ms |
43980 KB |
Output is correct |
15 |
Correct |
32 ms |
46352 KB |
Output is correct |
16 |
Correct |
30 ms |
46384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
38732 KB |
Output is correct |
2 |
Correct |
19 ms |
38828 KB |
Output is correct |
3 |
Correct |
20 ms |
38732 KB |
Output is correct |
4 |
Correct |
21 ms |
38804 KB |
Output is correct |
5 |
Correct |
20 ms |
38768 KB |
Output is correct |
6 |
Correct |
20 ms |
38756 KB |
Output is correct |
7 |
Correct |
21 ms |
38860 KB |
Output is correct |
8 |
Correct |
21 ms |
38848 KB |
Output is correct |
9 |
Correct |
20 ms |
38828 KB |
Output is correct |
10 |
Correct |
20 ms |
38836 KB |
Output is correct |
11 |
Correct |
20 ms |
38832 KB |
Output is correct |
12 |
Correct |
20 ms |
38836 KB |
Output is correct |
13 |
Correct |
21 ms |
38788 KB |
Output is correct |
14 |
Correct |
20 ms |
38860 KB |
Output is correct |
15 |
Correct |
22 ms |
38768 KB |
Output is correct |
16 |
Correct |
19 ms |
38832 KB |
Output is correct |
17 |
Correct |
20 ms |
38832 KB |
Output is correct |
18 |
Correct |
24 ms |
40572 KB |
Output is correct |
19 |
Correct |
23 ms |
40500 KB |
Output is correct |
20 |
Correct |
24 ms |
40504 KB |
Output is correct |
21 |
Correct |
24 ms |
40524 KB |
Output is correct |
22 |
Correct |
25 ms |
40244 KB |
Output is correct |
23 |
Correct |
23 ms |
40568 KB |
Output is correct |
24 |
Correct |
22 ms |
40168 KB |
Output is correct |
25 |
Correct |
20 ms |
39108 KB |
Output is correct |
26 |
Correct |
21 ms |
39116 KB |
Output is correct |
27 |
Correct |
22 ms |
40292 KB |
Output is correct |
28 |
Correct |
23 ms |
40636 KB |
Output is correct |
29 |
Correct |
25 ms |
40556 KB |
Output is correct |
30 |
Correct |
23 ms |
40524 KB |
Output is correct |
31 |
Correct |
23 ms |
40512 KB |
Output is correct |
32 |
Correct |
24 ms |
40604 KB |
Output is correct |
33 |
Correct |
21 ms |
39240 KB |
Output is correct |
34 |
Correct |
21 ms |
39568 KB |
Output is correct |
35 |
Correct |
23 ms |
40248 KB |
Output is correct |
36 |
Correct |
26 ms |
40448 KB |
Output is correct |
37 |
Correct |
23 ms |
40124 KB |
Output is correct |
38 |
Correct |
27 ms |
40380 KB |
Output is correct |
39 |
Correct |
25 ms |
40524 KB |
Output is correct |
40 |
Correct |
20 ms |
38988 KB |
Output is correct |
41 |
Correct |
21 ms |
39424 KB |
Output is correct |
42 |
Correct |
19 ms |
38732 KB |
Output is correct |
43 |
Correct |
30 ms |
46448 KB |
Output is correct |
44 |
Correct |
30 ms |
46396 KB |
Output is correct |
45 |
Correct |
36 ms |
46436 KB |
Output is correct |
46 |
Correct |
31 ms |
46428 KB |
Output is correct |
47 |
Correct |
30 ms |
46388 KB |
Output is correct |
48 |
Correct |
30 ms |
46292 KB |
Output is correct |
49 |
Correct |
32 ms |
46272 KB |
Output is correct |
50 |
Correct |
30 ms |
46296 KB |
Output is correct |
51 |
Correct |
31 ms |
46500 KB |
Output is correct |
52 |
Correct |
30 ms |
46420 KB |
Output is correct |
53 |
Correct |
23 ms |
40848 KB |
Output is correct |
54 |
Correct |
23 ms |
42416 KB |
Output is correct |
55 |
Correct |
27 ms |
43980 KB |
Output is correct |
56 |
Correct |
32 ms |
46352 KB |
Output is correct |
57 |
Correct |
30 ms |
46384 KB |
Output is correct |
58 |
Execution timed out |
1120 ms |
489080 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |