This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<int,ll> pil;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const ll inf=1e18;
const int N=2e2+1;
ll dp[N][N][N];
///l,r,prefix
signed main(){
fast_resp;
int n;
string s;
cin>>n>>s;
int a,b,c;
cin>>a>>b>>c;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
for(int k=0;k<=j-i+1;k++)
dp[i][j][k]=inf;
}
}
for(int d=1;d<=n;d++){
for(int l=0;l+d-1<n;l++){
int r=l+d-1;
if(l==r) dp[l][r][0]=a;
for(int k=0;k<=r-l+1;k++){
// if(k && l+k<n)
// umin(dp[l][r][k],dp[l+k][r][0]+dp[l][l+k-1][0]+b);
if(r-k+1>=l+k && s.substr(l,k)==s.substr(r-k+1,k)){
umin(dp[l][r][k],dp[l][r-k][k]+c);
// cout<<"W "<<k<<" "<<' '<<l<<' '<<r<<' '<<dp[l][r][k]<<endl;
}
umin(dp[l][r][r-l+1],dp[l][r][k]+b+c);
if(l)
umin(dp[l-1][r][0],dp[l][r][k]+a);
if(r+1<n)
umin(dp[l][r+1][k],dp[l][r][k]+a);
umin(dp[l][r][0],dp[l][r][k]);
}
// cout<<dp[2][4][3]<<endl;
}
}
// cout<<dp[2][4][3]<<endl;
ll ans=inf;
for(int i=0;i<=n;i++)
umin(ans,dp[0][n-1][i]);
cout<<ans;
return 0;
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |