#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll MOD=1e9+7;
const ll N=4e2+5;
const ld pi=3.14159265359;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll)a.size()
ll R,G,Y;
ll n,dp[N][N][N][3],p[N][3];
string s;
vector<ll> v[3];
ll f(ll id,ll ty,ll ha){
return max(0LL,ha-p[id][ty]);
}
int main(){
//ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>s;
REP(i,SZ(s))(s[i]=='R'?v[0]:(s[i]=='G'?v[1]:v[2])).pb(i),(s[i]=='R'?R:(s[i]=='G'?G:Y))++;
REP(i,n){
REP(ty,3)p[i][ty]=(i?p[i-1][ty]:0LL);
(s[i]=='R'?p[i][0]:(s[i]=='G'?p[i][1]:p[i][2]))++;
}
REP(i,2)REP(j,N)REP(k,N)REP(ty,3)dp[i][j][k][ty]=(1LL<<60);
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
REP(i,n){
REP(j,G+1){
REP(k,Y+1){
if(k+j>i||i-j-k>R)continue;
REP(ty,3){
//cout<<i<<" "<<j<<" "<<k<<" "<<ty<<"::"<<dp[i][j][k][ty]<<"\n";
ll x=i-j-k;
if(ty!=0&&x<R)dp[((i+1)&1)][j][k][0]=min(dp[((i+1)&1)][j][k][0],dp[(i&1)][j][k][ty]+(v[0][x]+f(v[0][x],1,j)+f(v[0][x],2,k)-i));
if(ty!=1&&j<G)dp[((i+1)&1)][j+1][k][1]=min(dp[((i+1)&1)][j+1][k][1],dp[(i&1)][j][k][ty]+(v[1][j]+f(v[1][j],0,x)+f(v[1][j],2,k)-i));
if(ty!=2&&k<Y)dp[((i+1)&1)][j][k+1][2]=min(dp[((i+1)&1)][j][k+1][2],dp[(i&1)][j][k][ty]+(v[2][k]+f(v[2][k],0,x)+f(v[2][k],1,j)-i));
}
}
}
REP(j,G+1)REP(k,Y+1)REP(ty,3)dp[(i&1)][j][k][ty]=(1LL<<60);
}
ll ans=min(dp[(n&1)][G][Y][0],min(dp[(n&1)][G][Y][1],dp[(n&1)][G][Y][2]));
cout<<(ans>=(1LL<<60)?-1:ans)<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8056 KB |
Output is correct |
2 |
Correct |
9 ms |
8056 KB |
Output is correct |
3 |
Correct |
8 ms |
8056 KB |
Output is correct |
4 |
Correct |
9 ms |
8104 KB |
Output is correct |
5 |
Correct |
9 ms |
8056 KB |
Output is correct |
6 |
Correct |
9 ms |
8056 KB |
Output is correct |
7 |
Correct |
9 ms |
8056 KB |
Output is correct |
8 |
Correct |
8 ms |
8056 KB |
Output is correct |
9 |
Correct |
9 ms |
8056 KB |
Output is correct |
10 |
Correct |
9 ms |
8056 KB |
Output is correct |
11 |
Correct |
9 ms |
8056 KB |
Output is correct |
12 |
Correct |
9 ms |
8056 KB |
Output is correct |
13 |
Correct |
8 ms |
8056 KB |
Output is correct |
14 |
Correct |
8 ms |
8056 KB |
Output is correct |
15 |
Correct |
9 ms |
8056 KB |
Output is correct |
16 |
Correct |
9 ms |
8056 KB |
Output is correct |
17 |
Correct |
8 ms |
8056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8056 KB |
Output is correct |
2 |
Correct |
9 ms |
8056 KB |
Output is correct |
3 |
Correct |
8 ms |
8056 KB |
Output is correct |
4 |
Correct |
9 ms |
8104 KB |
Output is correct |
5 |
Correct |
9 ms |
8056 KB |
Output is correct |
6 |
Correct |
9 ms |
8056 KB |
Output is correct |
7 |
Correct |
9 ms |
8056 KB |
Output is correct |
8 |
Correct |
8 ms |
8056 KB |
Output is correct |
9 |
Correct |
9 ms |
8056 KB |
Output is correct |
10 |
Correct |
9 ms |
8056 KB |
Output is correct |
11 |
Correct |
9 ms |
8056 KB |
Output is correct |
12 |
Correct |
9 ms |
8056 KB |
Output is correct |
13 |
Correct |
8 ms |
8056 KB |
Output is correct |
14 |
Correct |
8 ms |
8056 KB |
Output is correct |
15 |
Correct |
9 ms |
8056 KB |
Output is correct |
16 |
Correct |
9 ms |
8056 KB |
Output is correct |
17 |
Correct |
8 ms |
8056 KB |
Output is correct |
18 |
Correct |
9 ms |
8056 KB |
Output is correct |
19 |
Correct |
9 ms |
8056 KB |
Output is correct |
20 |
Correct |
9 ms |
8056 KB |
Output is correct |
21 |
Correct |
9 ms |
8056 KB |
Output is correct |
22 |
Correct |
8 ms |
8056 KB |
Output is correct |
23 |
Correct |
9 ms |
8056 KB |
Output is correct |
24 |
Correct |
9 ms |
8056 KB |
Output is correct |
25 |
Correct |
9 ms |
8056 KB |
Output is correct |
26 |
Correct |
9 ms |
8056 KB |
Output is correct |
27 |
Correct |
9 ms |
8056 KB |
Output is correct |
28 |
Correct |
9 ms |
8056 KB |
Output is correct |
29 |
Correct |
9 ms |
8056 KB |
Output is correct |
30 |
Correct |
9 ms |
8056 KB |
Output is correct |
31 |
Correct |
9 ms |
8056 KB |
Output is correct |
32 |
Correct |
9 ms |
8056 KB |
Output is correct |
33 |
Correct |
8 ms |
8056 KB |
Output is correct |
34 |
Correct |
9 ms |
8056 KB |
Output is correct |
35 |
Correct |
9 ms |
8056 KB |
Output is correct |
36 |
Correct |
9 ms |
8056 KB |
Output is correct |
37 |
Correct |
9 ms |
8056 KB |
Output is correct |
38 |
Correct |
9 ms |
8056 KB |
Output is correct |
39 |
Correct |
9 ms |
8056 KB |
Output is correct |
40 |
Correct |
9 ms |
8056 KB |
Output is correct |
41 |
Correct |
9 ms |
8056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8056 KB |
Output is correct |
2 |
Correct |
11 ms |
8056 KB |
Output is correct |
3 |
Correct |
9 ms |
8056 KB |
Output is correct |
4 |
Correct |
11 ms |
8184 KB |
Output is correct |
5 |
Correct |
9 ms |
8056 KB |
Output is correct |
6 |
Correct |
10 ms |
8056 KB |
Output is correct |
7 |
Correct |
10 ms |
8056 KB |
Output is correct |
8 |
Correct |
10 ms |
8056 KB |
Output is correct |
9 |
Correct |
10 ms |
8056 KB |
Output is correct |
10 |
Correct |
9 ms |
8056 KB |
Output is correct |
11 |
Correct |
10 ms |
8056 KB |
Output is correct |
12 |
Correct |
9 ms |
8056 KB |
Output is correct |
13 |
Correct |
9 ms |
8056 KB |
Output is correct |
14 |
Correct |
9 ms |
8056 KB |
Output is correct |
15 |
Correct |
10 ms |
8056 KB |
Output is correct |
16 |
Correct |
10 ms |
8056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8056 KB |
Output is correct |
2 |
Correct |
9 ms |
8056 KB |
Output is correct |
3 |
Correct |
8 ms |
8056 KB |
Output is correct |
4 |
Correct |
9 ms |
8104 KB |
Output is correct |
5 |
Correct |
9 ms |
8056 KB |
Output is correct |
6 |
Correct |
9 ms |
8056 KB |
Output is correct |
7 |
Correct |
9 ms |
8056 KB |
Output is correct |
8 |
Correct |
8 ms |
8056 KB |
Output is correct |
9 |
Correct |
9 ms |
8056 KB |
Output is correct |
10 |
Correct |
9 ms |
8056 KB |
Output is correct |
11 |
Correct |
9 ms |
8056 KB |
Output is correct |
12 |
Correct |
9 ms |
8056 KB |
Output is correct |
13 |
Correct |
8 ms |
8056 KB |
Output is correct |
14 |
Correct |
8 ms |
8056 KB |
Output is correct |
15 |
Correct |
9 ms |
8056 KB |
Output is correct |
16 |
Correct |
9 ms |
8056 KB |
Output is correct |
17 |
Correct |
8 ms |
8056 KB |
Output is correct |
18 |
Correct |
9 ms |
8056 KB |
Output is correct |
19 |
Correct |
9 ms |
8056 KB |
Output is correct |
20 |
Correct |
9 ms |
8056 KB |
Output is correct |
21 |
Correct |
9 ms |
8056 KB |
Output is correct |
22 |
Correct |
8 ms |
8056 KB |
Output is correct |
23 |
Correct |
9 ms |
8056 KB |
Output is correct |
24 |
Correct |
9 ms |
8056 KB |
Output is correct |
25 |
Correct |
9 ms |
8056 KB |
Output is correct |
26 |
Correct |
9 ms |
8056 KB |
Output is correct |
27 |
Correct |
9 ms |
8056 KB |
Output is correct |
28 |
Correct |
9 ms |
8056 KB |
Output is correct |
29 |
Correct |
9 ms |
8056 KB |
Output is correct |
30 |
Correct |
9 ms |
8056 KB |
Output is correct |
31 |
Correct |
9 ms |
8056 KB |
Output is correct |
32 |
Correct |
9 ms |
8056 KB |
Output is correct |
33 |
Correct |
8 ms |
8056 KB |
Output is correct |
34 |
Correct |
9 ms |
8056 KB |
Output is correct |
35 |
Correct |
9 ms |
8056 KB |
Output is correct |
36 |
Correct |
9 ms |
8056 KB |
Output is correct |
37 |
Correct |
9 ms |
8056 KB |
Output is correct |
38 |
Correct |
9 ms |
8056 KB |
Output is correct |
39 |
Correct |
9 ms |
8056 KB |
Output is correct |
40 |
Correct |
9 ms |
8056 KB |
Output is correct |
41 |
Correct |
9 ms |
8056 KB |
Output is correct |
42 |
Correct |
9 ms |
8056 KB |
Output is correct |
43 |
Correct |
11 ms |
8056 KB |
Output is correct |
44 |
Correct |
9 ms |
8056 KB |
Output is correct |
45 |
Correct |
11 ms |
8184 KB |
Output is correct |
46 |
Correct |
9 ms |
8056 KB |
Output is correct |
47 |
Correct |
10 ms |
8056 KB |
Output is correct |
48 |
Correct |
10 ms |
8056 KB |
Output is correct |
49 |
Correct |
10 ms |
8056 KB |
Output is correct |
50 |
Correct |
10 ms |
8056 KB |
Output is correct |
51 |
Correct |
9 ms |
8056 KB |
Output is correct |
52 |
Correct |
10 ms |
8056 KB |
Output is correct |
53 |
Correct |
9 ms |
8056 KB |
Output is correct |
54 |
Correct |
9 ms |
8056 KB |
Output is correct |
55 |
Correct |
9 ms |
8056 KB |
Output is correct |
56 |
Correct |
10 ms |
8056 KB |
Output is correct |
57 |
Correct |
10 ms |
8056 KB |
Output is correct |
58 |
Correct |
70 ms |
8056 KB |
Output is correct |
59 |
Correct |
68 ms |
8056 KB |
Output is correct |
60 |
Correct |
65 ms |
8060 KB |
Output is correct |
61 |
Correct |
74 ms |
8184 KB |
Output is correct |
62 |
Correct |
13 ms |
8056 KB |
Output is correct |
63 |
Correct |
16 ms |
8056 KB |
Output is correct |
64 |
Correct |
37 ms |
8056 KB |
Output is correct |
65 |
Correct |
48 ms |
8056 KB |
Output is correct |
66 |
Correct |
67 ms |
8056 KB |
Output is correct |
67 |
Correct |
69 ms |
8096 KB |
Output is correct |
68 |
Correct |
71 ms |
8056 KB |
Output is correct |
69 |
Correct |
69 ms |
8056 KB |
Output is correct |
70 |
Correct |
69 ms |
8056 KB |
Output is correct |
71 |
Correct |
64 ms |
8056 KB |
Output is correct |
72 |
Correct |
22 ms |
8184 KB |
Output is correct |
73 |
Correct |
38 ms |
8056 KB |
Output is correct |