#include <bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=2509;
int n;
string s;
long long x,y,z;
long long f[maxn][maxn];
int lcp[maxn][maxn];
int id[maxn];
int nxt[maxn];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n>>s>>x>>y>>z;
s=" "+s;
for1(i,1,n){
for1(j,1,n){
lcp[i][j]=lcp[i-1][j-1]-1;
if (lcp[i-1][j-1]==0)lcp[i][j]=0;
while (lcp[i][j]<=n-max(j,i)+1&&s[i+lcp[i][j]]==s[j+lcp[i][j]]){
lcp[i][j]++;
}
}
}
for1(i,1,n){
for1(j,i,n){
f[i][j]=1ll*(j-i+1)*x;
}
}
for1(len,1,n){
for1(i,1,n-len+1){
id[i]=i;
int j=i+len-1;
if (i>1)f[i-1][j]=min(f[i-1][j],f[i][j]+x);
if (j+1<=n)f[i][j+1]=min(f[i][j+1],f[i][j]+x);
}
sort(id+1,id+1+n-len+1,[&](int i,int j){
if (lcp[i][j]>=len)return i<j;
return s[i+lcp[i][j]]<s[j+lcp[i][j]];
});
for1(i,1,n-len+1)nxt[id[i]]=-1;
//if (len==4)for1(i,1,n-len+1)cout<<id[i]<<" ";
for1(i,1,n-len+1){
int p=id[i];
int poi=i;
for1(j,i,n-len+1){
int q=id[j];
if (lcp[p][q]<len)break;
poi=j;
}
//if (len==1)cout<<i<<" "<<poi<<'\n';
for2(j,poi,i){
int l=j+1,r=poi;
//if (len==1)cout<<id[j]<<" "<<j<<" "<<l<<" "<<r<<'\n';
nxt[id[j]]=-1;
while (l<=r){
int mid=(l+r)/2;
if (id[j]+len<=id[mid]){
nxt[id[j]]=mid;
r=mid-1;
}
else l=mid+1;
}
//if (len==4&&id[j]==4)cout<<nxt[id[j]]<<'\n';
long long cost=y+z+f[id[j]][id[j]+len-1];
int nw=id[j];
//if (len==1)cout<<id[j]<<" "<<nxt[id[j]]<<'\n';
while (true){
if (nxt[nw]==-1)break;
//if (id[j]==1&&len==1)cout<<id[nw]<<" "<<id[nxt[nw]]<<'\n';
int vcl=id[nxt[nw]];
//if (len==4&&id[j]==4)cout<<id[j]<<" "<<nw<<" "<<vcl<<'\n';
cost+=(vcl-1-(nw+len)+1)*x+z;
nw=id[nxt[nw]];
f[id[j]][nw+len-1]=min(f[id[j]][nw+len-1],cost);
}
}
i=poi;
}
}
cout<<f[1][n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
155 ms |
58060 KB |
Output is correct |
4 |
Correct |
210 ms |
62184 KB |
Output is correct |
5 |
Correct |
234 ms |
68324 KB |
Output is correct |
6 |
Correct |
301 ms |
72332 KB |
Output is correct |
7 |
Correct |
334 ms |
72828 KB |
Output is correct |
8 |
Correct |
343 ms |
72828 KB |
Output is correct |
9 |
Correct |
333 ms |
72832 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
0 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6552 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6552 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
1 ms |
6488 KB |
Output is correct |
41 |
Correct |
2 ms |
8540 KB |
Output is correct |
42 |
Correct |
2 ms |
8540 KB |
Output is correct |
43 |
Correct |
2 ms |
8540 KB |
Output is correct |
44 |
Correct |
2 ms |
8540 KB |
Output is correct |
45 |
Correct |
2 ms |
8620 KB |
Output is correct |
46 |
Correct |
2 ms |
8540 KB |
Output is correct |
47 |
Correct |
2 ms |
8540 KB |
Output is correct |
48 |
Correct |
3 ms |
8540 KB |
Output is correct |
49 |
Correct |
3 ms |
8592 KB |
Output is correct |
50 |
Correct |
3 ms |
8540 KB |
Output is correct |
51 |
Correct |
3 ms |
8748 KB |
Output is correct |
52 |
Correct |
2 ms |
8540 KB |
Output is correct |
53 |
Correct |
2 ms |
8736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6552 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
1 ms |
6488 KB |
Output is correct |
41 |
Correct |
2 ms |
8540 KB |
Output is correct |
42 |
Correct |
2 ms |
8540 KB |
Output is correct |
43 |
Correct |
2 ms |
8540 KB |
Output is correct |
44 |
Correct |
2 ms |
8540 KB |
Output is correct |
45 |
Correct |
2 ms |
8620 KB |
Output is correct |
46 |
Correct |
2 ms |
8540 KB |
Output is correct |
47 |
Correct |
2 ms |
8540 KB |
Output is correct |
48 |
Correct |
3 ms |
8540 KB |
Output is correct |
49 |
Correct |
3 ms |
8592 KB |
Output is correct |
50 |
Correct |
3 ms |
8540 KB |
Output is correct |
51 |
Correct |
3 ms |
8748 KB |
Output is correct |
52 |
Correct |
2 ms |
8540 KB |
Output is correct |
53 |
Correct |
2 ms |
8736 KB |
Output is correct |
54 |
Correct |
10 ms |
18780 KB |
Output is correct |
55 |
Correct |
32 ms |
33428 KB |
Output is correct |
56 |
Correct |
51 ms |
33372 KB |
Output is correct |
57 |
Correct |
52 ms |
33372 KB |
Output is correct |
58 |
Correct |
49 ms |
33368 KB |
Output is correct |
59 |
Correct |
50 ms |
33372 KB |
Output is correct |
60 |
Correct |
51 ms |
33620 KB |
Output is correct |
61 |
Correct |
51 ms |
33376 KB |
Output is correct |
62 |
Correct |
40 ms |
33372 KB |
Output is correct |
63 |
Correct |
49 ms |
33372 KB |
Output is correct |
64 |
Correct |
50 ms |
33372 KB |
Output is correct |
65 |
Correct |
52 ms |
33372 KB |
Output is correct |
66 |
Correct |
55 ms |
33368 KB |
Output is correct |
67 |
Correct |
51 ms |
33412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
155 ms |
58060 KB |
Output is correct |
14 |
Correct |
210 ms |
62184 KB |
Output is correct |
15 |
Correct |
234 ms |
68324 KB |
Output is correct |
16 |
Correct |
301 ms |
72332 KB |
Output is correct |
17 |
Correct |
334 ms |
72828 KB |
Output is correct |
18 |
Correct |
343 ms |
72828 KB |
Output is correct |
19 |
Correct |
333 ms |
72832 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
0 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
6552 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6492 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
1 ms |
6492 KB |
Output is correct |
48 |
Correct |
1 ms |
6492 KB |
Output is correct |
49 |
Correct |
1 ms |
6492 KB |
Output is correct |
50 |
Correct |
1 ms |
6492 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
0 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
4444 KB |
Output is correct |
56 |
Correct |
1 ms |
6492 KB |
Output is correct |
57 |
Correct |
1 ms |
6488 KB |
Output is correct |
58 |
Correct |
2 ms |
8540 KB |
Output is correct |
59 |
Correct |
2 ms |
8540 KB |
Output is correct |
60 |
Correct |
2 ms |
8540 KB |
Output is correct |
61 |
Correct |
2 ms |
8540 KB |
Output is correct |
62 |
Correct |
2 ms |
8620 KB |
Output is correct |
63 |
Correct |
2 ms |
8540 KB |
Output is correct |
64 |
Correct |
2 ms |
8540 KB |
Output is correct |
65 |
Correct |
3 ms |
8540 KB |
Output is correct |
66 |
Correct |
3 ms |
8592 KB |
Output is correct |
67 |
Correct |
3 ms |
8540 KB |
Output is correct |
68 |
Correct |
3 ms |
8748 KB |
Output is correct |
69 |
Correct |
2 ms |
8540 KB |
Output is correct |
70 |
Correct |
2 ms |
8736 KB |
Output is correct |
71 |
Correct |
10 ms |
18780 KB |
Output is correct |
72 |
Correct |
32 ms |
33428 KB |
Output is correct |
73 |
Correct |
51 ms |
33372 KB |
Output is correct |
74 |
Correct |
52 ms |
33372 KB |
Output is correct |
75 |
Correct |
49 ms |
33368 KB |
Output is correct |
76 |
Correct |
50 ms |
33372 KB |
Output is correct |
77 |
Correct |
51 ms |
33620 KB |
Output is correct |
78 |
Correct |
51 ms |
33376 KB |
Output is correct |
79 |
Correct |
40 ms |
33372 KB |
Output is correct |
80 |
Correct |
49 ms |
33372 KB |
Output is correct |
81 |
Correct |
50 ms |
33372 KB |
Output is correct |
82 |
Correct |
52 ms |
33372 KB |
Output is correct |
83 |
Correct |
55 ms |
33368 KB |
Output is correct |
84 |
Correct |
51 ms |
33412 KB |
Output is correct |
85 |
Correct |
141 ms |
51880 KB |
Output is correct |
86 |
Correct |
410 ms |
72836 KB |
Output is correct |
87 |
Correct |
418 ms |
72792 KB |
Output is correct |
88 |
Correct |
465 ms |
72836 KB |
Output is correct |
89 |
Correct |
368 ms |
72788 KB |
Output is correct |
90 |
Correct |
398 ms |
72856 KB |
Output is correct |
91 |
Correct |
488 ms |
72836 KB |
Output is correct |
92 |
Correct |
479 ms |
72828 KB |
Output is correct |
93 |
Correct |
418 ms |
72832 KB |
Output is correct |
94 |
Correct |
474 ms |
72836 KB |
Output is correct |
95 |
Correct |
495 ms |
72832 KB |
Output is correct |
96 |
Correct |
550 ms |
72832 KB |
Output is correct |
97 |
Correct |
381 ms |
72784 KB |
Output is correct |