#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int MAX=2505;
const int LOG=15;
int N;
ll CA,CB,CC;
int A[MAX];
int up[MAX][MAX][LOG];
int nxt[MAX][MAX];
ll dp[MAX][MAX];
priority_queue<pii,vector<pii>,greater<pii>> pq;
ll V[MAX];
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>N;
for (int i=0;i<N;i++){
char c;cin>>c;
A[i]=c-'a';
}
cin>>CA>>CB>>CC;
for (int l=N-1;l>=0;l--){
int x;
for (x=l+1;x<N;x++) if (A[x]==A[l]) break;
up[l][l][0]=x;
for (int r=l+1;r<N;r++){
while (x-l+r<N&&A[x-l+r]!=A[r]) x=up[x][x-l+(r-1)][0];
if (x-l+r>=N) x=N;
up[l][r][0]=x;
}
}
for (int k=1;k<LOG;k++) for (int l=0;l<N;l++) for (int r=l;r<N;r++){
int x=up[l][r][k-1];
if (x>=N) up[l][r][k]=N;
else up[l][r][k]=up[x][x-l+r][k-1];
}
for (int l=0;l<N;l++){
for (int r=l;r<N;r++){
int x=l;
for (int k=LOG-1;k>=0;k--) if (x-l+r<N&&up[x][x-l+r][k]<=r) x=up[x][x-l+r][k];
nxt[l][r]=up[x][x-l+r][0];
}
}
for (int l=N-1;l>=0;l--){
while (pq.size()) pq.pop();
for (int r=l;r<N;r++){
dp[l][r]=min(dp[l+1][r]+CA,dp[l][r-1]+CA);
while (pq.size()&&pq.top().first<=r){
auto [_,x]=pq.top();
pq.pop();
pq.push({nxt[r-x+l][r]-l+x,x});
V[x]+=CC-(x-l+1)*CA;
dp[l][r]=min(dp[l][r],(r-l+1)*CA+V[x]);
}
V[r]=dp[l][r]+CB+CC-(r-l+1)*CA;
pq.push({nxt[l][r]-l+r,r});
}
}
cout<<dp[0][N-1]<<'\n';
}
| # | 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... |