#include <bits/stdc++.h>
using namespace std;
typedef long long lo;
typedef pair< lo,lo > PII;
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 400;
const lo mod = 1000000007;
int n,m,k,flag,t,x,siz1,siz2,siz3,dp[li][li][li][4],ind1[3][li],ind2[3][li],ind[3][li];
int cev;
string s;
vector<int> v[3];
inline int in(){
int x;
scanf("%d",&x);
return x;
}
inline int f(int bir,int iki,int uc,int sira,int son){
int cevv=1000000000;
if(bir==siz1 && iki==siz2 && uc==siz3)return 0;
if(~dp[bir][iki][uc][son])return dp[bir][iki][uc][son];
if(bir!=siz1){
if(son!=0)cevv=min(cevv,f(bir+1,iki,uc,sira+1,0)+max(0,(v[0][bir]+max(0,iki-ind[1][bir]-1)+max(0,uc-ind[2][bir]-1))-sira));
}
if(iki!=siz2){
if(son!=1)cevv=min(cevv,f(bir,iki+1,uc,sira+1,1)+max(0,(v[1][iki]+max(0,bir-ind1[0][iki]-1)+max(0,uc-ind1[2][iki]-1))-sira));
}
if(uc!=siz3){
if(son!=2)cevv=min(cevv,f(bir,iki,uc+1,sira+1,2)+max(0,(v[2][uc]+max(0,iki-ind2[1][uc]-1)+max(0,bir-ind2[0][uc]-1))-sira));
}
return dp[bir][iki][uc][son]=cevv;
}
int main(void){
fio();
cin>>n;
cin>>s;
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++){
if(s[i]=='R'){s[i]='a';siz1++;}
if(s[i]=='G'){s[i]='b';siz2++;}
if(s[i]=='Y'){s[i]='c';siz3++;}
v[s[i]-'a'].pb(i);
}
for(int i=0;i<(int)v[0].size();i++){
ind[1][i]=-1;
ind[2][i]=-1;
for(int j=0;j<(int)v[1].size();j++){
if(v[1][j]>v[0][i]){break;}
ind[1][i]=j;
}
for(int j=0;j<(int)v[2].size();j++){
if(v[2][j]>v[0][i]){break;}
ind[2][i]=j;
}
}
for(int i=0;i<(int)v[1].size();i++){
ind1[0][i]=-1;
ind1[2][i]=-1;
for(int j=0;j<(int)v[0].size();j++){
if(v[0][j]>v[1][i]){break;}
ind1[0][i]=j;
}
for(int j=0;j<(int)v[2].size();j++){
if(v[2][j]>v[1][i]){break;}
ind1[2][i]=j;
}
}
for(int i=0;i<(int)v[2].size();i++){
ind2[0][i]=-1;
ind2[1][i]=-1;
for(int j=0;j<(int)v[0].size();j++){
if(v[0][j]>v[2][i]){break;}
ind2[0][i]=j;
}
for(int j=0;j<(int)v[1].size();j++){
if(v[1][j]>v[2][i]){break;}
ind2[1][i]=j;
}
//~ cout<<i<<" :: "<<ind2[0][i]<<" :: "<<ind2[1][i]<<endl;
}
int yaz=f(0,0,0,0,3);
if(yaz>=1000000000)yaz=-1;
cout<<yaz;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
1002224 KB |
Output is correct |
2 |
Correct |
392 ms |
1002172 KB |
Output is correct |
3 |
Correct |
347 ms |
1002200 KB |
Output is correct |
4 |
Correct |
359 ms |
1002296 KB |
Output is correct |
5 |
Correct |
375 ms |
1002208 KB |
Output is correct |
6 |
Correct |
399 ms |
1002320 KB |
Output is correct |
7 |
Correct |
353 ms |
1002184 KB |
Output is correct |
8 |
Correct |
370 ms |
1002300 KB |
Output is correct |
9 |
Correct |
346 ms |
1002148 KB |
Output is correct |
10 |
Correct |
393 ms |
1002156 KB |
Output is correct |
11 |
Correct |
352 ms |
1002188 KB |
Output is correct |
12 |
Correct |
353 ms |
1002224 KB |
Output is correct |
13 |
Correct |
354 ms |
1002204 KB |
Output is correct |
14 |
Correct |
351 ms |
1002260 KB |
Output is correct |
15 |
Correct |
366 ms |
1002232 KB |
Output is correct |
16 |
Correct |
350 ms |
1002232 KB |
Output is correct |
17 |
Correct |
370 ms |
1002220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
1002224 KB |
Output is correct |
2 |
Correct |
392 ms |
1002172 KB |
Output is correct |
3 |
Correct |
347 ms |
1002200 KB |
Output is correct |
4 |
Correct |
359 ms |
1002296 KB |
Output is correct |
5 |
Correct |
375 ms |
1002208 KB |
Output is correct |
6 |
Correct |
399 ms |
1002320 KB |
Output is correct |
7 |
Correct |
353 ms |
1002184 KB |
Output is correct |
8 |
Correct |
370 ms |
1002300 KB |
Output is correct |
9 |
Correct |
346 ms |
1002148 KB |
Output is correct |
10 |
Correct |
393 ms |
1002156 KB |
Output is correct |
11 |
Correct |
352 ms |
1002188 KB |
Output is correct |
12 |
Correct |
353 ms |
1002224 KB |
Output is correct |
13 |
Correct |
354 ms |
1002204 KB |
Output is correct |
14 |
Correct |
351 ms |
1002260 KB |
Output is correct |
15 |
Correct |
366 ms |
1002232 KB |
Output is correct |
16 |
Correct |
350 ms |
1002232 KB |
Output is correct |
17 |
Correct |
370 ms |
1002220 KB |
Output is correct |
18 |
Correct |
359 ms |
1002192 KB |
Output is correct |
19 |
Correct |
365 ms |
1002188 KB |
Output is correct |
20 |
Correct |
353 ms |
1002168 KB |
Output is correct |
21 |
Correct |
370 ms |
1002176 KB |
Output is correct |
22 |
Correct |
360 ms |
1002228 KB |
Output is correct |
23 |
Correct |
361 ms |
1002216 KB |
Output is correct |
24 |
Correct |
360 ms |
1002176 KB |
Output is correct |
25 |
Correct |
379 ms |
1002316 KB |
Output is correct |
26 |
Correct |
384 ms |
1002232 KB |
Output is correct |
27 |
Correct |
361 ms |
1002188 KB |
Output is correct |
28 |
Correct |
353 ms |
1002152 KB |
Output is correct |
29 |
Correct |
390 ms |
1002220 KB |
Output is correct |
30 |
Correct |
356 ms |
1002164 KB |
Output is correct |
31 |
Correct |
366 ms |
1002236 KB |
Output is correct |
32 |
Correct |
356 ms |
1002252 KB |
Output is correct |
33 |
Correct |
372 ms |
1002144 KB |
Output is correct |
34 |
Correct |
358 ms |
1002200 KB |
Output is correct |
35 |
Correct |
385 ms |
1002196 KB |
Output is correct |
36 |
Correct |
363 ms |
1002172 KB |
Output is correct |
37 |
Correct |
375 ms |
1002228 KB |
Output is correct |
38 |
Correct |
373 ms |
1002244 KB |
Output is correct |
39 |
Correct |
365 ms |
1002184 KB |
Output is correct |
40 |
Correct |
357 ms |
1002180 KB |
Output is correct |
41 |
Correct |
354 ms |
1002184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
360 ms |
1002276 KB |
Output is correct |
2 |
Correct |
381 ms |
1002320 KB |
Output is correct |
3 |
Correct |
354 ms |
1002444 KB |
Output is correct |
4 |
Correct |
352 ms |
1002280 KB |
Output is correct |
5 |
Correct |
364 ms |
1002328 KB |
Output is correct |
6 |
Correct |
358 ms |
1002220 KB |
Output is correct |
7 |
Correct |
375 ms |
1002248 KB |
Output is correct |
8 |
Correct |
362 ms |
1002312 KB |
Output is correct |
9 |
Correct |
368 ms |
1002468 KB |
Output is correct |
10 |
Correct |
385 ms |
1002200 KB |
Output is correct |
11 |
Correct |
369 ms |
1002336 KB |
Output is correct |
12 |
Correct |
351 ms |
1002236 KB |
Output is correct |
13 |
Correct |
376 ms |
1002396 KB |
Output is correct |
14 |
Correct |
355 ms |
1002188 KB |
Output is correct |
15 |
Correct |
345 ms |
1002316 KB |
Output is correct |
16 |
Correct |
353 ms |
1002280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
1002224 KB |
Output is correct |
2 |
Correct |
392 ms |
1002172 KB |
Output is correct |
3 |
Correct |
347 ms |
1002200 KB |
Output is correct |
4 |
Correct |
359 ms |
1002296 KB |
Output is correct |
5 |
Correct |
375 ms |
1002208 KB |
Output is correct |
6 |
Correct |
399 ms |
1002320 KB |
Output is correct |
7 |
Correct |
353 ms |
1002184 KB |
Output is correct |
8 |
Correct |
370 ms |
1002300 KB |
Output is correct |
9 |
Correct |
346 ms |
1002148 KB |
Output is correct |
10 |
Correct |
393 ms |
1002156 KB |
Output is correct |
11 |
Correct |
352 ms |
1002188 KB |
Output is correct |
12 |
Correct |
353 ms |
1002224 KB |
Output is correct |
13 |
Correct |
354 ms |
1002204 KB |
Output is correct |
14 |
Correct |
351 ms |
1002260 KB |
Output is correct |
15 |
Correct |
366 ms |
1002232 KB |
Output is correct |
16 |
Correct |
350 ms |
1002232 KB |
Output is correct |
17 |
Correct |
370 ms |
1002220 KB |
Output is correct |
18 |
Correct |
359 ms |
1002192 KB |
Output is correct |
19 |
Correct |
365 ms |
1002188 KB |
Output is correct |
20 |
Correct |
353 ms |
1002168 KB |
Output is correct |
21 |
Correct |
370 ms |
1002176 KB |
Output is correct |
22 |
Correct |
360 ms |
1002228 KB |
Output is correct |
23 |
Correct |
361 ms |
1002216 KB |
Output is correct |
24 |
Correct |
360 ms |
1002176 KB |
Output is correct |
25 |
Correct |
379 ms |
1002316 KB |
Output is correct |
26 |
Correct |
384 ms |
1002232 KB |
Output is correct |
27 |
Correct |
361 ms |
1002188 KB |
Output is correct |
28 |
Correct |
353 ms |
1002152 KB |
Output is correct |
29 |
Correct |
390 ms |
1002220 KB |
Output is correct |
30 |
Correct |
356 ms |
1002164 KB |
Output is correct |
31 |
Correct |
366 ms |
1002236 KB |
Output is correct |
32 |
Correct |
356 ms |
1002252 KB |
Output is correct |
33 |
Correct |
372 ms |
1002144 KB |
Output is correct |
34 |
Correct |
358 ms |
1002200 KB |
Output is correct |
35 |
Correct |
385 ms |
1002196 KB |
Output is correct |
36 |
Correct |
363 ms |
1002172 KB |
Output is correct |
37 |
Correct |
375 ms |
1002228 KB |
Output is correct |
38 |
Correct |
373 ms |
1002244 KB |
Output is correct |
39 |
Correct |
365 ms |
1002184 KB |
Output is correct |
40 |
Correct |
357 ms |
1002180 KB |
Output is correct |
41 |
Correct |
354 ms |
1002184 KB |
Output is correct |
42 |
Correct |
360 ms |
1002276 KB |
Output is correct |
43 |
Correct |
381 ms |
1002320 KB |
Output is correct |
44 |
Correct |
354 ms |
1002444 KB |
Output is correct |
45 |
Correct |
352 ms |
1002280 KB |
Output is correct |
46 |
Correct |
364 ms |
1002328 KB |
Output is correct |
47 |
Correct |
358 ms |
1002220 KB |
Output is correct |
48 |
Correct |
375 ms |
1002248 KB |
Output is correct |
49 |
Correct |
362 ms |
1002312 KB |
Output is correct |
50 |
Correct |
368 ms |
1002468 KB |
Output is correct |
51 |
Correct |
385 ms |
1002200 KB |
Output is correct |
52 |
Correct |
369 ms |
1002336 KB |
Output is correct |
53 |
Correct |
351 ms |
1002236 KB |
Output is correct |
54 |
Correct |
376 ms |
1002396 KB |
Output is correct |
55 |
Correct |
355 ms |
1002188 KB |
Output is correct |
56 |
Correct |
345 ms |
1002316 KB |
Output is correct |
57 |
Correct |
353 ms |
1002280 KB |
Output is correct |
58 |
Correct |
437 ms |
1002428 KB |
Output is correct |
59 |
Correct |
442 ms |
1002316 KB |
Output is correct |
60 |
Correct |
461 ms |
1002384 KB |
Output is correct |
61 |
Correct |
438 ms |
1002328 KB |
Output is correct |
62 |
Correct |
363 ms |
1002244 KB |
Output is correct |
63 |
Correct |
353 ms |
1002240 KB |
Output is correct |
64 |
Correct |
406 ms |
1002296 KB |
Output is correct |
65 |
Correct |
410 ms |
1002220 KB |
Output is correct |
66 |
Correct |
440 ms |
1002412 KB |
Output is correct |
67 |
Correct |
446 ms |
1002280 KB |
Output is correct |
68 |
Correct |
461 ms |
1002316 KB |
Output is correct |
69 |
Correct |
479 ms |
1002348 KB |
Output is correct |
70 |
Correct |
474 ms |
1002248 KB |
Output is correct |
71 |
Correct |
448 ms |
1002316 KB |
Output is correct |
72 |
Correct |
352 ms |
1002264 KB |
Output is correct |
73 |
Correct |
384 ms |
1002280 KB |
Output is correct |