#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 4e2 + 5, lg = 22, lng = 26, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
vector<int> pos[3];
int dp[maxn][maxn][maxn][3];
int ptr[maxn][3], a[maxn];
int main(){
cin.tie(0) -> sync_with_stdio(0);
int n; cin >> n;
string s; cin >> s;
rep(i,0,n){
int t = 0;
if(s[i] == 'G') t = 1;
if(s[i] == 'Y') t = 2;
rep(j,0,3) ptr[i][j] = sz(pos[j]);
pos[t].pb(i), a[i] = t;
}
auto clc_pos = [&](int a, int b, int c, int pos){
return pos + max(0, a - ptr[pos][0]) + max(0, b - ptr[pos][1]) + max(0, c - ptr[pos][2]);
};
int A = sz(pos[0]), B = sz(pos[1]), C = sz(pos[2]);
rep(i,0,A+1) rep(j,0,B+1) rep(k,0,C+1) rep(l,0,3) dp[i][j][k][l] = inf;
if(A) dp[1][0][0][0] = pos[0][0];
if(B) dp[0][1][0][1] = pos[1][0];
if(C) dp[0][0][1][2] = pos[2][0];
rep(p,1,n){
rep(i,0,min(p,A)+1){
rep(j,0,min(p,B)+1){
int k = p - i - j;
if(k < 0) break;
if(k > C) continue;
rep(l,0,3){
if(i < A && l) dp[i+1][j][k][0] = min(dp[i+1][j][k][0], dp[i][j][k][l] + clc_pos(i, j, k, pos[0][i]) - p);
if(j < B && l - 1) dp[i][j+1][k][1] = min(dp[i][j+1][k][1], dp[i][j][k][l] + clc_pos(i, j, k, pos[1][j]) - p);
if(k < C && l - 2) dp[i][j][k+1][2] = min(dp[i][j][k+1][2], dp[i][j][k][l] + clc_pos(i, j, k, pos[2][k]) - p);
}
}
}
}
int ans = inf;
rep(i,0,3) ans = min(ans, dp[A][B][C][i]);
cout << (ans == inf? -1: ans) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
432 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
432 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1624 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
1 ms |
3032 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2264 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2392 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1744 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
71 ms |
162880 KB |
Output is correct |
3 |
Correct |
69 ms |
162232 KB |
Output is correct |
4 |
Correct |
69 ms |
162892 KB |
Output is correct |
5 |
Correct |
74 ms |
162876 KB |
Output is correct |
6 |
Correct |
71 ms |
163024 KB |
Output is correct |
7 |
Correct |
70 ms |
162196 KB |
Output is correct |
8 |
Correct |
70 ms |
162124 KB |
Output is correct |
9 |
Correct |
70 ms |
161384 KB |
Output is correct |
10 |
Correct |
70 ms |
162988 KB |
Output is correct |
11 |
Correct |
70 ms |
162892 KB |
Output is correct |
12 |
Correct |
19 ms |
43988 KB |
Output is correct |
13 |
Correct |
31 ms |
77132 KB |
Output is correct |
14 |
Correct |
49 ms |
111268 KB |
Output is correct |
15 |
Correct |
73 ms |
162956 KB |
Output is correct |
16 |
Correct |
71 ms |
162884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
432 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1624 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
1 ms |
3032 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2264 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
2 ms |
2392 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1744 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
2 ms |
2260 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
71 ms |
162880 KB |
Output is correct |
44 |
Correct |
69 ms |
162232 KB |
Output is correct |
45 |
Correct |
69 ms |
162892 KB |
Output is correct |
46 |
Correct |
74 ms |
162876 KB |
Output is correct |
47 |
Correct |
71 ms |
163024 KB |
Output is correct |
48 |
Correct |
70 ms |
162196 KB |
Output is correct |
49 |
Correct |
70 ms |
162124 KB |
Output is correct |
50 |
Correct |
70 ms |
161384 KB |
Output is correct |
51 |
Correct |
70 ms |
162988 KB |
Output is correct |
52 |
Correct |
70 ms |
162892 KB |
Output is correct |
53 |
Correct |
19 ms |
43988 KB |
Output is correct |
54 |
Correct |
31 ms |
77132 KB |
Output is correct |
55 |
Correct |
49 ms |
111268 KB |
Output is correct |
56 |
Correct |
73 ms |
162956 KB |
Output is correct |
57 |
Correct |
71 ms |
162884 KB |
Output is correct |
58 |
Correct |
91 ms |
76244 KB |
Output is correct |
59 |
Correct |
110 ms |
96620 KB |
Output is correct |
60 |
Correct |
96 ms |
87788 KB |
Output is correct |
61 |
Correct |
95 ms |
84888 KB |
Output is correct |
62 |
Correct |
70 ms |
160628 KB |
Output is correct |
63 |
Correct |
73 ms |
160132 KB |
Output is correct |
64 |
Correct |
91 ms |
150140 KB |
Output is correct |
65 |
Correct |
101 ms |
138540 KB |
Output is correct |
66 |
Correct |
96 ms |
86036 KB |
Output is correct |
67 |
Correct |
99 ms |
82796 KB |
Output is correct |
68 |
Correct |
96 ms |
88616 KB |
Output is correct |
69 |
Correct |
95 ms |
85452 KB |
Output is correct |
70 |
Correct |
98 ms |
92488 KB |
Output is correct |
71 |
Correct |
93 ms |
83904 KB |
Output is correct |
72 |
Correct |
36 ms |
41556 KB |
Output is correct |
73 |
Correct |
5 ms |
2900 KB |
Output is correct |