#include<bits/stdc++.h>
using namespace std;
const long long maxn=2500+10;
long long lcp[maxn][maxn],nxt[maxn][maxn],n;
long long dp[maxn][maxn],a,b,c,inf=1e15;
string s;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
cin>>a>>b>>c;
for(long long i=n-1;i>=0;i--){
for(long long j=i+1;j<n;j++){
if(s[i]==s[j]){
lcp[i][j]=lcp[i+1][j+1]+1;
}
}
}
for(long long i=0;i<maxn;i++){
for(long long j=0;j<maxn;j++){
nxt[i][j]=n+3;
}
}
for(long long i=0;i<n;i++){
for(long long j=n-1;j>i;j--){
nxt[i][min(lcp[i][j],j-i)]=j;
}
for(long long j=n;j>=0;j--){
nxt[i][j]=min(nxt[i][j],nxt[i][j+1]);
}
}
for(long long i=0;i<maxn;i++){
for(long long j=0;j<maxn;j++){
dp[i][j]=inf;
}
}
for(long long i=n-1;i>=0;i--){
dp[i][i]=a;
for(long long j=i+1;j<n;j++){
dp[i][j]=min(dp[i][j],min(dp[i+1][j],dp[i][j-1])+a);
}
for(long long j=1;j<n;j++){
long long ted=2;
long long z=dp[i][j+i-1];
for(long long h=nxt[i][j];h!=n+3;h=nxt[h][j]){
// cout<<i<<" "<<h<<" "<<j<<" "<<ted<<" "<<z<<" "<<endl;
dp[i][h+j-1]=min(dp[i][h+j-1],z+ted*c+b+a*(h+j-i-(ted*j)));
ted++;
}
}
for(long long j=i+1;j<n;j++){
dp[i][j]=min(dp[i][j],min(dp[i+1][j],dp[i][j-1])+a);
}
}
cout<<dp[0][n-1]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
98908 KB |
Output is correct |
2 |
Correct |
15 ms |
99028 KB |
Output is correct |
3 |
Correct |
15 ms |
98908 KB |
Output is correct |
4 |
Correct |
14 ms |
99040 KB |
Output is correct |
5 |
Correct |
15 ms |
98908 KB |
Output is correct |
6 |
Correct |
16 ms |
98908 KB |
Output is correct |
7 |
Correct |
15 ms |
98908 KB |
Output is correct |
8 |
Correct |
15 ms |
98908 KB |
Output is correct |
9 |
Correct |
14 ms |
99052 KB |
Output is correct |
10 |
Correct |
15 ms |
98908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
99164 KB |
Output is correct |
2 |
Correct |
15 ms |
99100 KB |
Output is correct |
3 |
Correct |
128 ms |
134440 KB |
Output is correct |
4 |
Correct |
145 ms |
136456 KB |
Output is correct |
5 |
Correct |
172 ms |
140112 KB |
Output is correct |
6 |
Correct |
207 ms |
144208 KB |
Output is correct |
7 |
Correct |
243 ms |
146724 KB |
Output is correct |
8 |
Correct |
243 ms |
146728 KB |
Output is correct |
9 |
Correct |
250 ms |
146768 KB |
Output is correct |
10 |
Correct |
13 ms |
98904 KB |
Output is correct |
11 |
Correct |
17 ms |
98908 KB |
Output is correct |
12 |
Correct |
14 ms |
98908 KB |
Output is correct |
13 |
Correct |
14 ms |
99104 KB |
Output is correct |
14 |
Correct |
14 ms |
99140 KB |
Output is correct |
15 |
Correct |
14 ms |
99416 KB |
Output is correct |
16 |
Correct |
14 ms |
99180 KB |
Output is correct |
17 |
Correct |
15 ms |
98908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
98908 KB |
Output is correct |
2 |
Correct |
15 ms |
99028 KB |
Output is correct |
3 |
Correct |
15 ms |
98908 KB |
Output is correct |
4 |
Correct |
14 ms |
99040 KB |
Output is correct |
5 |
Correct |
15 ms |
98908 KB |
Output is correct |
6 |
Correct |
16 ms |
98908 KB |
Output is correct |
7 |
Correct |
15 ms |
98908 KB |
Output is correct |
8 |
Correct |
15 ms |
98908 KB |
Output is correct |
9 |
Correct |
14 ms |
99052 KB |
Output is correct |
10 |
Correct |
15 ms |
98908 KB |
Output is correct |
11 |
Correct |
15 ms |
98904 KB |
Output is correct |
12 |
Correct |
14 ms |
99172 KB |
Output is correct |
13 |
Correct |
14 ms |
99164 KB |
Output is correct |
14 |
Correct |
15 ms |
98908 KB |
Output is correct |
15 |
Correct |
16 ms |
99160 KB |
Output is correct |
16 |
Correct |
14 ms |
98904 KB |
Output is correct |
17 |
Correct |
14 ms |
99076 KB |
Output is correct |
18 |
Correct |
14 ms |
98908 KB |
Output is correct |
19 |
Correct |
15 ms |
98908 KB |
Output is correct |
20 |
Correct |
15 ms |
98908 KB |
Output is correct |
21 |
Correct |
14 ms |
99164 KB |
Output is correct |
22 |
Correct |
14 ms |
99164 KB |
Output is correct |
23 |
Correct |
14 ms |
98968 KB |
Output is correct |
24 |
Correct |
15 ms |
99160 KB |
Output is correct |
25 |
Correct |
15 ms |
99024 KB |
Output is correct |
26 |
Correct |
16 ms |
99160 KB |
Output is correct |
27 |
Correct |
14 ms |
99164 KB |
Output is correct |
28 |
Correct |
14 ms |
99196 KB |
Output is correct |
29 |
Correct |
16 ms |
99164 KB |
Output is correct |
30 |
Correct |
14 ms |
99164 KB |
Output is correct |
31 |
Correct |
14 ms |
99140 KB |
Output is correct |
32 |
Correct |
15 ms |
99164 KB |
Output is correct |
33 |
Correct |
15 ms |
98908 KB |
Output is correct |
34 |
Correct |
14 ms |
99052 KB |
Output is correct |
35 |
Correct |
15 ms |
98908 KB |
Output is correct |
36 |
Correct |
15 ms |
98908 KB |
Output is correct |
37 |
Correct |
14 ms |
99072 KB |
Output is correct |
38 |
Correct |
16 ms |
99164 KB |
Output is correct |
39 |
Correct |
14 ms |
99164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
98908 KB |
Output is correct |
2 |
Correct |
15 ms |
99028 KB |
Output is correct |
3 |
Correct |
15 ms |
98908 KB |
Output is correct |
4 |
Correct |
14 ms |
99040 KB |
Output is correct |
5 |
Correct |
15 ms |
98908 KB |
Output is correct |
6 |
Correct |
16 ms |
98908 KB |
Output is correct |
7 |
Correct |
15 ms |
98908 KB |
Output is correct |
8 |
Correct |
15 ms |
98908 KB |
Output is correct |
9 |
Correct |
14 ms |
99052 KB |
Output is correct |
10 |
Correct |
15 ms |
98908 KB |
Output is correct |
11 |
Correct |
15 ms |
98904 KB |
Output is correct |
12 |
Correct |
14 ms |
99172 KB |
Output is correct |
13 |
Correct |
14 ms |
99164 KB |
Output is correct |
14 |
Correct |
15 ms |
98908 KB |
Output is correct |
15 |
Correct |
16 ms |
99160 KB |
Output is correct |
16 |
Correct |
14 ms |
98904 KB |
Output is correct |
17 |
Correct |
14 ms |
99076 KB |
Output is correct |
18 |
Correct |
14 ms |
98908 KB |
Output is correct |
19 |
Correct |
15 ms |
98908 KB |
Output is correct |
20 |
Correct |
15 ms |
98908 KB |
Output is correct |
21 |
Correct |
14 ms |
99164 KB |
Output is correct |
22 |
Correct |
14 ms |
99164 KB |
Output is correct |
23 |
Correct |
14 ms |
98968 KB |
Output is correct |
24 |
Correct |
15 ms |
99160 KB |
Output is correct |
25 |
Correct |
15 ms |
99024 KB |
Output is correct |
26 |
Correct |
16 ms |
99160 KB |
Output is correct |
27 |
Correct |
14 ms |
99164 KB |
Output is correct |
28 |
Correct |
14 ms |
99196 KB |
Output is correct |
29 |
Correct |
16 ms |
99164 KB |
Output is correct |
30 |
Correct |
14 ms |
99164 KB |
Output is correct |
31 |
Correct |
14 ms |
99140 KB |
Output is correct |
32 |
Correct |
15 ms |
99164 KB |
Output is correct |
33 |
Correct |
15 ms |
98908 KB |
Output is correct |
34 |
Correct |
14 ms |
99052 KB |
Output is correct |
35 |
Correct |
15 ms |
98908 KB |
Output is correct |
36 |
Correct |
15 ms |
98908 KB |
Output is correct |
37 |
Correct |
14 ms |
99072 KB |
Output is correct |
38 |
Correct |
16 ms |
99164 KB |
Output is correct |
39 |
Correct |
14 ms |
99164 KB |
Output is correct |
40 |
Correct |
15 ms |
99420 KB |
Output is correct |
41 |
Correct |
15 ms |
101580 KB |
Output is correct |
42 |
Correct |
14 ms |
101468 KB |
Output is correct |
43 |
Correct |
15 ms |
101464 KB |
Output is correct |
44 |
Correct |
15 ms |
101464 KB |
Output is correct |
45 |
Correct |
17 ms |
101448 KB |
Output is correct |
46 |
Correct |
16 ms |
101388 KB |
Output is correct |
47 |
Correct |
18 ms |
101468 KB |
Output is correct |
48 |
Correct |
14 ms |
101468 KB |
Output is correct |
49 |
Correct |
15 ms |
101468 KB |
Output is correct |
50 |
Correct |
16 ms |
101468 KB |
Output is correct |
51 |
Correct |
16 ms |
101504 KB |
Output is correct |
52 |
Incorrect |
15 ms |
101464 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
98908 KB |
Output is correct |
2 |
Correct |
15 ms |
99028 KB |
Output is correct |
3 |
Correct |
15 ms |
98908 KB |
Output is correct |
4 |
Correct |
14 ms |
99040 KB |
Output is correct |
5 |
Correct |
15 ms |
98908 KB |
Output is correct |
6 |
Correct |
16 ms |
98908 KB |
Output is correct |
7 |
Correct |
15 ms |
98908 KB |
Output is correct |
8 |
Correct |
15 ms |
98908 KB |
Output is correct |
9 |
Correct |
14 ms |
99052 KB |
Output is correct |
10 |
Correct |
15 ms |
98908 KB |
Output is correct |
11 |
Correct |
15 ms |
98904 KB |
Output is correct |
12 |
Correct |
14 ms |
99172 KB |
Output is correct |
13 |
Correct |
14 ms |
99164 KB |
Output is correct |
14 |
Correct |
15 ms |
98908 KB |
Output is correct |
15 |
Correct |
16 ms |
99160 KB |
Output is correct |
16 |
Correct |
14 ms |
98904 KB |
Output is correct |
17 |
Correct |
14 ms |
99076 KB |
Output is correct |
18 |
Correct |
14 ms |
98908 KB |
Output is correct |
19 |
Correct |
15 ms |
98908 KB |
Output is correct |
20 |
Correct |
15 ms |
98908 KB |
Output is correct |
21 |
Correct |
14 ms |
99164 KB |
Output is correct |
22 |
Correct |
14 ms |
99164 KB |
Output is correct |
23 |
Correct |
14 ms |
98968 KB |
Output is correct |
24 |
Correct |
15 ms |
99160 KB |
Output is correct |
25 |
Correct |
15 ms |
99024 KB |
Output is correct |
26 |
Correct |
16 ms |
99160 KB |
Output is correct |
27 |
Correct |
14 ms |
99164 KB |
Output is correct |
28 |
Correct |
14 ms |
99196 KB |
Output is correct |
29 |
Correct |
16 ms |
99164 KB |
Output is correct |
30 |
Correct |
14 ms |
99164 KB |
Output is correct |
31 |
Correct |
14 ms |
99140 KB |
Output is correct |
32 |
Correct |
15 ms |
99164 KB |
Output is correct |
33 |
Correct |
15 ms |
98908 KB |
Output is correct |
34 |
Correct |
14 ms |
99052 KB |
Output is correct |
35 |
Correct |
15 ms |
98908 KB |
Output is correct |
36 |
Correct |
15 ms |
98908 KB |
Output is correct |
37 |
Correct |
14 ms |
99072 KB |
Output is correct |
38 |
Correct |
16 ms |
99164 KB |
Output is correct |
39 |
Correct |
14 ms |
99164 KB |
Output is correct |
40 |
Correct |
15 ms |
99420 KB |
Output is correct |
41 |
Correct |
15 ms |
101580 KB |
Output is correct |
42 |
Correct |
14 ms |
101468 KB |
Output is correct |
43 |
Correct |
15 ms |
101464 KB |
Output is correct |
44 |
Correct |
15 ms |
101464 KB |
Output is correct |
45 |
Correct |
17 ms |
101448 KB |
Output is correct |
46 |
Correct |
16 ms |
101388 KB |
Output is correct |
47 |
Correct |
18 ms |
101468 KB |
Output is correct |
48 |
Correct |
14 ms |
101468 KB |
Output is correct |
49 |
Correct |
15 ms |
101468 KB |
Output is correct |
50 |
Correct |
16 ms |
101468 KB |
Output is correct |
51 |
Correct |
16 ms |
101504 KB |
Output is correct |
52 |
Incorrect |
15 ms |
101464 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
98908 KB |
Output is correct |
2 |
Correct |
15 ms |
99028 KB |
Output is correct |
3 |
Correct |
15 ms |
98908 KB |
Output is correct |
4 |
Correct |
14 ms |
99040 KB |
Output is correct |
5 |
Correct |
15 ms |
98908 KB |
Output is correct |
6 |
Correct |
16 ms |
98908 KB |
Output is correct |
7 |
Correct |
15 ms |
98908 KB |
Output is correct |
8 |
Correct |
15 ms |
98908 KB |
Output is correct |
9 |
Correct |
14 ms |
99052 KB |
Output is correct |
10 |
Correct |
15 ms |
98908 KB |
Output is correct |
11 |
Correct |
14 ms |
99164 KB |
Output is correct |
12 |
Correct |
15 ms |
99100 KB |
Output is correct |
13 |
Correct |
128 ms |
134440 KB |
Output is correct |
14 |
Correct |
145 ms |
136456 KB |
Output is correct |
15 |
Correct |
172 ms |
140112 KB |
Output is correct |
16 |
Correct |
207 ms |
144208 KB |
Output is correct |
17 |
Correct |
243 ms |
146724 KB |
Output is correct |
18 |
Correct |
243 ms |
146728 KB |
Output is correct |
19 |
Correct |
250 ms |
146768 KB |
Output is correct |
20 |
Correct |
13 ms |
98904 KB |
Output is correct |
21 |
Correct |
17 ms |
98908 KB |
Output is correct |
22 |
Correct |
14 ms |
98908 KB |
Output is correct |
23 |
Correct |
14 ms |
99104 KB |
Output is correct |
24 |
Correct |
14 ms |
99140 KB |
Output is correct |
25 |
Correct |
14 ms |
99416 KB |
Output is correct |
26 |
Correct |
14 ms |
99180 KB |
Output is correct |
27 |
Correct |
15 ms |
98908 KB |
Output is correct |
28 |
Correct |
15 ms |
98904 KB |
Output is correct |
29 |
Correct |
14 ms |
99172 KB |
Output is correct |
30 |
Correct |
14 ms |
99164 KB |
Output is correct |
31 |
Correct |
15 ms |
98908 KB |
Output is correct |
32 |
Correct |
16 ms |
99160 KB |
Output is correct |
33 |
Correct |
14 ms |
98904 KB |
Output is correct |
34 |
Correct |
14 ms |
99076 KB |
Output is correct |
35 |
Correct |
14 ms |
98908 KB |
Output is correct |
36 |
Correct |
15 ms |
98908 KB |
Output is correct |
37 |
Correct |
15 ms |
98908 KB |
Output is correct |
38 |
Correct |
14 ms |
99164 KB |
Output is correct |
39 |
Correct |
14 ms |
99164 KB |
Output is correct |
40 |
Correct |
14 ms |
98968 KB |
Output is correct |
41 |
Correct |
15 ms |
99160 KB |
Output is correct |
42 |
Correct |
15 ms |
99024 KB |
Output is correct |
43 |
Correct |
16 ms |
99160 KB |
Output is correct |
44 |
Correct |
14 ms |
99164 KB |
Output is correct |
45 |
Correct |
14 ms |
99196 KB |
Output is correct |
46 |
Correct |
16 ms |
99164 KB |
Output is correct |
47 |
Correct |
14 ms |
99164 KB |
Output is correct |
48 |
Correct |
14 ms |
99140 KB |
Output is correct |
49 |
Correct |
15 ms |
99164 KB |
Output is correct |
50 |
Correct |
15 ms |
98908 KB |
Output is correct |
51 |
Correct |
14 ms |
99052 KB |
Output is correct |
52 |
Correct |
15 ms |
98908 KB |
Output is correct |
53 |
Correct |
15 ms |
98908 KB |
Output is correct |
54 |
Correct |
14 ms |
99072 KB |
Output is correct |
55 |
Correct |
16 ms |
99164 KB |
Output is correct |
56 |
Correct |
14 ms |
99164 KB |
Output is correct |
57 |
Correct |
15 ms |
99420 KB |
Output is correct |
58 |
Correct |
15 ms |
101580 KB |
Output is correct |
59 |
Correct |
14 ms |
101468 KB |
Output is correct |
60 |
Correct |
15 ms |
101464 KB |
Output is correct |
61 |
Correct |
15 ms |
101464 KB |
Output is correct |
62 |
Correct |
17 ms |
101448 KB |
Output is correct |
63 |
Correct |
16 ms |
101388 KB |
Output is correct |
64 |
Correct |
18 ms |
101468 KB |
Output is correct |
65 |
Correct |
14 ms |
101468 KB |
Output is correct |
66 |
Correct |
15 ms |
101468 KB |
Output is correct |
67 |
Correct |
16 ms |
101468 KB |
Output is correct |
68 |
Correct |
16 ms |
101504 KB |
Output is correct |
69 |
Incorrect |
15 ms |
101464 KB |
Output isn't correct |
70 |
Halted |
0 ms |
0 KB |
- |