#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=1e3+3;
ll dp[N][N];
///l,r,prefix
//int pref[N][N];
vec<int> zet[N];
vec<int> z_func(string s){
int l=0,r=-1;
vec<int> zt(sz(s),0);
for(int i=1;i<sz(s);i++){
zt[i]=(i>r?0:min(r-i+1,zt[i-l]));
while(i+zt[i]<sz(s) && s[i+zt[i]]==s[zt[i]]) ++zt[i];
if(i+zt[i]-1>r){
r=i+zt[i]-1;
l=i;
}
}
return zt;
}
signed main(){
fast_resp;
int n;
string s;
cin>>n>>s;
int a,b,c;
cin>>a>>b>>c;
for(int j=0;j<N;j++){
for(int k=0;k<N;k++)
dp[j][k]=inf;
}
vec<vec<int>>nxt(n,vec<int>(n+1,-1));
for(int i=0;i<n;i++){
zet[i]=z_func(s.substr(i,n-i)+'#'+s.substr(i,n-i));
int last=0;
for(int j=i;j<n;j++){
// if((last+1)==1 && i==2){
// i=2;
// cout<<zet[i][j-i+n-i+1]<<endl;
// }
while(last+1<=zet[i][j-i+n-i+1] && (j-i)>=last+1){
++last;
// cout<<"WO "<<i<<' '<<last<<' '<<j<<endl;
nxt[i][last]=j;
}
}
}
for(int l=n-1;l>=0;l--){
for(int r=l;r<n;r++){
if(l==r) dp[l][r]=a+b;
// else{
if(l) umin(dp[l-1][r],dp[l][r]+a);
if(r+1<n) umin(dp[l][r+1],dp[l][r]+a);
ll total=dp[l][r]+c;
// if(l==2 && r==2){
// l=r=2;
// l=2;
// }
/// i'ma b
int j=nxt[l][r-l+1];
int last=r;
// cout<<"L "<<l<<' '<<r<<endl;
while(j!=-1){
// assert(j>last);
// cout<<"HEY "<<j-last-1<<endl;
total+=1ll*(j-last-1)*a;total+=c;
umin(dp[l][j+r-l],total+b);
last=j+r-l;
j=nxt[j][r-l+1];
}
// cout<<endl;
// }
}
}
// cout<<dp[2][8]<<endl;
cout<<dp[0][n-1]-b;
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8188 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8220 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Runtime error |
54 ms |
64276 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8188 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8220 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
4 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
4 ms |
8216 KB |
Output is correct |
23 |
Correct |
4 ms |
8148 KB |
Output is correct |
24 |
Correct |
4 ms |
8148 KB |
Output is correct |
25 |
Correct |
4 ms |
8216 KB |
Output is correct |
26 |
Correct |
4 ms |
8180 KB |
Output is correct |
27 |
Correct |
3 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
8148 KB |
Output is correct |
29 |
Correct |
4 ms |
8148 KB |
Output is correct |
30 |
Correct |
4 ms |
8148 KB |
Output is correct |
31 |
Correct |
4 ms |
8148 KB |
Output is correct |
32 |
Correct |
4 ms |
8148 KB |
Output is correct |
33 |
Correct |
4 ms |
8148 KB |
Output is correct |
34 |
Correct |
4 ms |
8148 KB |
Output is correct |
35 |
Correct |
4 ms |
8148 KB |
Output is correct |
36 |
Correct |
5 ms |
8148 KB |
Output is correct |
37 |
Correct |
4 ms |
8140 KB |
Output is correct |
38 |
Correct |
4 ms |
8148 KB |
Output is correct |
39 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8188 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8220 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
4 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
4 ms |
8216 KB |
Output is correct |
23 |
Correct |
4 ms |
8148 KB |
Output is correct |
24 |
Correct |
4 ms |
8148 KB |
Output is correct |
25 |
Correct |
4 ms |
8216 KB |
Output is correct |
26 |
Correct |
4 ms |
8180 KB |
Output is correct |
27 |
Correct |
3 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
8148 KB |
Output is correct |
29 |
Correct |
4 ms |
8148 KB |
Output is correct |
30 |
Correct |
4 ms |
8148 KB |
Output is correct |
31 |
Correct |
4 ms |
8148 KB |
Output is correct |
32 |
Correct |
4 ms |
8148 KB |
Output is correct |
33 |
Correct |
4 ms |
8148 KB |
Output is correct |
34 |
Correct |
4 ms |
8148 KB |
Output is correct |
35 |
Correct |
4 ms |
8148 KB |
Output is correct |
36 |
Correct |
5 ms |
8148 KB |
Output is correct |
37 |
Correct |
4 ms |
8140 KB |
Output is correct |
38 |
Correct |
4 ms |
8148 KB |
Output is correct |
39 |
Correct |
4 ms |
8148 KB |
Output is correct |
40 |
Correct |
4 ms |
8276 KB |
Output is correct |
41 |
Correct |
5 ms |
8532 KB |
Output is correct |
42 |
Correct |
4 ms |
8464 KB |
Output is correct |
43 |
Correct |
5 ms |
8532 KB |
Output is correct |
44 |
Correct |
6 ms |
8532 KB |
Output is correct |
45 |
Correct |
6 ms |
8512 KB |
Output is correct |
46 |
Correct |
4 ms |
8532 KB |
Output is correct |
47 |
Correct |
5 ms |
8532 KB |
Output is correct |
48 |
Correct |
4 ms |
8532 KB |
Output is correct |
49 |
Correct |
4 ms |
8532 KB |
Output is correct |
50 |
Correct |
5 ms |
8532 KB |
Output is correct |
51 |
Correct |
4 ms |
8532 KB |
Output is correct |
52 |
Correct |
5 ms |
8532 KB |
Output is correct |
53 |
Correct |
4 ms |
8532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8188 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8220 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
4 ms |
8148 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
4 ms |
8148 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
4 ms |
8216 KB |
Output is correct |
23 |
Correct |
4 ms |
8148 KB |
Output is correct |
24 |
Correct |
4 ms |
8148 KB |
Output is correct |
25 |
Correct |
4 ms |
8216 KB |
Output is correct |
26 |
Correct |
4 ms |
8180 KB |
Output is correct |
27 |
Correct |
3 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
8148 KB |
Output is correct |
29 |
Correct |
4 ms |
8148 KB |
Output is correct |
30 |
Correct |
4 ms |
8148 KB |
Output is correct |
31 |
Correct |
4 ms |
8148 KB |
Output is correct |
32 |
Correct |
4 ms |
8148 KB |
Output is correct |
33 |
Correct |
4 ms |
8148 KB |
Output is correct |
34 |
Correct |
4 ms |
8148 KB |
Output is correct |
35 |
Correct |
4 ms |
8148 KB |
Output is correct |
36 |
Correct |
5 ms |
8148 KB |
Output is correct |
37 |
Correct |
4 ms |
8140 KB |
Output is correct |
38 |
Correct |
4 ms |
8148 KB |
Output is correct |
39 |
Correct |
4 ms |
8148 KB |
Output is correct |
40 |
Correct |
4 ms |
8276 KB |
Output is correct |
41 |
Correct |
5 ms |
8532 KB |
Output is correct |
42 |
Correct |
4 ms |
8464 KB |
Output is correct |
43 |
Correct |
5 ms |
8532 KB |
Output is correct |
44 |
Correct |
6 ms |
8532 KB |
Output is correct |
45 |
Correct |
6 ms |
8512 KB |
Output is correct |
46 |
Correct |
4 ms |
8532 KB |
Output is correct |
47 |
Correct |
5 ms |
8532 KB |
Output is correct |
48 |
Correct |
4 ms |
8532 KB |
Output is correct |
49 |
Correct |
4 ms |
8532 KB |
Output is correct |
50 |
Correct |
5 ms |
8532 KB |
Output is correct |
51 |
Correct |
4 ms |
8532 KB |
Output is correct |
52 |
Correct |
5 ms |
8532 KB |
Output is correct |
53 |
Correct |
4 ms |
8532 KB |
Output is correct |
54 |
Correct |
7 ms |
9812 KB |
Output is correct |
55 |
Correct |
38 ms |
16084 KB |
Output is correct |
56 |
Correct |
22 ms |
16084 KB |
Output is correct |
57 |
Correct |
20 ms |
16072 KB |
Output is correct |
58 |
Correct |
17 ms |
16108 KB |
Output is correct |
59 |
Correct |
15 ms |
15996 KB |
Output is correct |
60 |
Correct |
14 ms |
16084 KB |
Output is correct |
61 |
Correct |
15 ms |
16084 KB |
Output is correct |
62 |
Correct |
35 ms |
16084 KB |
Output is correct |
63 |
Correct |
15 ms |
16084 KB |
Output is correct |
64 |
Correct |
15 ms |
16084 KB |
Output is correct |
65 |
Correct |
22 ms |
16036 KB |
Output is correct |
66 |
Correct |
22 ms |
16064 KB |
Output is correct |
67 |
Correct |
14 ms |
16084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8188 KB |
Output is correct |
2 |
Correct |
3 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
6 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8220 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Runtime error |
54 ms |
64276 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |