This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define ALL(x) x.begin(),x.end()
int lowbit(int x) {return x&(-x);}
const int maxn = 2555;
int n;
ll A,B,C;
string s;
int a[maxn];
int len[maxn][maxn];
int nxt[maxn][maxn];
ll f[maxn][maxn],g[maxn][maxn];
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
memset(f,inf,sizeof(f));
memset(g,inf,sizeof(g));
cin>>n>>s>>A>>B>>C;
rep(i,0,n) a[i] = (int)(s[i]-'a');
rep(i,1,n) {
vector <int> tmp;
for (int j = 0;i+j<n;j++) {
if (a[j]!=a[j+i]) {
int cnt = 0;
while (!tmp.empty()) {
cnt++;
len[tmp.back()][tmp.back()+i] = cnt;
tmp.pop_back();
}
}
else tmp.pb(j);
}
int cnt = 0;
while (!tmp.empty()) {
cnt++;
len[tmp.back()][tmp.back()+i] = cnt;
tmp.pop_back();
}
}
memset(nxt,-1,sizeof(nxt));
rep(i,0,n) {
pq <pii,vector<pii>,greater<pii>> q;
rep(j,i+1,n) {
q.push({j,len[i][j]});
}
rep(j,i,n) {
while (!q.empty() and (q.top().se<j-i+1 or q.top().fi<=j)) q.pop();
if (!q.empty()) nxt[i][j] = q.top().fi;
// cout<<i<<" "<<j<<" "<<nxt[i][j]<<endl;
}
}
rep(k,0,n) {
for (int i = 0;i+k<n;i++) {
int j = i+k;
f[i][j] = min(f[i][j],(j-i+1)*A);
f[i][j+1] = min(f[i][j+1],f[i][j]+A);
g[i][j] = min(g[i][j],f[i][j]+B);
if (i>=1) g[i-1][j] = min(g[i-1][j],g[i][j]+A);
int curi=i;
ll sum = g[i][j] + C;
while (nxt[curi][curi+j-i]!=-1) {
int tmp = nxt[curi][curi+j-i];
sum += A*(tmp - (curi+j-i) - 1) + C;
f[i][tmp+j-i] = min(f[i][tmp+j-i],sum);
curi = tmp;
}
}
}
ll ans = f[0][n-1];
rep(i,1,n) ans = min(ans,A*i+f[i][n-1]);
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... |