#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll INF = 1e18;
ll dp[2500][2500];
int nxt[2500][2500];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
int n;
string s;
ll a,b,c;
cin>>n>>s>>a>>b>>c;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
dp[i][j] = INF;
nxt[i][j] = n;
}
}
for(int i=0;i<n;i++){
vector<int>z(n,0);
vector<bool>hav(n,0);
string t = s.substr(i,n-i);
int cur = 1;
int l = 0,r = 0;
//cout<<"at"<<" "<<i<<'\n';
for(int j=1;j<n-i;j++){
if(j<=r)z[j] = min(r-j+1,z[j-l]);
while(j+z[j]<n-i && t[j+z[j]] == t[z[j]])z[j]++;
if(j+z[j]-1>r){
l = j;
r = j+z[j]-1;
}
while(cur<=j && cur<=z[j]){
//cout<<i<<" "<<i+cur-1<<" "<<i+j<<'\n';
nxt[i][i+cur-1] = i+j;
cur++;
}
}
/*
for(int j=0;j<n;j++)cout<<z[j]<<" ";
cout<<'\n';
for(int j=i;j<n;j++)cout<<nxt[i][j]<<" ";
cout<<'\n';
* */
}
for(int i=0;i<n;i++)dp[i][i] = a+b;
for(int len=0;len<n;len++){
for(int i=0;i+len<n;i++){
int r = i+len;
if(dp[i][r] == INF)continue;
//cout<<i<<" "<<r<<" "<<dp[i][r]<<'\n';
if(r+1<n)dp[i][r+1] = min(dp[i][r] + a,dp[i][r+1]);
if(i)dp[i-1][r] = min(dp[i][r]+a,dp[i-1][r]);
ll cur = c;
int pos = i;
while(pos<n){
int jump = nxt[pos][pos+len];
if(jump>=n)break;
cur+=(jump-(pos+len)-1)*1LL*a;
cur+=c;
dp[i][jump+len] = min(dp[i][jump+len],dp[i][r] + cur+b);
pos = jump;
}
}
}
cout<<dp[0][n-1] - b<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
253 ms |
51672 KB |
Output is correct |
4 |
Correct |
295 ms |
58200 KB |
Output is correct |
5 |
Correct |
375 ms |
63060 KB |
Output is correct |
6 |
Correct |
448 ms |
68200 KB |
Output is correct |
7 |
Correct |
544 ms |
73740 KB |
Output is correct |
8 |
Correct |
557 ms |
73760 KB |
Output is correct |
9 |
Correct |
554 ms |
73804 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
3 ms |
2388 KB |
Output is correct |
42 |
Correct |
1 ms |
2388 KB |
Output is correct |
43 |
Correct |
1 ms |
2388 KB |
Output is correct |
44 |
Correct |
1 ms |
2388 KB |
Output is correct |
45 |
Correct |
1 ms |
2388 KB |
Output is correct |
46 |
Correct |
1 ms |
2388 KB |
Output is correct |
47 |
Correct |
2 ms |
2388 KB |
Output is correct |
48 |
Correct |
1 ms |
2388 KB |
Output is correct |
49 |
Correct |
2 ms |
2388 KB |
Output is correct |
50 |
Correct |
1 ms |
2388 KB |
Output is correct |
51 |
Correct |
1 ms |
2388 KB |
Output is correct |
52 |
Correct |
1 ms |
2388 KB |
Output is correct |
53 |
Correct |
2 ms |
2344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
3 ms |
2388 KB |
Output is correct |
42 |
Correct |
1 ms |
2388 KB |
Output is correct |
43 |
Correct |
1 ms |
2388 KB |
Output is correct |
44 |
Correct |
1 ms |
2388 KB |
Output is correct |
45 |
Correct |
1 ms |
2388 KB |
Output is correct |
46 |
Correct |
1 ms |
2388 KB |
Output is correct |
47 |
Correct |
2 ms |
2388 KB |
Output is correct |
48 |
Correct |
1 ms |
2388 KB |
Output is correct |
49 |
Correct |
2 ms |
2388 KB |
Output is correct |
50 |
Correct |
1 ms |
2388 KB |
Output is correct |
51 |
Correct |
1 ms |
2388 KB |
Output is correct |
52 |
Correct |
1 ms |
2388 KB |
Output is correct |
53 |
Correct |
2 ms |
2344 KB |
Output is correct |
54 |
Correct |
6 ms |
6228 KB |
Output is correct |
55 |
Correct |
53 ms |
20088 KB |
Output is correct |
56 |
Correct |
22 ms |
19996 KB |
Output is correct |
57 |
Correct |
20 ms |
20096 KB |
Output is correct |
58 |
Correct |
18 ms |
19988 KB |
Output is correct |
59 |
Correct |
17 ms |
20056 KB |
Output is correct |
60 |
Correct |
17 ms |
20052 KB |
Output is correct |
61 |
Correct |
24 ms |
20052 KB |
Output is correct |
62 |
Correct |
43 ms |
20052 KB |
Output is correct |
63 |
Correct |
20 ms |
20092 KB |
Output is correct |
64 |
Correct |
21 ms |
20000 KB |
Output is correct |
65 |
Correct |
35 ms |
20052 KB |
Output is correct |
66 |
Correct |
32 ms |
20012 KB |
Output is correct |
67 |
Correct |
21 ms |
20088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
253 ms |
51672 KB |
Output is correct |
14 |
Correct |
295 ms |
58200 KB |
Output is correct |
15 |
Correct |
375 ms |
63060 KB |
Output is correct |
16 |
Correct |
448 ms |
68200 KB |
Output is correct |
17 |
Correct |
544 ms |
73740 KB |
Output is correct |
18 |
Correct |
557 ms |
73760 KB |
Output is correct |
19 |
Correct |
554 ms |
73804 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
0 ms |
468 KB |
Output is correct |
41 |
Correct |
0 ms |
468 KB |
Output is correct |
42 |
Correct |
0 ms |
468 KB |
Output is correct |
43 |
Correct |
0 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
0 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
980 KB |
Output is correct |
58 |
Correct |
3 ms |
2388 KB |
Output is correct |
59 |
Correct |
1 ms |
2388 KB |
Output is correct |
60 |
Correct |
1 ms |
2388 KB |
Output is correct |
61 |
Correct |
1 ms |
2388 KB |
Output is correct |
62 |
Correct |
1 ms |
2388 KB |
Output is correct |
63 |
Correct |
1 ms |
2388 KB |
Output is correct |
64 |
Correct |
2 ms |
2388 KB |
Output is correct |
65 |
Correct |
1 ms |
2388 KB |
Output is correct |
66 |
Correct |
2 ms |
2388 KB |
Output is correct |
67 |
Correct |
1 ms |
2388 KB |
Output is correct |
68 |
Correct |
1 ms |
2388 KB |
Output is correct |
69 |
Correct |
1 ms |
2388 KB |
Output is correct |
70 |
Correct |
2 ms |
2344 KB |
Output is correct |
71 |
Correct |
6 ms |
6228 KB |
Output is correct |
72 |
Correct |
53 ms |
20088 KB |
Output is correct |
73 |
Correct |
22 ms |
19996 KB |
Output is correct |
74 |
Correct |
20 ms |
20096 KB |
Output is correct |
75 |
Correct |
18 ms |
19988 KB |
Output is correct |
76 |
Correct |
17 ms |
20056 KB |
Output is correct |
77 |
Correct |
17 ms |
20052 KB |
Output is correct |
78 |
Correct |
24 ms |
20052 KB |
Output is correct |
79 |
Correct |
43 ms |
20052 KB |
Output is correct |
80 |
Correct |
20 ms |
20092 KB |
Output is correct |
81 |
Correct |
21 ms |
20000 KB |
Output is correct |
82 |
Correct |
35 ms |
20052 KB |
Output is correct |
83 |
Correct |
32 ms |
20012 KB |
Output is correct |
84 |
Correct |
21 ms |
20088 KB |
Output is correct |
85 |
Correct |
61 ms |
41692 KB |
Output is correct |
86 |
Correct |
161 ms |
73684 KB |
Output is correct |
87 |
Correct |
128 ms |
73740 KB |
Output is correct |
88 |
Correct |
121 ms |
73744 KB |
Output is correct |
89 |
Correct |
97 ms |
73844 KB |
Output is correct |
90 |
Correct |
98 ms |
73684 KB |
Output is correct |
91 |
Correct |
282 ms |
73720 KB |
Output is correct |
92 |
Correct |
413 ms |
73748 KB |
Output is correct |
93 |
Correct |
150 ms |
73712 KB |
Output is correct |
94 |
Correct |
154 ms |
73744 KB |
Output is correct |
95 |
Correct |
309 ms |
73740 KB |
Output is correct |
96 |
Correct |
306 ms |
73716 KB |
Output is correct |
97 |
Correct |
92 ms |
73672 KB |
Output is correct |