#include<bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
const int N=2505, M=1e9+7;
int n, h[N], t[N], tt[N][N];
ll A, B, C, dp[N][N], pd[N][N], o[N];
string s;
void opr(const int &j, vector<int>&v){
v.push_back(n);
int k=v.size()-1;
for(int i=v.size()-2;i>=0;--i){
while(k>0 && v[i]+j<v[k-1])
--k;
t[v[i]]=v[k];
}
}
int main(){
cin>>n>>s>>A>>B>>C;
if(n>200){
for(int i=1;i<=n;++i){
o[i]=A*i;
for(int j=1;j<i;++j)
o[i]=min(o[i], o[j]+B+C*(i/j)+(i%j)*A);
}
cout<<o[n]<<'\n';
return 0;
}
for(int i=0;i<=n;++i){
fill(dp[i], dp[i]+n+1, 1e18);
}
for(int i=0;i<=n;++i){
for(int j=i;j<=n;++j)
dp[i][j]=A*(j-i);
}
for(int j=0;j<n;++j){
for(int i=0;i<=n;++i){
for(int k=i;k<=n;++k)
pd[i][k]=(k-i)*A;
}
for(int i=0;i<n-j;++i)
h[i]=(1LL*h[i]*37+s[i+j]-'a')%M;
vector<pii>v;
vector<int>w;
for(int i=0;i<n-j;++i)
v.push_back({h[i], i});
sort(all(v));
w.push_back(v[0].S);
for(int i=1;i<n-j;++i){
if(v[i].F==v[i-1].F)
w.push_back(v[i].S);
else{
opr(j, w);
w.clear();
w.push_back(v[i].S);
}
}
opr(j, w);
memset(tt, 0, sizeof(tt));
for(int i=n-j-1;i>=0;--i){
for(int k=i+j+1;k<=n;++k){
tt[i][k]=1;
if(t[i]<k)
tt[i][k]+=tt[t[i]][k];
dp[i][k]=min(dp[i][k], dp[i][i+j+1]+B+C*tt[i][k]+(k-i-tt[i][k]*(j+1))*A);
}
}
for(int i=0;i<=n;++i){
for(int k=i+1;k<=n;++k)
dp[i][k]=min(dp[i][k], dp[i+1][k]+A);
}
}
cout<<dp[0][n]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24788 KB |
Output is correct |
2 |
Correct |
15 ms |
24912 KB |
Output is correct |
3 |
Correct |
15 ms |
24788 KB |
Output is correct |
4 |
Correct |
18 ms |
24912 KB |
Output is correct |
5 |
Correct |
19 ms |
24916 KB |
Output is correct |
6 |
Correct |
18 ms |
24788 KB |
Output is correct |
7 |
Correct |
17 ms |
24916 KB |
Output is correct |
8 |
Correct |
16 ms |
24912 KB |
Output is correct |
9 |
Correct |
17 ms |
24852 KB |
Output is correct |
10 |
Correct |
18 ms |
24796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
25008 KB |
Output is correct |
2 |
Correct |
18 ms |
24844 KB |
Output is correct |
3 |
Correct |
8 ms |
328 KB |
Output is correct |
4 |
Correct |
10 ms |
336 KB |
Output is correct |
5 |
Correct |
12 ms |
340 KB |
Output is correct |
6 |
Correct |
15 ms |
344 KB |
Output is correct |
7 |
Correct |
15 ms |
340 KB |
Output is correct |
8 |
Correct |
16 ms |
340 KB |
Output is correct |
9 |
Correct |
16 ms |
340 KB |
Output is correct |
10 |
Correct |
11 ms |
24788 KB |
Output is correct |
11 |
Correct |
13 ms |
24816 KB |
Output is correct |
12 |
Correct |
17 ms |
24916 KB |
Output is correct |
13 |
Correct |
22 ms |
24932 KB |
Output is correct |
14 |
Correct |
28 ms |
24868 KB |
Output is correct |
15 |
Correct |
41 ms |
24916 KB |
Output is correct |
16 |
Correct |
84 ms |
25144 KB |
Output is correct |
17 |
Correct |
16 ms |
24916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24788 KB |
Output is correct |
2 |
Correct |
15 ms |
24912 KB |
Output is correct |
3 |
Correct |
15 ms |
24788 KB |
Output is correct |
4 |
Correct |
18 ms |
24912 KB |
Output is correct |
5 |
Correct |
19 ms |
24916 KB |
Output is correct |
6 |
Correct |
18 ms |
24788 KB |
Output is correct |
7 |
Correct |
17 ms |
24916 KB |
Output is correct |
8 |
Correct |
16 ms |
24912 KB |
Output is correct |
9 |
Correct |
17 ms |
24852 KB |
Output is correct |
10 |
Correct |
18 ms |
24796 KB |
Output is correct |
11 |
Correct |
35 ms |
24916 KB |
Output is correct |
12 |
Correct |
48 ms |
25004 KB |
Output is correct |
13 |
Correct |
55 ms |
25044 KB |
Output is correct |
14 |
Correct |
30 ms |
24952 KB |
Output is correct |
15 |
Correct |
60 ms |
25040 KB |
Output is correct |
16 |
Correct |
43 ms |
24964 KB |
Output is correct |
17 |
Correct |
23 ms |
24876 KB |
Output is correct |
18 |
Correct |
12 ms |
24788 KB |
Output is correct |
19 |
Correct |
46 ms |
24916 KB |
Output is correct |
20 |
Correct |
23 ms |
24904 KB |
Output is correct |
21 |
Correct |
91 ms |
25128 KB |
Output is correct |
22 |
Correct |
97 ms |
25044 KB |
Output is correct |
23 |
Correct |
94 ms |
25044 KB |
Output is correct |
24 |
Correct |
84 ms |
25152 KB |
Output is correct |
25 |
Correct |
87 ms |
25132 KB |
Output is correct |
26 |
Correct |
83 ms |
25044 KB |
Output is correct |
27 |
Correct |
87 ms |
25128 KB |
Output is correct |
28 |
Correct |
95 ms |
25144 KB |
Output is correct |
29 |
Correct |
88 ms |
25144 KB |
Output is correct |
30 |
Correct |
85 ms |
25132 KB |
Output is correct |
31 |
Correct |
83 ms |
25136 KB |
Output is correct |
32 |
Correct |
85 ms |
25144 KB |
Output is correct |
33 |
Correct |
86 ms |
25132 KB |
Output is correct |
34 |
Correct |
13 ms |
24772 KB |
Output is correct |
35 |
Correct |
14 ms |
24796 KB |
Output is correct |
36 |
Correct |
26 ms |
24908 KB |
Output is correct |
37 |
Correct |
30 ms |
24916 KB |
Output is correct |
38 |
Correct |
40 ms |
24980 KB |
Output is correct |
39 |
Correct |
83 ms |
25044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24788 KB |
Output is correct |
2 |
Correct |
15 ms |
24912 KB |
Output is correct |
3 |
Correct |
15 ms |
24788 KB |
Output is correct |
4 |
Correct |
18 ms |
24912 KB |
Output is correct |
5 |
Correct |
19 ms |
24916 KB |
Output is correct |
6 |
Correct |
18 ms |
24788 KB |
Output is correct |
7 |
Correct |
17 ms |
24916 KB |
Output is correct |
8 |
Correct |
16 ms |
24912 KB |
Output is correct |
9 |
Correct |
17 ms |
24852 KB |
Output is correct |
10 |
Correct |
18 ms |
24796 KB |
Output is correct |
11 |
Correct |
35 ms |
24916 KB |
Output is correct |
12 |
Correct |
48 ms |
25004 KB |
Output is correct |
13 |
Correct |
55 ms |
25044 KB |
Output is correct |
14 |
Correct |
30 ms |
24952 KB |
Output is correct |
15 |
Correct |
60 ms |
25040 KB |
Output is correct |
16 |
Correct |
43 ms |
24964 KB |
Output is correct |
17 |
Correct |
23 ms |
24876 KB |
Output is correct |
18 |
Correct |
12 ms |
24788 KB |
Output is correct |
19 |
Correct |
46 ms |
24916 KB |
Output is correct |
20 |
Correct |
23 ms |
24904 KB |
Output is correct |
21 |
Correct |
91 ms |
25128 KB |
Output is correct |
22 |
Correct |
97 ms |
25044 KB |
Output is correct |
23 |
Correct |
94 ms |
25044 KB |
Output is correct |
24 |
Correct |
84 ms |
25152 KB |
Output is correct |
25 |
Correct |
87 ms |
25132 KB |
Output is correct |
26 |
Correct |
83 ms |
25044 KB |
Output is correct |
27 |
Correct |
87 ms |
25128 KB |
Output is correct |
28 |
Correct |
95 ms |
25144 KB |
Output is correct |
29 |
Correct |
88 ms |
25144 KB |
Output is correct |
30 |
Correct |
85 ms |
25132 KB |
Output is correct |
31 |
Correct |
83 ms |
25136 KB |
Output is correct |
32 |
Correct |
85 ms |
25144 KB |
Output is correct |
33 |
Correct |
86 ms |
25132 KB |
Output is correct |
34 |
Correct |
13 ms |
24772 KB |
Output is correct |
35 |
Correct |
14 ms |
24796 KB |
Output is correct |
36 |
Correct |
26 ms |
24908 KB |
Output is correct |
37 |
Correct |
30 ms |
24916 KB |
Output is correct |
38 |
Correct |
40 ms |
24980 KB |
Output is correct |
39 |
Correct |
83 ms |
25044 KB |
Output is correct |
40 |
Correct |
251 ms |
25652 KB |
Output is correct |
41 |
Correct |
549 ms |
26972 KB |
Output is correct |
42 |
Correct |
588 ms |
27004 KB |
Output is correct |
43 |
Correct |
559 ms |
26964 KB |
Output is correct |
44 |
Correct |
537 ms |
26972 KB |
Output is correct |
45 |
Correct |
556 ms |
26972 KB |
Output is correct |
46 |
Correct |
559 ms |
26972 KB |
Output is correct |
47 |
Correct |
547 ms |
26880 KB |
Output is correct |
48 |
Correct |
543 ms |
26972 KB |
Output is correct |
49 |
Correct |
535 ms |
26968 KB |
Output is correct |
50 |
Correct |
542 ms |
26968 KB |
Output is correct |
51 |
Correct |
577 ms |
26964 KB |
Output is correct |
52 |
Correct |
556 ms |
26964 KB |
Output is correct |
53 |
Correct |
544 ms |
26968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24788 KB |
Output is correct |
2 |
Correct |
15 ms |
24912 KB |
Output is correct |
3 |
Correct |
15 ms |
24788 KB |
Output is correct |
4 |
Correct |
18 ms |
24912 KB |
Output is correct |
5 |
Correct |
19 ms |
24916 KB |
Output is correct |
6 |
Correct |
18 ms |
24788 KB |
Output is correct |
7 |
Correct |
17 ms |
24916 KB |
Output is correct |
8 |
Correct |
16 ms |
24912 KB |
Output is correct |
9 |
Correct |
17 ms |
24852 KB |
Output is correct |
10 |
Correct |
18 ms |
24796 KB |
Output is correct |
11 |
Correct |
35 ms |
24916 KB |
Output is correct |
12 |
Correct |
48 ms |
25004 KB |
Output is correct |
13 |
Correct |
55 ms |
25044 KB |
Output is correct |
14 |
Correct |
30 ms |
24952 KB |
Output is correct |
15 |
Correct |
60 ms |
25040 KB |
Output is correct |
16 |
Correct |
43 ms |
24964 KB |
Output is correct |
17 |
Correct |
23 ms |
24876 KB |
Output is correct |
18 |
Correct |
12 ms |
24788 KB |
Output is correct |
19 |
Correct |
46 ms |
24916 KB |
Output is correct |
20 |
Correct |
23 ms |
24904 KB |
Output is correct |
21 |
Correct |
91 ms |
25128 KB |
Output is correct |
22 |
Correct |
97 ms |
25044 KB |
Output is correct |
23 |
Correct |
94 ms |
25044 KB |
Output is correct |
24 |
Correct |
84 ms |
25152 KB |
Output is correct |
25 |
Correct |
87 ms |
25132 KB |
Output is correct |
26 |
Correct |
83 ms |
25044 KB |
Output is correct |
27 |
Correct |
87 ms |
25128 KB |
Output is correct |
28 |
Correct |
95 ms |
25144 KB |
Output is correct |
29 |
Correct |
88 ms |
25144 KB |
Output is correct |
30 |
Correct |
85 ms |
25132 KB |
Output is correct |
31 |
Correct |
83 ms |
25136 KB |
Output is correct |
32 |
Correct |
85 ms |
25144 KB |
Output is correct |
33 |
Correct |
86 ms |
25132 KB |
Output is correct |
34 |
Correct |
13 ms |
24772 KB |
Output is correct |
35 |
Correct |
14 ms |
24796 KB |
Output is correct |
36 |
Correct |
26 ms |
24908 KB |
Output is correct |
37 |
Correct |
30 ms |
24916 KB |
Output is correct |
38 |
Correct |
40 ms |
24980 KB |
Output is correct |
39 |
Correct |
83 ms |
25044 KB |
Output is correct |
40 |
Correct |
251 ms |
25652 KB |
Output is correct |
41 |
Correct |
549 ms |
26972 KB |
Output is correct |
42 |
Correct |
588 ms |
27004 KB |
Output is correct |
43 |
Correct |
559 ms |
26964 KB |
Output is correct |
44 |
Correct |
537 ms |
26972 KB |
Output is correct |
45 |
Correct |
556 ms |
26972 KB |
Output is correct |
46 |
Correct |
559 ms |
26972 KB |
Output is correct |
47 |
Correct |
547 ms |
26880 KB |
Output is correct |
48 |
Correct |
543 ms |
26972 KB |
Output is correct |
49 |
Correct |
535 ms |
26968 KB |
Output is correct |
50 |
Correct |
542 ms |
26968 KB |
Output is correct |
51 |
Correct |
577 ms |
26964 KB |
Output is correct |
52 |
Correct |
556 ms |
26964 KB |
Output is correct |
53 |
Correct |
544 ms |
26968 KB |
Output is correct |
54 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24788 KB |
Output is correct |
2 |
Correct |
15 ms |
24912 KB |
Output is correct |
3 |
Correct |
15 ms |
24788 KB |
Output is correct |
4 |
Correct |
18 ms |
24912 KB |
Output is correct |
5 |
Correct |
19 ms |
24916 KB |
Output is correct |
6 |
Correct |
18 ms |
24788 KB |
Output is correct |
7 |
Correct |
17 ms |
24916 KB |
Output is correct |
8 |
Correct |
16 ms |
24912 KB |
Output is correct |
9 |
Correct |
17 ms |
24852 KB |
Output is correct |
10 |
Correct |
18 ms |
24796 KB |
Output is correct |
11 |
Correct |
50 ms |
25008 KB |
Output is correct |
12 |
Correct |
18 ms |
24844 KB |
Output is correct |
13 |
Correct |
8 ms |
328 KB |
Output is correct |
14 |
Correct |
10 ms |
336 KB |
Output is correct |
15 |
Correct |
12 ms |
340 KB |
Output is correct |
16 |
Correct |
15 ms |
344 KB |
Output is correct |
17 |
Correct |
15 ms |
340 KB |
Output is correct |
18 |
Correct |
16 ms |
340 KB |
Output is correct |
19 |
Correct |
16 ms |
340 KB |
Output is correct |
20 |
Correct |
11 ms |
24788 KB |
Output is correct |
21 |
Correct |
13 ms |
24816 KB |
Output is correct |
22 |
Correct |
17 ms |
24916 KB |
Output is correct |
23 |
Correct |
22 ms |
24932 KB |
Output is correct |
24 |
Correct |
28 ms |
24868 KB |
Output is correct |
25 |
Correct |
41 ms |
24916 KB |
Output is correct |
26 |
Correct |
84 ms |
25144 KB |
Output is correct |
27 |
Correct |
16 ms |
24916 KB |
Output is correct |
28 |
Correct |
35 ms |
24916 KB |
Output is correct |
29 |
Correct |
48 ms |
25004 KB |
Output is correct |
30 |
Correct |
55 ms |
25044 KB |
Output is correct |
31 |
Correct |
30 ms |
24952 KB |
Output is correct |
32 |
Correct |
60 ms |
25040 KB |
Output is correct |
33 |
Correct |
43 ms |
24964 KB |
Output is correct |
34 |
Correct |
23 ms |
24876 KB |
Output is correct |
35 |
Correct |
12 ms |
24788 KB |
Output is correct |
36 |
Correct |
46 ms |
24916 KB |
Output is correct |
37 |
Correct |
23 ms |
24904 KB |
Output is correct |
38 |
Correct |
91 ms |
25128 KB |
Output is correct |
39 |
Correct |
97 ms |
25044 KB |
Output is correct |
40 |
Correct |
94 ms |
25044 KB |
Output is correct |
41 |
Correct |
84 ms |
25152 KB |
Output is correct |
42 |
Correct |
87 ms |
25132 KB |
Output is correct |
43 |
Correct |
83 ms |
25044 KB |
Output is correct |
44 |
Correct |
87 ms |
25128 KB |
Output is correct |
45 |
Correct |
95 ms |
25144 KB |
Output is correct |
46 |
Correct |
88 ms |
25144 KB |
Output is correct |
47 |
Correct |
85 ms |
25132 KB |
Output is correct |
48 |
Correct |
83 ms |
25136 KB |
Output is correct |
49 |
Correct |
85 ms |
25144 KB |
Output is correct |
50 |
Correct |
86 ms |
25132 KB |
Output is correct |
51 |
Correct |
13 ms |
24772 KB |
Output is correct |
52 |
Correct |
14 ms |
24796 KB |
Output is correct |
53 |
Correct |
26 ms |
24908 KB |
Output is correct |
54 |
Correct |
30 ms |
24916 KB |
Output is correct |
55 |
Correct |
40 ms |
24980 KB |
Output is correct |
56 |
Correct |
83 ms |
25044 KB |
Output is correct |
57 |
Correct |
251 ms |
25652 KB |
Output is correct |
58 |
Correct |
549 ms |
26972 KB |
Output is correct |
59 |
Correct |
588 ms |
27004 KB |
Output is correct |
60 |
Correct |
559 ms |
26964 KB |
Output is correct |
61 |
Correct |
537 ms |
26972 KB |
Output is correct |
62 |
Correct |
556 ms |
26972 KB |
Output is correct |
63 |
Correct |
559 ms |
26972 KB |
Output is correct |
64 |
Correct |
547 ms |
26880 KB |
Output is correct |
65 |
Correct |
543 ms |
26972 KB |
Output is correct |
66 |
Correct |
535 ms |
26968 KB |
Output is correct |
67 |
Correct |
542 ms |
26968 KB |
Output is correct |
68 |
Correct |
577 ms |
26964 KB |
Output is correct |
69 |
Correct |
556 ms |
26964 KB |
Output is correct |
70 |
Correct |
544 ms |
26968 KB |
Output is correct |
71 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
72 |
Halted |
0 ms |
0 KB |
- |