#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=65;
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,N)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+1){
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][j][k][0]=min(dp[i+1][j][k][0],dp[i][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][j+1][k][1]=min(dp[i+1][j+1][k][1],dp[i][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][j][k+1][2]=min(dp[i+1][j][k+1][2],dp[i][j][k][ty]+(v[2][k]+f(v[2][k],0,x)+f(v[2][k],1,j)-i));
}
}
}
}
ll ans=min(dp[n][G][Y][0],min(dp[n][G][Y][1],dp[n][G][Y][2]));
cout<<(ans>=(1LL<<60)?-1:ans)<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6776 KB |
Output is correct |
2 |
Correct |
8 ms |
6776 KB |
Output is correct |
3 |
Correct |
8 ms |
6776 KB |
Output is correct |
4 |
Correct |
8 ms |
6776 KB |
Output is correct |
5 |
Correct |
8 ms |
6776 KB |
Output is correct |
6 |
Correct |
8 ms |
6776 KB |
Output is correct |
7 |
Correct |
8 ms |
6776 KB |
Output is correct |
8 |
Correct |
8 ms |
6776 KB |
Output is correct |
9 |
Correct |
8 ms |
6776 KB |
Output is correct |
10 |
Correct |
8 ms |
6776 KB |
Output is correct |
11 |
Correct |
8 ms |
6776 KB |
Output is correct |
12 |
Correct |
9 ms |
6776 KB |
Output is correct |
13 |
Correct |
8 ms |
6776 KB |
Output is correct |
14 |
Correct |
8 ms |
6780 KB |
Output is correct |
15 |
Correct |
8 ms |
6780 KB |
Output is correct |
16 |
Correct |
8 ms |
6776 KB |
Output is correct |
17 |
Correct |
9 ms |
6776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6776 KB |
Output is correct |
2 |
Correct |
8 ms |
6776 KB |
Output is correct |
3 |
Correct |
8 ms |
6776 KB |
Output is correct |
4 |
Correct |
8 ms |
6776 KB |
Output is correct |
5 |
Correct |
8 ms |
6776 KB |
Output is correct |
6 |
Correct |
8 ms |
6776 KB |
Output is correct |
7 |
Correct |
8 ms |
6776 KB |
Output is correct |
8 |
Correct |
8 ms |
6776 KB |
Output is correct |
9 |
Correct |
8 ms |
6776 KB |
Output is correct |
10 |
Correct |
8 ms |
6776 KB |
Output is correct |
11 |
Correct |
8 ms |
6776 KB |
Output is correct |
12 |
Correct |
9 ms |
6776 KB |
Output is correct |
13 |
Correct |
8 ms |
6776 KB |
Output is correct |
14 |
Correct |
8 ms |
6780 KB |
Output is correct |
15 |
Correct |
8 ms |
6780 KB |
Output is correct |
16 |
Correct |
8 ms |
6776 KB |
Output is correct |
17 |
Correct |
9 ms |
6776 KB |
Output is correct |
18 |
Correct |
8 ms |
6776 KB |
Output is correct |
19 |
Correct |
8 ms |
6776 KB |
Output is correct |
20 |
Correct |
8 ms |
6780 KB |
Output is correct |
21 |
Correct |
8 ms |
6776 KB |
Output is correct |
22 |
Correct |
8 ms |
6776 KB |
Output is correct |
23 |
Correct |
8 ms |
6700 KB |
Output is correct |
24 |
Correct |
8 ms |
6776 KB |
Output is correct |
25 |
Correct |
8 ms |
6776 KB |
Output is correct |
26 |
Correct |
9 ms |
6776 KB |
Output is correct |
27 |
Correct |
8 ms |
6776 KB |
Output is correct |
28 |
Correct |
8 ms |
6776 KB |
Output is correct |
29 |
Correct |
8 ms |
6776 KB |
Output is correct |
30 |
Correct |
9 ms |
6776 KB |
Output is correct |
31 |
Correct |
9 ms |
6776 KB |
Output is correct |
32 |
Correct |
10 ms |
6776 KB |
Output is correct |
33 |
Correct |
8 ms |
6776 KB |
Output is correct |
34 |
Correct |
8 ms |
6776 KB |
Output is correct |
35 |
Correct |
8 ms |
6776 KB |
Output is correct |
36 |
Correct |
8 ms |
6776 KB |
Output is correct |
37 |
Correct |
8 ms |
6776 KB |
Output is correct |
38 |
Correct |
8 ms |
6776 KB |
Output is correct |
39 |
Correct |
8 ms |
6776 KB |
Output is correct |
40 |
Correct |
8 ms |
6776 KB |
Output is correct |
41 |
Correct |
8 ms |
6776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6776 KB |
Output is correct |
2 |
Runtime error |
16 ms |
13432 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6776 KB |
Output is correct |
2 |
Correct |
8 ms |
6776 KB |
Output is correct |
3 |
Correct |
8 ms |
6776 KB |
Output is correct |
4 |
Correct |
8 ms |
6776 KB |
Output is correct |
5 |
Correct |
8 ms |
6776 KB |
Output is correct |
6 |
Correct |
8 ms |
6776 KB |
Output is correct |
7 |
Correct |
8 ms |
6776 KB |
Output is correct |
8 |
Correct |
8 ms |
6776 KB |
Output is correct |
9 |
Correct |
8 ms |
6776 KB |
Output is correct |
10 |
Correct |
8 ms |
6776 KB |
Output is correct |
11 |
Correct |
8 ms |
6776 KB |
Output is correct |
12 |
Correct |
9 ms |
6776 KB |
Output is correct |
13 |
Correct |
8 ms |
6776 KB |
Output is correct |
14 |
Correct |
8 ms |
6780 KB |
Output is correct |
15 |
Correct |
8 ms |
6780 KB |
Output is correct |
16 |
Correct |
8 ms |
6776 KB |
Output is correct |
17 |
Correct |
9 ms |
6776 KB |
Output is correct |
18 |
Correct |
8 ms |
6776 KB |
Output is correct |
19 |
Correct |
8 ms |
6776 KB |
Output is correct |
20 |
Correct |
8 ms |
6780 KB |
Output is correct |
21 |
Correct |
8 ms |
6776 KB |
Output is correct |
22 |
Correct |
8 ms |
6776 KB |
Output is correct |
23 |
Correct |
8 ms |
6700 KB |
Output is correct |
24 |
Correct |
8 ms |
6776 KB |
Output is correct |
25 |
Correct |
8 ms |
6776 KB |
Output is correct |
26 |
Correct |
9 ms |
6776 KB |
Output is correct |
27 |
Correct |
8 ms |
6776 KB |
Output is correct |
28 |
Correct |
8 ms |
6776 KB |
Output is correct |
29 |
Correct |
8 ms |
6776 KB |
Output is correct |
30 |
Correct |
9 ms |
6776 KB |
Output is correct |
31 |
Correct |
9 ms |
6776 KB |
Output is correct |
32 |
Correct |
10 ms |
6776 KB |
Output is correct |
33 |
Correct |
8 ms |
6776 KB |
Output is correct |
34 |
Correct |
8 ms |
6776 KB |
Output is correct |
35 |
Correct |
8 ms |
6776 KB |
Output is correct |
36 |
Correct |
8 ms |
6776 KB |
Output is correct |
37 |
Correct |
8 ms |
6776 KB |
Output is correct |
38 |
Correct |
8 ms |
6776 KB |
Output is correct |
39 |
Correct |
8 ms |
6776 KB |
Output is correct |
40 |
Correct |
8 ms |
6776 KB |
Output is correct |
41 |
Correct |
8 ms |
6776 KB |
Output is correct |
42 |
Correct |
8 ms |
6776 KB |
Output is correct |
43 |
Runtime error |
16 ms |
13432 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
44 |
Halted |
0 ms |
0 KB |
- |