#include<bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false);cin.tie(0);
#define all(x) x.begin(),x.end()
const int INF=1e9;
const int mod=998244353;
const int N=405;
int dp[3][405][405][405]{};
int32_t main(){
LCBorz;
int n;cin>>n;
string s;cin>>s;
int r=0,g=0,y=0;
vector<pair<int,int>> r1,g1,y1;
for(int i=0;i<n;i++){
if(s[i]=='R'){
r1.push_back({g,y});
r++;
}
if(s[i]=='G'){
g1.push_back({y,r});
g++;
}
if(s[i]=='Y'){
y1.push_back({r,g});
y++;
}
}
r1.push_back({0,0});
g1.push_back({0,0});
y1.push_back({0,0});
for(int i=0;i<3;i++){
for(int j=0;j<N;j++){
for(int k=0;k<N;k++){
for(int p=0;p<N;p++){
dp[i][j][k][p]=INF;
}
}
}
}
dp[0][0][0][0]=0;
dp[1][0][0][0]=0;
dp[2][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++){
//cout<<i<<' '<<j<<' '<<k<<' '<<endl;
int r2=max((int)0,r1[i].first-j)+max((int)0,r1[i].second-k);
int g2=max((int)0,g1[j].first-k)+max((int)0,g1[j].second-i);
int y2=max((int)0,y1[k].first-i)+max((int)0,y1[k].second-j);
dp[0][i+1][j][k]=min(dp[0][i+1][j][k],min(dp[1][i][j][k],dp[2][i][j][k])+r2);
dp[1][i][j+1][k]=min(dp[1][i][j+1][k],min(dp[2][i][j][k],dp[0][i][j][k])+g2);
dp[2][i][j][k+1]=min(dp[2][i][j][k+1],min(dp[0][i][j][k],dp[1][i][j][k])+y2);
}
}
}
int ans=min({dp[0][r][g][y],dp[1][r][g][y],dp[2][r][g][y]});
cout<<(ans==INF?-1:ans)<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
780248 KB |
Output is correct |
2 |
Correct |
276 ms |
780428 KB |
Output is correct |
3 |
Correct |
260 ms |
780364 KB |
Output is correct |
4 |
Correct |
261 ms |
780380 KB |
Output is correct |
5 |
Correct |
271 ms |
780368 KB |
Output is correct |
6 |
Correct |
259 ms |
780284 KB |
Output is correct |
7 |
Correct |
266 ms |
780388 KB |
Output is correct |
8 |
Correct |
264 ms |
780372 KB |
Output is correct |
9 |
Correct |
261 ms |
780468 KB |
Output is correct |
10 |
Correct |
265 ms |
780544 KB |
Output is correct |
11 |
Correct |
262 ms |
780372 KB |
Output is correct |
12 |
Correct |
283 ms |
780416 KB |
Output is correct |
13 |
Correct |
285 ms |
780420 KB |
Output is correct |
14 |
Correct |
261 ms |
780260 KB |
Output is correct |
15 |
Correct |
259 ms |
780380 KB |
Output is correct |
16 |
Correct |
263 ms |
780372 KB |
Output is correct |
17 |
Correct |
264 ms |
780444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
780248 KB |
Output is correct |
2 |
Correct |
276 ms |
780428 KB |
Output is correct |
3 |
Correct |
260 ms |
780364 KB |
Output is correct |
4 |
Correct |
261 ms |
780380 KB |
Output is correct |
5 |
Correct |
271 ms |
780368 KB |
Output is correct |
6 |
Correct |
259 ms |
780284 KB |
Output is correct |
7 |
Correct |
266 ms |
780388 KB |
Output is correct |
8 |
Correct |
264 ms |
780372 KB |
Output is correct |
9 |
Correct |
261 ms |
780468 KB |
Output is correct |
10 |
Correct |
265 ms |
780544 KB |
Output is correct |
11 |
Correct |
262 ms |
780372 KB |
Output is correct |
12 |
Correct |
283 ms |
780416 KB |
Output is correct |
13 |
Correct |
285 ms |
780420 KB |
Output is correct |
14 |
Correct |
261 ms |
780260 KB |
Output is correct |
15 |
Correct |
259 ms |
780380 KB |
Output is correct |
16 |
Correct |
263 ms |
780372 KB |
Output is correct |
17 |
Correct |
264 ms |
780444 KB |
Output is correct |
18 |
Correct |
283 ms |
780416 KB |
Output is correct |
19 |
Correct |
262 ms |
780260 KB |
Output is correct |
20 |
Correct |
259 ms |
780368 KB |
Output is correct |
21 |
Correct |
261 ms |
780368 KB |
Output is correct |
22 |
Correct |
266 ms |
780408 KB |
Output is correct |
23 |
Correct |
261 ms |
780372 KB |
Output is correct |
24 |
Correct |
258 ms |
780368 KB |
Output is correct |
25 |
Correct |
262 ms |
780368 KB |
Output is correct |
26 |
Correct |
257 ms |
780368 KB |
Output is correct |
27 |
Correct |
271 ms |
780564 KB |
Output is correct |
28 |
Correct |
259 ms |
780368 KB |
Output is correct |
29 |
Correct |
265 ms |
780464 KB |
Output is correct |
30 |
Correct |
260 ms |
780368 KB |
Output is correct |
31 |
Correct |
263 ms |
780372 KB |
Output is correct |
32 |
Correct |
264 ms |
780256 KB |
Output is correct |
33 |
Correct |
262 ms |
780436 KB |
Output is correct |
34 |
Correct |
286 ms |
780388 KB |
Output is correct |
35 |
Correct |
262 ms |
780320 KB |
Output is correct |
36 |
Correct |
265 ms |
780372 KB |
Output is correct |
37 |
Correct |
267 ms |
780360 KB |
Output is correct |
38 |
Correct |
261 ms |
780372 KB |
Output is correct |
39 |
Correct |
260 ms |
780372 KB |
Output is correct |
40 |
Correct |
259 ms |
780372 KB |
Output is correct |
41 |
Correct |
267 ms |
780372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
290 ms |
780220 KB |
Output is correct |
2 |
Correct |
268 ms |
780572 KB |
Output is correct |
3 |
Correct |
262 ms |
780268 KB |
Output is correct |
4 |
Correct |
275 ms |
780368 KB |
Output is correct |
5 |
Correct |
266 ms |
780372 KB |
Output is correct |
6 |
Correct |
272 ms |
780372 KB |
Output is correct |
7 |
Correct |
263 ms |
780372 KB |
Output is correct |
8 |
Correct |
306 ms |
780436 KB |
Output is correct |
9 |
Correct |
261 ms |
780388 KB |
Output is correct |
10 |
Correct |
277 ms |
780352 KB |
Output is correct |
11 |
Correct |
268 ms |
780380 KB |
Output is correct |
12 |
Correct |
267 ms |
780372 KB |
Output is correct |
13 |
Correct |
262 ms |
780644 KB |
Output is correct |
14 |
Correct |
277 ms |
780212 KB |
Output is correct |
15 |
Correct |
263 ms |
780408 KB |
Output is correct |
16 |
Correct |
286 ms |
780368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
780248 KB |
Output is correct |
2 |
Correct |
276 ms |
780428 KB |
Output is correct |
3 |
Correct |
260 ms |
780364 KB |
Output is correct |
4 |
Correct |
261 ms |
780380 KB |
Output is correct |
5 |
Correct |
271 ms |
780368 KB |
Output is correct |
6 |
Correct |
259 ms |
780284 KB |
Output is correct |
7 |
Correct |
266 ms |
780388 KB |
Output is correct |
8 |
Correct |
264 ms |
780372 KB |
Output is correct |
9 |
Correct |
261 ms |
780468 KB |
Output is correct |
10 |
Correct |
265 ms |
780544 KB |
Output is correct |
11 |
Correct |
262 ms |
780372 KB |
Output is correct |
12 |
Correct |
283 ms |
780416 KB |
Output is correct |
13 |
Correct |
285 ms |
780420 KB |
Output is correct |
14 |
Correct |
261 ms |
780260 KB |
Output is correct |
15 |
Correct |
259 ms |
780380 KB |
Output is correct |
16 |
Correct |
263 ms |
780372 KB |
Output is correct |
17 |
Correct |
264 ms |
780444 KB |
Output is correct |
18 |
Correct |
283 ms |
780416 KB |
Output is correct |
19 |
Correct |
262 ms |
780260 KB |
Output is correct |
20 |
Correct |
259 ms |
780368 KB |
Output is correct |
21 |
Correct |
261 ms |
780368 KB |
Output is correct |
22 |
Correct |
266 ms |
780408 KB |
Output is correct |
23 |
Correct |
261 ms |
780372 KB |
Output is correct |
24 |
Correct |
258 ms |
780368 KB |
Output is correct |
25 |
Correct |
262 ms |
780368 KB |
Output is correct |
26 |
Correct |
257 ms |
780368 KB |
Output is correct |
27 |
Correct |
271 ms |
780564 KB |
Output is correct |
28 |
Correct |
259 ms |
780368 KB |
Output is correct |
29 |
Correct |
265 ms |
780464 KB |
Output is correct |
30 |
Correct |
260 ms |
780368 KB |
Output is correct |
31 |
Correct |
263 ms |
780372 KB |
Output is correct |
32 |
Correct |
264 ms |
780256 KB |
Output is correct |
33 |
Correct |
262 ms |
780436 KB |
Output is correct |
34 |
Correct |
286 ms |
780388 KB |
Output is correct |
35 |
Correct |
262 ms |
780320 KB |
Output is correct |
36 |
Correct |
265 ms |
780372 KB |
Output is correct |
37 |
Correct |
267 ms |
780360 KB |
Output is correct |
38 |
Correct |
261 ms |
780372 KB |
Output is correct |
39 |
Correct |
260 ms |
780372 KB |
Output is correct |
40 |
Correct |
259 ms |
780372 KB |
Output is correct |
41 |
Correct |
267 ms |
780372 KB |
Output is correct |
42 |
Correct |
290 ms |
780220 KB |
Output is correct |
43 |
Correct |
268 ms |
780572 KB |
Output is correct |
44 |
Correct |
262 ms |
780268 KB |
Output is correct |
45 |
Correct |
275 ms |
780368 KB |
Output is correct |
46 |
Correct |
266 ms |
780372 KB |
Output is correct |
47 |
Correct |
272 ms |
780372 KB |
Output is correct |
48 |
Correct |
263 ms |
780372 KB |
Output is correct |
49 |
Correct |
306 ms |
780436 KB |
Output is correct |
50 |
Correct |
261 ms |
780388 KB |
Output is correct |
51 |
Correct |
277 ms |
780352 KB |
Output is correct |
52 |
Correct |
268 ms |
780380 KB |
Output is correct |
53 |
Correct |
267 ms |
780372 KB |
Output is correct |
54 |
Correct |
262 ms |
780644 KB |
Output is correct |
55 |
Correct |
277 ms |
780212 KB |
Output is correct |
56 |
Correct |
263 ms |
780408 KB |
Output is correct |
57 |
Correct |
286 ms |
780368 KB |
Output is correct |
58 |
Correct |
279 ms |
780368 KB |
Output is correct |
59 |
Correct |
292 ms |
780364 KB |
Output is correct |
60 |
Correct |
301 ms |
780452 KB |
Output is correct |
61 |
Correct |
295 ms |
780372 KB |
Output is correct |
62 |
Correct |
309 ms |
780232 KB |
Output is correct |
63 |
Correct |
273 ms |
780412 KB |
Output is correct |
64 |
Correct |
280 ms |
780204 KB |
Output is correct |
65 |
Correct |
284 ms |
780440 KB |
Output is correct |
66 |
Correct |
292 ms |
780336 KB |
Output is correct |
67 |
Correct |
289 ms |
780380 KB |
Output is correct |
68 |
Correct |
290 ms |
780368 KB |
Output is correct |
69 |
Correct |
300 ms |
780600 KB |
Output is correct |
70 |
Correct |
280 ms |
780412 KB |
Output is correct |
71 |
Correct |
280 ms |
780372 KB |
Output is correct |
72 |
Correct |
267 ms |
780300 KB |
Output is correct |
73 |
Correct |
263 ms |
780200 KB |
Output is correct |