#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=2500+3;
ll dp[2][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 t=0;t<2;t++){
for(int j=0;j<N;j++){
for(int k=0;k<N;k++)
dp[t][j][k]=inf;
}
}
for(int i=0;i<n;i++){
zet[i]=z_func(s.substr(i,n-i)+'#'+s.substr(i,n-i));
// for(int j=i;j<n;j++){
// cout<<s.substr(i,n-i)<<' '<<s.substr(j,n-j)<<' '<<zet[i][j-i+n-i+1]<<'\n';
// }
// for(int j=i+1;j<n;j++){
// int jt=pref[i][j-1];
// while(jt && s[j]!=s[jt+i]) jt=pref[i][jt-1];
// pref[i][j]=jt+(s[jt+i]==s[j]);
// }
}
for(int l=n-1;l>=0;l--){
int nl=(l%2)^1;
for(int r=max(l-1,0);r<n;r++){
for(int k=0;k<=r-l+2;k++)
dp[nl][r][k]=inf;
}
int lt=l%2;
for(int r=l;r<n;r++){
if(l==r) dp[lt][r][0]=a;
for(int k=0;k<=r-l+1;k++){
// cout<<"HEY "<<r-k+2+n-l<<' '<<sz(zet[l])<<endl;
if(r-k+1>=l+k && zet[l][r-k+2+n-l-l]>=k){
// assert((r-k+2+n-l)<sz(zet[l]));
// if(zet[l][r-k+2+n-l-l]>=k){
// assert((r-k+2+n-l)<sz(zet[l]));
umin(dp[lt][r][k],dp[lt][r-k][k]+c);
// }
}
umin(dp[lt][r][r-l+1],dp[lt][r][k]+b+c);
if(l)
umin(dp[nl][r][0],dp[lt][r][k]+a);
if(r+1<n)
umin(dp[lt][r+1][k],dp[lt][r][k]+a);
umin(dp[lt][r][0],dp[lt][r][k]);
}
}
}
// 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 |
1 |
Correct |
37 ms |
98332 KB |
Output is correct |
2 |
Correct |
37 ms |
98428 KB |
Output is correct |
3 |
Correct |
38 ms |
98400 KB |
Output is correct |
4 |
Correct |
38 ms |
98340 KB |
Output is correct |
5 |
Correct |
46 ms |
98416 KB |
Output is correct |
6 |
Correct |
40 ms |
98380 KB |
Output is correct |
7 |
Correct |
42 ms |
98372 KB |
Output is correct |
8 |
Correct |
46 ms |
98416 KB |
Output is correct |
9 |
Correct |
46 ms |
98388 KB |
Output is correct |
10 |
Correct |
42 ms |
98380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
98360 KB |
Output is correct |
2 |
Correct |
45 ms |
98436 KB |
Output is correct |
3 |
Execution timed out |
3098 ms |
111476 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98332 KB |
Output is correct |
2 |
Correct |
37 ms |
98428 KB |
Output is correct |
3 |
Correct |
38 ms |
98400 KB |
Output is correct |
4 |
Correct |
38 ms |
98340 KB |
Output is correct |
5 |
Correct |
46 ms |
98416 KB |
Output is correct |
6 |
Correct |
40 ms |
98380 KB |
Output is correct |
7 |
Correct |
42 ms |
98372 KB |
Output is correct |
8 |
Correct |
46 ms |
98416 KB |
Output is correct |
9 |
Correct |
46 ms |
98388 KB |
Output is correct |
10 |
Correct |
42 ms |
98380 KB |
Output is correct |
11 |
Correct |
41 ms |
98432 KB |
Output is correct |
12 |
Correct |
40 ms |
98396 KB |
Output is correct |
13 |
Correct |
38 ms |
98356 KB |
Output is correct |
14 |
Correct |
42 ms |
98388 KB |
Output is correct |
15 |
Correct |
39 ms |
98380 KB |
Output is correct |
16 |
Correct |
38 ms |
98372 KB |
Output is correct |
17 |
Correct |
39 ms |
98380 KB |
Output is correct |
18 |
Correct |
38 ms |
98352 KB |
Output is correct |
19 |
Correct |
44 ms |
98372 KB |
Output is correct |
20 |
Correct |
40 ms |
98388 KB |
Output is correct |
21 |
Correct |
45 ms |
98344 KB |
Output is correct |
22 |
Correct |
38 ms |
98428 KB |
Output is correct |
23 |
Correct |
41 ms |
98320 KB |
Output is correct |
24 |
Correct |
38 ms |
98440 KB |
Output is correct |
25 |
Correct |
39 ms |
98432 KB |
Output is correct |
26 |
Correct |
41 ms |
98384 KB |
Output is correct |
27 |
Correct |
53 ms |
98340 KB |
Output is correct |
28 |
Correct |
40 ms |
98392 KB |
Output is correct |
29 |
Correct |
37 ms |
98392 KB |
Output is correct |
30 |
Correct |
37 ms |
98388 KB |
Output is correct |
31 |
Correct |
37 ms |
98364 KB |
Output is correct |
32 |
Correct |
38 ms |
98348 KB |
Output is correct |
33 |
Correct |
44 ms |
98416 KB |
Output is correct |
34 |
Correct |
42 ms |
98424 KB |
Output is correct |
35 |
Correct |
38 ms |
98376 KB |
Output is correct |
36 |
Correct |
42 ms |
98344 KB |
Output is correct |
37 |
Correct |
48 ms |
98416 KB |
Output is correct |
38 |
Correct |
40 ms |
98416 KB |
Output is correct |
39 |
Correct |
37 ms |
98368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98332 KB |
Output is correct |
2 |
Correct |
37 ms |
98428 KB |
Output is correct |
3 |
Correct |
38 ms |
98400 KB |
Output is correct |
4 |
Correct |
38 ms |
98340 KB |
Output is correct |
5 |
Correct |
46 ms |
98416 KB |
Output is correct |
6 |
Correct |
40 ms |
98380 KB |
Output is correct |
7 |
Correct |
42 ms |
98372 KB |
Output is correct |
8 |
Correct |
46 ms |
98416 KB |
Output is correct |
9 |
Correct |
46 ms |
98388 KB |
Output is correct |
10 |
Correct |
42 ms |
98380 KB |
Output is correct |
11 |
Correct |
41 ms |
98432 KB |
Output is correct |
12 |
Correct |
40 ms |
98396 KB |
Output is correct |
13 |
Correct |
38 ms |
98356 KB |
Output is correct |
14 |
Correct |
42 ms |
98388 KB |
Output is correct |
15 |
Correct |
39 ms |
98380 KB |
Output is correct |
16 |
Correct |
38 ms |
98372 KB |
Output is correct |
17 |
Correct |
39 ms |
98380 KB |
Output is correct |
18 |
Correct |
38 ms |
98352 KB |
Output is correct |
19 |
Correct |
44 ms |
98372 KB |
Output is correct |
20 |
Correct |
40 ms |
98388 KB |
Output is correct |
21 |
Correct |
45 ms |
98344 KB |
Output is correct |
22 |
Correct |
38 ms |
98428 KB |
Output is correct |
23 |
Correct |
41 ms |
98320 KB |
Output is correct |
24 |
Correct |
38 ms |
98440 KB |
Output is correct |
25 |
Correct |
39 ms |
98432 KB |
Output is correct |
26 |
Correct |
41 ms |
98384 KB |
Output is correct |
27 |
Correct |
53 ms |
98340 KB |
Output is correct |
28 |
Correct |
40 ms |
98392 KB |
Output is correct |
29 |
Correct |
37 ms |
98392 KB |
Output is correct |
30 |
Correct |
37 ms |
98388 KB |
Output is correct |
31 |
Correct |
37 ms |
98364 KB |
Output is correct |
32 |
Correct |
38 ms |
98348 KB |
Output is correct |
33 |
Correct |
44 ms |
98416 KB |
Output is correct |
34 |
Correct |
42 ms |
98424 KB |
Output is correct |
35 |
Correct |
38 ms |
98376 KB |
Output is correct |
36 |
Correct |
42 ms |
98344 KB |
Output is correct |
37 |
Correct |
48 ms |
98416 KB |
Output is correct |
38 |
Correct |
40 ms |
98416 KB |
Output is correct |
39 |
Correct |
37 ms |
98368 KB |
Output is correct |
40 |
Correct |
39 ms |
98440 KB |
Output is correct |
41 |
Correct |
45 ms |
98504 KB |
Output is correct |
42 |
Correct |
44 ms |
98520 KB |
Output is correct |
43 |
Correct |
54 ms |
98692 KB |
Output is correct |
44 |
Correct |
43 ms |
98528 KB |
Output is correct |
45 |
Correct |
44 ms |
98596 KB |
Output is correct |
46 |
Correct |
43 ms |
98552 KB |
Output is correct |
47 |
Correct |
43 ms |
98608 KB |
Output is correct |
48 |
Correct |
43 ms |
98580 KB |
Output is correct |
49 |
Correct |
45 ms |
98512 KB |
Output is correct |
50 |
Correct |
47 ms |
98504 KB |
Output is correct |
51 |
Correct |
46 ms |
98508 KB |
Output is correct |
52 |
Correct |
60 ms |
98528 KB |
Output is correct |
53 |
Correct |
45 ms |
98604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98332 KB |
Output is correct |
2 |
Correct |
37 ms |
98428 KB |
Output is correct |
3 |
Correct |
38 ms |
98400 KB |
Output is correct |
4 |
Correct |
38 ms |
98340 KB |
Output is correct |
5 |
Correct |
46 ms |
98416 KB |
Output is correct |
6 |
Correct |
40 ms |
98380 KB |
Output is correct |
7 |
Correct |
42 ms |
98372 KB |
Output is correct |
8 |
Correct |
46 ms |
98416 KB |
Output is correct |
9 |
Correct |
46 ms |
98388 KB |
Output is correct |
10 |
Correct |
42 ms |
98380 KB |
Output is correct |
11 |
Correct |
41 ms |
98432 KB |
Output is correct |
12 |
Correct |
40 ms |
98396 KB |
Output is correct |
13 |
Correct |
38 ms |
98356 KB |
Output is correct |
14 |
Correct |
42 ms |
98388 KB |
Output is correct |
15 |
Correct |
39 ms |
98380 KB |
Output is correct |
16 |
Correct |
38 ms |
98372 KB |
Output is correct |
17 |
Correct |
39 ms |
98380 KB |
Output is correct |
18 |
Correct |
38 ms |
98352 KB |
Output is correct |
19 |
Correct |
44 ms |
98372 KB |
Output is correct |
20 |
Correct |
40 ms |
98388 KB |
Output is correct |
21 |
Correct |
45 ms |
98344 KB |
Output is correct |
22 |
Correct |
38 ms |
98428 KB |
Output is correct |
23 |
Correct |
41 ms |
98320 KB |
Output is correct |
24 |
Correct |
38 ms |
98440 KB |
Output is correct |
25 |
Correct |
39 ms |
98432 KB |
Output is correct |
26 |
Correct |
41 ms |
98384 KB |
Output is correct |
27 |
Correct |
53 ms |
98340 KB |
Output is correct |
28 |
Correct |
40 ms |
98392 KB |
Output is correct |
29 |
Correct |
37 ms |
98392 KB |
Output is correct |
30 |
Correct |
37 ms |
98388 KB |
Output is correct |
31 |
Correct |
37 ms |
98364 KB |
Output is correct |
32 |
Correct |
38 ms |
98348 KB |
Output is correct |
33 |
Correct |
44 ms |
98416 KB |
Output is correct |
34 |
Correct |
42 ms |
98424 KB |
Output is correct |
35 |
Correct |
38 ms |
98376 KB |
Output is correct |
36 |
Correct |
42 ms |
98344 KB |
Output is correct |
37 |
Correct |
48 ms |
98416 KB |
Output is correct |
38 |
Correct |
40 ms |
98416 KB |
Output is correct |
39 |
Correct |
37 ms |
98368 KB |
Output is correct |
40 |
Correct |
39 ms |
98440 KB |
Output is correct |
41 |
Correct |
45 ms |
98504 KB |
Output is correct |
42 |
Correct |
44 ms |
98520 KB |
Output is correct |
43 |
Correct |
54 ms |
98692 KB |
Output is correct |
44 |
Correct |
43 ms |
98528 KB |
Output is correct |
45 |
Correct |
44 ms |
98596 KB |
Output is correct |
46 |
Correct |
43 ms |
98552 KB |
Output is correct |
47 |
Correct |
43 ms |
98608 KB |
Output is correct |
48 |
Correct |
43 ms |
98580 KB |
Output is correct |
49 |
Correct |
45 ms |
98512 KB |
Output is correct |
50 |
Correct |
47 ms |
98504 KB |
Output is correct |
51 |
Correct |
46 ms |
98508 KB |
Output is correct |
52 |
Correct |
60 ms |
98528 KB |
Output is correct |
53 |
Correct |
45 ms |
98604 KB |
Output is correct |
54 |
Correct |
99 ms |
99144 KB |
Output is correct |
55 |
Correct |
788 ms |
102380 KB |
Output is correct |
56 |
Correct |
798 ms |
102380 KB |
Output is correct |
57 |
Correct |
775 ms |
102384 KB |
Output is correct |
58 |
Correct |
794 ms |
102380 KB |
Output is correct |
59 |
Correct |
775 ms |
102380 KB |
Output is correct |
60 |
Correct |
813 ms |
102384 KB |
Output is correct |
61 |
Correct |
828 ms |
102380 KB |
Output is correct |
62 |
Correct |
868 ms |
102376 KB |
Output is correct |
63 |
Correct |
797 ms |
102380 KB |
Output is correct |
64 |
Correct |
816 ms |
102380 KB |
Output is correct |
65 |
Correct |
772 ms |
102380 KB |
Output is correct |
66 |
Correct |
803 ms |
102380 KB |
Output is correct |
67 |
Correct |
777 ms |
102500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
98332 KB |
Output is correct |
2 |
Correct |
37 ms |
98428 KB |
Output is correct |
3 |
Correct |
38 ms |
98400 KB |
Output is correct |
4 |
Correct |
38 ms |
98340 KB |
Output is correct |
5 |
Correct |
46 ms |
98416 KB |
Output is correct |
6 |
Correct |
40 ms |
98380 KB |
Output is correct |
7 |
Correct |
42 ms |
98372 KB |
Output is correct |
8 |
Correct |
46 ms |
98416 KB |
Output is correct |
9 |
Correct |
46 ms |
98388 KB |
Output is correct |
10 |
Correct |
42 ms |
98380 KB |
Output is correct |
11 |
Correct |
38 ms |
98360 KB |
Output is correct |
12 |
Correct |
45 ms |
98436 KB |
Output is correct |
13 |
Execution timed out |
3098 ms |
111476 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |