#include <stdio.h>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
char S[2501];
long long dp[2501][2501], mod=1e9+7, h[2501], mul=1000003, nex[2501][2501];
int main(){
h[0]=1;
for(int i=1;i<2501;i++) h[i]=h[i-1]*mul%mod;
int n;
long long a, b, c;
scanf("%d%s%lld%lld%lld", &n, S, &a, &b, &c);
for(int i=0;i<=n;i++) for(int j=i;j<=n;j++) dp[i][j]=(j-i+1)*a+b;
for(int l=1;l<=n;l++){
map<long long, long long> m;
queue<long long> q[2501];
long long hc=0, cnt=0;
for(int i=0;i<l;i++) hc=(hc*mul+(S[i]-'a'))%mod;
m[hc]=++cnt;
q[cnt].push(0);
for(int i=l;i<n;i++){
hc=(hc*mul-h[l]*(S[i-l]-'a')+(S[i]-'a'))%mod;
if(m.find(hc)==m.end()) m[hc]=++cnt;
long long x=m[hc];
while(!q[x].empty() && q[x].front()+l-1<i-l+1) nex[q[x].front()][q[x].front()+l-1]=i-l-q[x].front()+1, q[x].pop();
q[x].push(i-l+1);
}
}
for(int l=1;l<=n;l++){
for(int i=0;i<n-l+1;i++){
int j=i+l-1, k=0, j1=j, cnt=1;
if(i) dp[i-1][j]=min(dp[i-1][j], dp[i][j]+a);
if(j+1!=n) dp[i][j+1]=min(dp[i][j+1], dp[i][j]+a);
while(nex[i+k][j]){
int x=nex[i+k][j];
j+=x; k+=x; cnt++;
dp[i][j]=min(dp[i][j], dp[i][j1]+(j-i+1-l*cnt)*a+b+c*cnt);
}
}
}
printf("%lld\n", dp[0][n-1]-b);
return 0;
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:15:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d%s%lld%lld%lld", &n, S, &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
2 ms |
2004 KB |
Output is correct |
4 |
Correct |
2 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
2004 KB |
Output is correct |
6 |
Correct |
2 ms |
2004 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2004 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
730 ms |
36296 KB |
Output is correct |
4 |
Correct |
804 ms |
40948 KB |
Output is correct |
5 |
Correct |
913 ms |
45976 KB |
Output is correct |
6 |
Correct |
1043 ms |
51400 KB |
Output is correct |
7 |
Correct |
1192 ms |
57748 KB |
Output is correct |
8 |
Correct |
1189 ms |
57740 KB |
Output is correct |
9 |
Correct |
1159 ms |
57748 KB |
Output is correct |
10 |
Correct |
1 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2004 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
3 ms |
1924 KB |
Output is correct |
14 |
Correct |
3 ms |
2004 KB |
Output is correct |
15 |
Correct |
5 ms |
2004 KB |
Output is correct |
16 |
Correct |
7 ms |
2132 KB |
Output is correct |
17 |
Correct |
2 ms |
2012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
2 ms |
2004 KB |
Output is correct |
4 |
Correct |
2 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
2004 KB |
Output is correct |
6 |
Correct |
2 ms |
2004 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2004 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
3 ms |
2004 KB |
Output is correct |
12 |
Correct |
4 ms |
2004 KB |
Output is correct |
13 |
Correct |
4 ms |
2004 KB |
Output is correct |
14 |
Correct |
2 ms |
2004 KB |
Output is correct |
15 |
Correct |
5 ms |
2132 KB |
Output is correct |
16 |
Correct |
3 ms |
2064 KB |
Output is correct |
17 |
Correct |
2 ms |
2036 KB |
Output is correct |
18 |
Correct |
1 ms |
1920 KB |
Output is correct |
19 |
Correct |
3 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2004 KB |
Output is correct |
21 |
Correct |
8 ms |
2132 KB |
Output is correct |
22 |
Correct |
7 ms |
2132 KB |
Output is correct |
23 |
Correct |
7 ms |
2132 KB |
Output is correct |
24 |
Correct |
9 ms |
2216 KB |
Output is correct |
25 |
Correct |
7 ms |
2132 KB |
Output is correct |
26 |
Correct |
11 ms |
2164 KB |
Output is correct |
27 |
Correct |
7 ms |
2132 KB |
Output is correct |
28 |
Correct |
8 ms |
2132 KB |
Output is correct |
29 |
Correct |
6 ms |
2132 KB |
Output is correct |
30 |
Correct |
7 ms |
2132 KB |
Output is correct |
31 |
Correct |
6 ms |
2132 KB |
Output is correct |
32 |
Correct |
6 ms |
2132 KB |
Output is correct |
33 |
Correct |
7 ms |
2132 KB |
Output is correct |
34 |
Correct |
2 ms |
2004 KB |
Output is correct |
35 |
Correct |
1 ms |
2004 KB |
Output is correct |
36 |
Correct |
2 ms |
2004 KB |
Output is correct |
37 |
Correct |
2 ms |
2004 KB |
Output is correct |
38 |
Correct |
3 ms |
2004 KB |
Output is correct |
39 |
Correct |
7 ms |
2236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
2 ms |
2004 KB |
Output is correct |
4 |
Correct |
2 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
2004 KB |
Output is correct |
6 |
Correct |
2 ms |
2004 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2004 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
3 ms |
2004 KB |
Output is correct |
12 |
Correct |
4 ms |
2004 KB |
Output is correct |
13 |
Correct |
4 ms |
2004 KB |
Output is correct |
14 |
Correct |
2 ms |
2004 KB |
Output is correct |
15 |
Correct |
5 ms |
2132 KB |
Output is correct |
16 |
Correct |
3 ms |
2064 KB |
Output is correct |
17 |
Correct |
2 ms |
2036 KB |
Output is correct |
18 |
Correct |
1 ms |
1920 KB |
Output is correct |
19 |
Correct |
3 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2004 KB |
Output is correct |
21 |
Correct |
8 ms |
2132 KB |
Output is correct |
22 |
Correct |
7 ms |
2132 KB |
Output is correct |
23 |
Correct |
7 ms |
2132 KB |
Output is correct |
24 |
Correct |
9 ms |
2216 KB |
Output is correct |
25 |
Correct |
7 ms |
2132 KB |
Output is correct |
26 |
Correct |
11 ms |
2164 KB |
Output is correct |
27 |
Correct |
7 ms |
2132 KB |
Output is correct |
28 |
Correct |
8 ms |
2132 KB |
Output is correct |
29 |
Correct |
6 ms |
2132 KB |
Output is correct |
30 |
Correct |
7 ms |
2132 KB |
Output is correct |
31 |
Correct |
6 ms |
2132 KB |
Output is correct |
32 |
Correct |
6 ms |
2132 KB |
Output is correct |
33 |
Correct |
7 ms |
2132 KB |
Output is correct |
34 |
Correct |
2 ms |
2004 KB |
Output is correct |
35 |
Correct |
1 ms |
2004 KB |
Output is correct |
36 |
Correct |
2 ms |
2004 KB |
Output is correct |
37 |
Correct |
2 ms |
2004 KB |
Output is correct |
38 |
Correct |
3 ms |
2004 KB |
Output is correct |
39 |
Correct |
7 ms |
2236 KB |
Output is correct |
40 |
Correct |
19 ms |
2644 KB |
Output is correct |
41 |
Correct |
43 ms |
3820 KB |
Output is correct |
42 |
Correct |
49 ms |
3776 KB |
Output is correct |
43 |
Correct |
55 ms |
3668 KB |
Output is correct |
44 |
Correct |
43 ms |
3668 KB |
Output is correct |
45 |
Correct |
44 ms |
3720 KB |
Output is correct |
46 |
Correct |
49 ms |
3668 KB |
Output is correct |
47 |
Correct |
39 ms |
3764 KB |
Output is correct |
48 |
Correct |
40 ms |
3668 KB |
Output is correct |
49 |
Correct |
40 ms |
3668 KB |
Output is correct |
50 |
Correct |
42 ms |
3668 KB |
Output is correct |
51 |
Correct |
45 ms |
3804 KB |
Output is correct |
52 |
Correct |
44 ms |
3796 KB |
Output is correct |
53 |
Correct |
45 ms |
3668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
2 ms |
2004 KB |
Output is correct |
4 |
Correct |
2 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
2004 KB |
Output is correct |
6 |
Correct |
2 ms |
2004 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2004 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
3 ms |
2004 KB |
Output is correct |
12 |
Correct |
4 ms |
2004 KB |
Output is correct |
13 |
Correct |
4 ms |
2004 KB |
Output is correct |
14 |
Correct |
2 ms |
2004 KB |
Output is correct |
15 |
Correct |
5 ms |
2132 KB |
Output is correct |
16 |
Correct |
3 ms |
2064 KB |
Output is correct |
17 |
Correct |
2 ms |
2036 KB |
Output is correct |
18 |
Correct |
1 ms |
1920 KB |
Output is correct |
19 |
Correct |
3 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2004 KB |
Output is correct |
21 |
Correct |
8 ms |
2132 KB |
Output is correct |
22 |
Correct |
7 ms |
2132 KB |
Output is correct |
23 |
Correct |
7 ms |
2132 KB |
Output is correct |
24 |
Correct |
9 ms |
2216 KB |
Output is correct |
25 |
Correct |
7 ms |
2132 KB |
Output is correct |
26 |
Correct |
11 ms |
2164 KB |
Output is correct |
27 |
Correct |
7 ms |
2132 KB |
Output is correct |
28 |
Correct |
8 ms |
2132 KB |
Output is correct |
29 |
Correct |
6 ms |
2132 KB |
Output is correct |
30 |
Correct |
7 ms |
2132 KB |
Output is correct |
31 |
Correct |
6 ms |
2132 KB |
Output is correct |
32 |
Correct |
6 ms |
2132 KB |
Output is correct |
33 |
Correct |
7 ms |
2132 KB |
Output is correct |
34 |
Correct |
2 ms |
2004 KB |
Output is correct |
35 |
Correct |
1 ms |
2004 KB |
Output is correct |
36 |
Correct |
2 ms |
2004 KB |
Output is correct |
37 |
Correct |
2 ms |
2004 KB |
Output is correct |
38 |
Correct |
3 ms |
2004 KB |
Output is correct |
39 |
Correct |
7 ms |
2236 KB |
Output is correct |
40 |
Correct |
19 ms |
2644 KB |
Output is correct |
41 |
Correct |
43 ms |
3820 KB |
Output is correct |
42 |
Correct |
49 ms |
3776 KB |
Output is correct |
43 |
Correct |
55 ms |
3668 KB |
Output is correct |
44 |
Correct |
43 ms |
3668 KB |
Output is correct |
45 |
Correct |
44 ms |
3720 KB |
Output is correct |
46 |
Correct |
49 ms |
3668 KB |
Output is correct |
47 |
Correct |
39 ms |
3764 KB |
Output is correct |
48 |
Correct |
40 ms |
3668 KB |
Output is correct |
49 |
Correct |
40 ms |
3668 KB |
Output is correct |
50 |
Correct |
42 ms |
3668 KB |
Output is correct |
51 |
Correct |
45 ms |
3804 KB |
Output is correct |
52 |
Correct |
44 ms |
3796 KB |
Output is correct |
53 |
Correct |
45 ms |
3668 KB |
Output is correct |
54 |
Correct |
110 ms |
6416 KB |
Output is correct |
55 |
Correct |
267 ms |
15808 KB |
Output is correct |
56 |
Correct |
335 ms |
14072 KB |
Output is correct |
57 |
Correct |
354 ms |
14040 KB |
Output is correct |
58 |
Correct |
298 ms |
13908 KB |
Output is correct |
59 |
Correct |
317 ms |
14032 KB |
Output is correct |
60 |
Correct |
395 ms |
13912 KB |
Output is correct |
61 |
Correct |
289 ms |
15360 KB |
Output is correct |
62 |
Correct |
287 ms |
15892 KB |
Output is correct |
63 |
Correct |
324 ms |
14888 KB |
Output is correct |
64 |
Correct |
332 ms |
14840 KB |
Output is correct |
65 |
Correct |
305 ms |
15604 KB |
Output is correct |
66 |
Correct |
333 ms |
15612 KB |
Output is correct |
67 |
Correct |
327 ms |
13916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
2 ms |
2004 KB |
Output is correct |
4 |
Correct |
2 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
2004 KB |
Output is correct |
6 |
Correct |
2 ms |
2004 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2004 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
4 ms |
2004 KB |
Output is correct |
12 |
Correct |
2 ms |
2004 KB |
Output is correct |
13 |
Correct |
730 ms |
36296 KB |
Output is correct |
14 |
Correct |
804 ms |
40948 KB |
Output is correct |
15 |
Correct |
913 ms |
45976 KB |
Output is correct |
16 |
Correct |
1043 ms |
51400 KB |
Output is correct |
17 |
Correct |
1192 ms |
57748 KB |
Output is correct |
18 |
Correct |
1189 ms |
57740 KB |
Output is correct |
19 |
Correct |
1159 ms |
57748 KB |
Output is correct |
20 |
Correct |
1 ms |
2004 KB |
Output is correct |
21 |
Correct |
2 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2004 KB |
Output is correct |
23 |
Correct |
3 ms |
1924 KB |
Output is correct |
24 |
Correct |
3 ms |
2004 KB |
Output is correct |
25 |
Correct |
5 ms |
2004 KB |
Output is correct |
26 |
Correct |
7 ms |
2132 KB |
Output is correct |
27 |
Correct |
2 ms |
2012 KB |
Output is correct |
28 |
Correct |
3 ms |
2004 KB |
Output is correct |
29 |
Correct |
4 ms |
2004 KB |
Output is correct |
30 |
Correct |
4 ms |
2004 KB |
Output is correct |
31 |
Correct |
2 ms |
2004 KB |
Output is correct |
32 |
Correct |
5 ms |
2132 KB |
Output is correct |
33 |
Correct |
3 ms |
2064 KB |
Output is correct |
34 |
Correct |
2 ms |
2036 KB |
Output is correct |
35 |
Correct |
1 ms |
1920 KB |
Output is correct |
36 |
Correct |
3 ms |
2004 KB |
Output is correct |
37 |
Correct |
2 ms |
2004 KB |
Output is correct |
38 |
Correct |
8 ms |
2132 KB |
Output is correct |
39 |
Correct |
7 ms |
2132 KB |
Output is correct |
40 |
Correct |
7 ms |
2132 KB |
Output is correct |
41 |
Correct |
9 ms |
2216 KB |
Output is correct |
42 |
Correct |
7 ms |
2132 KB |
Output is correct |
43 |
Correct |
11 ms |
2164 KB |
Output is correct |
44 |
Correct |
7 ms |
2132 KB |
Output is correct |
45 |
Correct |
8 ms |
2132 KB |
Output is correct |
46 |
Correct |
6 ms |
2132 KB |
Output is correct |
47 |
Correct |
7 ms |
2132 KB |
Output is correct |
48 |
Correct |
6 ms |
2132 KB |
Output is correct |
49 |
Correct |
6 ms |
2132 KB |
Output is correct |
50 |
Correct |
7 ms |
2132 KB |
Output is correct |
51 |
Correct |
2 ms |
2004 KB |
Output is correct |
52 |
Correct |
1 ms |
2004 KB |
Output is correct |
53 |
Correct |
2 ms |
2004 KB |
Output is correct |
54 |
Correct |
2 ms |
2004 KB |
Output is correct |
55 |
Correct |
3 ms |
2004 KB |
Output is correct |
56 |
Correct |
7 ms |
2236 KB |
Output is correct |
57 |
Correct |
19 ms |
2644 KB |
Output is correct |
58 |
Correct |
43 ms |
3820 KB |
Output is correct |
59 |
Correct |
49 ms |
3776 KB |
Output is correct |
60 |
Correct |
55 ms |
3668 KB |
Output is correct |
61 |
Correct |
43 ms |
3668 KB |
Output is correct |
62 |
Correct |
44 ms |
3720 KB |
Output is correct |
63 |
Correct |
49 ms |
3668 KB |
Output is correct |
64 |
Correct |
39 ms |
3764 KB |
Output is correct |
65 |
Correct |
40 ms |
3668 KB |
Output is correct |
66 |
Correct |
40 ms |
3668 KB |
Output is correct |
67 |
Correct |
42 ms |
3668 KB |
Output is correct |
68 |
Correct |
45 ms |
3804 KB |
Output is correct |
69 |
Correct |
44 ms |
3796 KB |
Output is correct |
70 |
Correct |
45 ms |
3668 KB |
Output is correct |
71 |
Correct |
110 ms |
6416 KB |
Output is correct |
72 |
Correct |
267 ms |
15808 KB |
Output is correct |
73 |
Correct |
335 ms |
14072 KB |
Output is correct |
74 |
Correct |
354 ms |
14040 KB |
Output is correct |
75 |
Correct |
298 ms |
13908 KB |
Output is correct |
76 |
Correct |
317 ms |
14032 KB |
Output is correct |
77 |
Correct |
395 ms |
13912 KB |
Output is correct |
78 |
Correct |
289 ms |
15360 KB |
Output is correct |
79 |
Correct |
287 ms |
15892 KB |
Output is correct |
80 |
Correct |
324 ms |
14888 KB |
Output is correct |
81 |
Correct |
332 ms |
14840 KB |
Output is correct |
82 |
Correct |
305 ms |
15604 KB |
Output is correct |
83 |
Correct |
333 ms |
15612 KB |
Output is correct |
84 |
Correct |
327 ms |
13916 KB |
Output is correct |
85 |
Correct |
688 ms |
24712 KB |
Output is correct |
86 |
Correct |
1485 ms |
45884 KB |
Output is correct |
87 |
Correct |
1476 ms |
45840 KB |
Output is correct |
88 |
Correct |
1421 ms |
45736 KB |
Output is correct |
89 |
Incorrect |
1403 ms |
45684 KB |
Output isn't correct |
90 |
Halted |
0 ms |
0 KB |
- |