#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=2505,mod=(1ll<<61)-1;
int h[N][N],dp[N][N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,a,b,c;
cin>>n;
string s;
cin>>s;
cin>>a>>b>>c;
for(int i=0;i<n;i++){
h[i][i]=s[i]-'a';
//cout<<h[i][i]<<" ";
for(int j=i+1;j<n;j++){
h[i][j]=h[i][j-1]*26+(s[j]-'a');
h[i][j]%=mod;
dp[i][j]=1e18;
//cout<<h[i][j]<<" ";
}
dp[i][i]=a;
//cout<<"\n";
}
for(int sz=1;sz<=n;sz++){
vector <pair <int,int> > v;
for(int l=0;l<=n-sz;l++){
int r=l+sz-1;
dp[l][r]=min(dp[l][r],min(dp[l+1][r],dp[l][r-1])+a);
v.pb({h[l][r],l});
}
sort(all(v));
for(auto x : v){
int pos=x.ss;
int hash=x.ff;
int cnt=1;
while(true){
pair <int,int> p={hash,pos+sz};
auto it=lower_bound(all(v),p);
if(it!=v.end() && it->ff==hash){
pos=it->ss;
cnt++;
dp[x.ss][pos+sz-1]=min(dp[x.ss][pos+sz-1],dp[x.ss][x.ss+sz-1]+b+c*cnt+a*(pos+sz-x.ss-sz*cnt));
}
else break;
}
}
}
cout<<dp[0][n-1]<<"\n";
}
/*
2 5 4 9 15 11
6 7 6 8 12 14
1 0 0 0
1 0 0 0
0 0 0 0
0 1 0 0
0 1 1 0
0 0 0 0
0 1 0 0
0 1 1 0
0 1 0 0
0 0 1 0
0 0 1 1
0 0 0 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
480 ms |
44028 KB |
Output is correct |
4 |
Correct |
594 ms |
49244 KB |
Output is correct |
5 |
Correct |
750 ms |
55128 KB |
Output is correct |
6 |
Correct |
884 ms |
61776 KB |
Output is correct |
7 |
Correct |
1031 ms |
68908 KB |
Output is correct |
8 |
Correct |
1088 ms |
68700 KB |
Output is correct |
9 |
Correct |
1039 ms |
68908 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
0 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4440 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 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 |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
0 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4440 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 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 |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6488 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
3 ms |
9052 KB |
Output is correct |
42 |
Correct |
3 ms |
7260 KB |
Output is correct |
43 |
Correct |
3 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
7168 KB |
Output is correct |
45 |
Correct |
2 ms |
9052 KB |
Output is correct |
46 |
Correct |
2 ms |
7260 KB |
Output is correct |
47 |
Correct |
3 ms |
10588 KB |
Output is correct |
48 |
Correct |
2 ms |
9052 KB |
Output is correct |
49 |
Correct |
2 ms |
7260 KB |
Output is correct |
50 |
Correct |
3 ms |
9052 KB |
Output is correct |
51 |
Correct |
3 ms |
9052 KB |
Output is correct |
52 |
Correct |
4 ms |
9052 KB |
Output is correct |
53 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
0 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4440 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 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 |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6488 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
3 ms |
9052 KB |
Output is correct |
42 |
Correct |
3 ms |
7260 KB |
Output is correct |
43 |
Correct |
3 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
7168 KB |
Output is correct |
45 |
Correct |
2 ms |
9052 KB |
Output is correct |
46 |
Correct |
2 ms |
7260 KB |
Output is correct |
47 |
Correct |
3 ms |
10588 KB |
Output is correct |
48 |
Correct |
2 ms |
9052 KB |
Output is correct |
49 |
Correct |
2 ms |
7260 KB |
Output is correct |
50 |
Correct |
3 ms |
9052 KB |
Output is correct |
51 |
Correct |
3 ms |
9052 KB |
Output is correct |
52 |
Correct |
4 ms |
9052 KB |
Output is correct |
53 |
Correct |
2 ms |
7260 KB |
Output is correct |
54 |
Correct |
9 ms |
11356 KB |
Output is correct |
55 |
Correct |
92 ms |
19928 KB |
Output is correct |
56 |
Correct |
47 ms |
21852 KB |
Output is correct |
57 |
Correct |
44 ms |
20828 KB |
Output is correct |
58 |
Correct |
45 ms |
20884 KB |
Output is correct |
59 |
Correct |
40 ms |
21596 KB |
Output is correct |
60 |
Correct |
43 ms |
20824 KB |
Output is correct |
61 |
Correct |
65 ms |
20888 KB |
Output is correct |
62 |
Correct |
82 ms |
21596 KB |
Output is correct |
63 |
Correct |
56 ms |
20828 KB |
Output is correct |
64 |
Correct |
63 ms |
21076 KB |
Output is correct |
65 |
Correct |
70 ms |
20828 KB |
Output is correct |
66 |
Correct |
75 ms |
21848 KB |
Output is correct |
67 |
Correct |
40 ms |
20828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
480 ms |
44028 KB |
Output is correct |
14 |
Correct |
594 ms |
49244 KB |
Output is correct |
15 |
Correct |
750 ms |
55128 KB |
Output is correct |
16 |
Correct |
884 ms |
61776 KB |
Output is correct |
17 |
Correct |
1031 ms |
68908 KB |
Output is correct |
18 |
Correct |
1088 ms |
68700 KB |
Output is correct |
19 |
Correct |
1039 ms |
68908 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
0 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
0 ms |
2396 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
0 ms |
2652 KB |
Output is correct |
43 |
Correct |
1 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4440 KB |
Output is correct |
45 |
Correct |
1 ms |
4444 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
0 ms |
2396 KB |
Output is correct |
53 |
Correct |
0 ms |
2396 KB |
Output is correct |
54 |
Correct |
0 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
4444 KB |
Output is correct |
56 |
Correct |
1 ms |
6488 KB |
Output is correct |
57 |
Correct |
1 ms |
6492 KB |
Output is correct |
58 |
Correct |
3 ms |
9052 KB |
Output is correct |
59 |
Correct |
3 ms |
7260 KB |
Output is correct |
60 |
Correct |
3 ms |
10588 KB |
Output is correct |
61 |
Correct |
2 ms |
7168 KB |
Output is correct |
62 |
Correct |
2 ms |
9052 KB |
Output is correct |
63 |
Correct |
2 ms |
7260 KB |
Output is correct |
64 |
Correct |
3 ms |
10588 KB |
Output is correct |
65 |
Correct |
2 ms |
9052 KB |
Output is correct |
66 |
Correct |
2 ms |
7260 KB |
Output is correct |
67 |
Correct |
3 ms |
9052 KB |
Output is correct |
68 |
Correct |
3 ms |
9052 KB |
Output is correct |
69 |
Correct |
4 ms |
9052 KB |
Output is correct |
70 |
Correct |
2 ms |
7260 KB |
Output is correct |
71 |
Correct |
9 ms |
11356 KB |
Output is correct |
72 |
Correct |
92 ms |
19928 KB |
Output is correct |
73 |
Correct |
47 ms |
21852 KB |
Output is correct |
74 |
Correct |
44 ms |
20828 KB |
Output is correct |
75 |
Correct |
45 ms |
20884 KB |
Output is correct |
76 |
Correct |
40 ms |
21596 KB |
Output is correct |
77 |
Correct |
43 ms |
20824 KB |
Output is correct |
78 |
Correct |
65 ms |
20888 KB |
Output is correct |
79 |
Correct |
82 ms |
21596 KB |
Output is correct |
80 |
Correct |
56 ms |
20828 KB |
Output is correct |
81 |
Correct |
63 ms |
21076 KB |
Output is correct |
82 |
Correct |
70 ms |
20828 KB |
Output is correct |
83 |
Correct |
75 ms |
21848 KB |
Output is correct |
84 |
Correct |
40 ms |
20828 KB |
Output is correct |
85 |
Correct |
125 ms |
35672 KB |
Output is correct |
86 |
Correct |
371 ms |
68692 KB |
Output is correct |
87 |
Correct |
318 ms |
68692 KB |
Output is correct |
88 |
Correct |
309 ms |
68944 KB |
Output is correct |
89 |
Correct |
303 ms |
68908 KB |
Output is correct |
90 |
Correct |
293 ms |
68972 KB |
Output is correct |
91 |
Correct |
488 ms |
68724 KB |
Output is correct |
92 |
Correct |
551 ms |
68696 KB |
Output is correct |
93 |
Correct |
408 ms |
68916 KB |
Output is correct |
94 |
Correct |
415 ms |
68916 KB |
Output is correct |
95 |
Correct |
540 ms |
68688 KB |
Output is correct |
96 |
Correct |
510 ms |
68688 KB |
Output is correct |
97 |
Correct |
318 ms |
68688 KB |
Output is correct |