#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
const int md = 1000000007;
const int g = 456;
inline int add(int a, int b) {
a += b;
if (a >= md) a -= md;
return a;
}
inline int sub(int a, int b) {
a -= b;
if (a < 0) a += md;
return a;
}
inline int mul(int a, int b) {
return (long long)a * b % md;
}
const int N = 2505;
int n, a, b, c, h[N], next[N];
long long d[N][N];
std::pair<int, int> str[N];
char s[N];
inline void mnm(long long &a, long long b) {
a = std::min(a, b);
}
int main() {
scanf("%d%s%d%d%d", &n, s, &a, &b, &c);
for (int i = 0; i < n; ++i) h[i + 1] = add(mul(h[i], g), s[i] - 'a');
memset(*d, 0x3f, N * N << 3);
for (int i = 0; i < n; ++i) d[i][i] = 0;
int gg = 1;
for (int l = 1; l <= n; ++l) {
gg = mul(gg, g);
for (int i = 0; i + l <= n; ++i) {
mnm(d[i][i + l], std::min(d[i][i + l - 1], d[i + 1][i + l]) + a);
str[i] = std::make_pair(sub(h[i + l], mul(gg, h[i])), i);
}
std::sort(str, str + n - l + 1);
for (int i = 0; i + l <= n;) {
int fi = i, j = i;
do {
while (j + l <= n && str[j].first == str[i].first && str[i].second + l > str[j].second) ++j;
next[i] = j;
++i;
} while (i + l <= n && str[i].first == str[i - 1].first);
for (; fi < i; ++fi) {
int cnt;
for (j = fi, cnt = 1; j < i; j = next[j], ++cnt) mnm(d[str[fi].second][str[j].second + l], d[str[fi].second][str[fi].second + l] + b + (long long)cnt * c + (long long)a * (str[j].second + l - str[fi].second - cnt * l));
}
}
}
printf("%lld\n", d[0][n]);
return 0;
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d%s%d%d%d", &n, s, &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49384 KB |
Output is correct |
3 |
Correct |
19 ms |
49392 KB |
Output is correct |
4 |
Correct |
19 ms |
49308 KB |
Output is correct |
5 |
Correct |
22 ms |
49300 KB |
Output is correct |
6 |
Correct |
23 ms |
49364 KB |
Output is correct |
7 |
Correct |
25 ms |
49332 KB |
Output is correct |
8 |
Correct |
25 ms |
49344 KB |
Output is correct |
9 |
Correct |
21 ms |
49388 KB |
Output is correct |
10 |
Correct |
20 ms |
49364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
49404 KB |
Output is correct |
2 |
Correct |
20 ms |
49296 KB |
Output is correct |
3 |
Correct |
168 ms |
49372 KB |
Output is correct |
4 |
Correct |
211 ms |
49420 KB |
Output is correct |
5 |
Correct |
257 ms |
49424 KB |
Output is correct |
6 |
Correct |
264 ms |
49364 KB |
Output is correct |
7 |
Correct |
390 ms |
49436 KB |
Output is correct |
8 |
Correct |
307 ms |
49420 KB |
Output is correct |
9 |
Correct |
331 ms |
49424 KB |
Output is correct |
10 |
Correct |
19 ms |
49364 KB |
Output is correct |
11 |
Correct |
18 ms |
49364 KB |
Output is correct |
12 |
Correct |
20 ms |
49328 KB |
Output is correct |
13 |
Correct |
20 ms |
49332 KB |
Output is correct |
14 |
Correct |
20 ms |
49388 KB |
Output is correct |
15 |
Correct |
19 ms |
49348 KB |
Output is correct |
16 |
Correct |
19 ms |
49304 KB |
Output is correct |
17 |
Correct |
20 ms |
49364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49384 KB |
Output is correct |
3 |
Correct |
19 ms |
49392 KB |
Output is correct |
4 |
Correct |
19 ms |
49308 KB |
Output is correct |
5 |
Correct |
22 ms |
49300 KB |
Output is correct |
6 |
Correct |
23 ms |
49364 KB |
Output is correct |
7 |
Correct |
25 ms |
49332 KB |
Output is correct |
8 |
Correct |
25 ms |
49344 KB |
Output is correct |
9 |
Correct |
21 ms |
49388 KB |
Output is correct |
10 |
Correct |
20 ms |
49364 KB |
Output is correct |
11 |
Correct |
18 ms |
49364 KB |
Output is correct |
12 |
Correct |
19 ms |
49312 KB |
Output is correct |
13 |
Correct |
20 ms |
49324 KB |
Output is correct |
14 |
Correct |
20 ms |
49376 KB |
Output is correct |
15 |
Correct |
19 ms |
49364 KB |
Output is correct |
16 |
Correct |
21 ms |
49324 KB |
Output is correct |
17 |
Correct |
25 ms |
49364 KB |
Output is correct |
18 |
Correct |
21 ms |
49364 KB |
Output is correct |
19 |
Correct |
20 ms |
49336 KB |
Output is correct |
20 |
Correct |
21 ms |
49312 KB |
Output is correct |
21 |
Correct |
22 ms |
49336 KB |
Output is correct |
22 |
Correct |
20 ms |
49364 KB |
Output is correct |
23 |
Correct |
20 ms |
49336 KB |
Output is correct |
24 |
Correct |
21 ms |
49404 KB |
Output is correct |
25 |
Correct |
19 ms |
49312 KB |
Output is correct |
26 |
Correct |
22 ms |
49388 KB |
Output is correct |
27 |
Correct |
20 ms |
49328 KB |
Output is correct |
28 |
Correct |
20 ms |
49364 KB |
Output is correct |
29 |
Correct |
23 ms |
49364 KB |
Output is correct |
30 |
Correct |
23 ms |
49364 KB |
Output is correct |
31 |
Correct |
21 ms |
49340 KB |
Output is correct |
32 |
Correct |
24 ms |
49364 KB |
Output is correct |
33 |
Correct |
19 ms |
49348 KB |
Output is correct |
34 |
Correct |
19 ms |
49388 KB |
Output is correct |
35 |
Correct |
19 ms |
49364 KB |
Output is correct |
36 |
Correct |
19 ms |
49364 KB |
Output is correct |
37 |
Correct |
20 ms |
49312 KB |
Output is correct |
38 |
Correct |
20 ms |
49364 KB |
Output is correct |
39 |
Correct |
19 ms |
49364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49384 KB |
Output is correct |
3 |
Correct |
19 ms |
49392 KB |
Output is correct |
4 |
Correct |
19 ms |
49308 KB |
Output is correct |
5 |
Correct |
22 ms |
49300 KB |
Output is correct |
6 |
Correct |
23 ms |
49364 KB |
Output is correct |
7 |
Correct |
25 ms |
49332 KB |
Output is correct |
8 |
Correct |
25 ms |
49344 KB |
Output is correct |
9 |
Correct |
21 ms |
49388 KB |
Output is correct |
10 |
Correct |
20 ms |
49364 KB |
Output is correct |
11 |
Correct |
18 ms |
49364 KB |
Output is correct |
12 |
Correct |
19 ms |
49312 KB |
Output is correct |
13 |
Correct |
20 ms |
49324 KB |
Output is correct |
14 |
Correct |
20 ms |
49376 KB |
Output is correct |
15 |
Correct |
19 ms |
49364 KB |
Output is correct |
16 |
Correct |
21 ms |
49324 KB |
Output is correct |
17 |
Correct |
25 ms |
49364 KB |
Output is correct |
18 |
Correct |
21 ms |
49364 KB |
Output is correct |
19 |
Correct |
20 ms |
49336 KB |
Output is correct |
20 |
Correct |
21 ms |
49312 KB |
Output is correct |
21 |
Correct |
22 ms |
49336 KB |
Output is correct |
22 |
Correct |
20 ms |
49364 KB |
Output is correct |
23 |
Correct |
20 ms |
49336 KB |
Output is correct |
24 |
Correct |
21 ms |
49404 KB |
Output is correct |
25 |
Correct |
19 ms |
49312 KB |
Output is correct |
26 |
Correct |
22 ms |
49388 KB |
Output is correct |
27 |
Correct |
20 ms |
49328 KB |
Output is correct |
28 |
Correct |
20 ms |
49364 KB |
Output is correct |
29 |
Correct |
23 ms |
49364 KB |
Output is correct |
30 |
Correct |
23 ms |
49364 KB |
Output is correct |
31 |
Correct |
21 ms |
49340 KB |
Output is correct |
32 |
Correct |
24 ms |
49364 KB |
Output is correct |
33 |
Correct |
19 ms |
49348 KB |
Output is correct |
34 |
Correct |
19 ms |
49388 KB |
Output is correct |
35 |
Correct |
19 ms |
49364 KB |
Output is correct |
36 |
Correct |
19 ms |
49364 KB |
Output is correct |
37 |
Correct |
20 ms |
49312 KB |
Output is correct |
38 |
Correct |
20 ms |
49364 KB |
Output is correct |
39 |
Correct |
19 ms |
49364 KB |
Output is correct |
40 |
Correct |
24 ms |
49320 KB |
Output is correct |
41 |
Correct |
23 ms |
49412 KB |
Output is correct |
42 |
Correct |
22 ms |
49320 KB |
Output is correct |
43 |
Correct |
21 ms |
49536 KB |
Output is correct |
44 |
Correct |
21 ms |
49408 KB |
Output is correct |
45 |
Correct |
22 ms |
49364 KB |
Output is correct |
46 |
Correct |
20 ms |
49364 KB |
Output is correct |
47 |
Correct |
21 ms |
49300 KB |
Output is correct |
48 |
Correct |
22 ms |
49356 KB |
Output is correct |
49 |
Correct |
21 ms |
49324 KB |
Output is correct |
50 |
Correct |
26 ms |
49324 KB |
Output is correct |
51 |
Correct |
24 ms |
49412 KB |
Output is correct |
52 |
Correct |
24 ms |
49424 KB |
Output is correct |
53 |
Correct |
21 ms |
49364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49384 KB |
Output is correct |
3 |
Correct |
19 ms |
49392 KB |
Output is correct |
4 |
Correct |
19 ms |
49308 KB |
Output is correct |
5 |
Correct |
22 ms |
49300 KB |
Output is correct |
6 |
Correct |
23 ms |
49364 KB |
Output is correct |
7 |
Correct |
25 ms |
49332 KB |
Output is correct |
8 |
Correct |
25 ms |
49344 KB |
Output is correct |
9 |
Correct |
21 ms |
49388 KB |
Output is correct |
10 |
Correct |
20 ms |
49364 KB |
Output is correct |
11 |
Correct |
18 ms |
49364 KB |
Output is correct |
12 |
Correct |
19 ms |
49312 KB |
Output is correct |
13 |
Correct |
20 ms |
49324 KB |
Output is correct |
14 |
Correct |
20 ms |
49376 KB |
Output is correct |
15 |
Correct |
19 ms |
49364 KB |
Output is correct |
16 |
Correct |
21 ms |
49324 KB |
Output is correct |
17 |
Correct |
25 ms |
49364 KB |
Output is correct |
18 |
Correct |
21 ms |
49364 KB |
Output is correct |
19 |
Correct |
20 ms |
49336 KB |
Output is correct |
20 |
Correct |
21 ms |
49312 KB |
Output is correct |
21 |
Correct |
22 ms |
49336 KB |
Output is correct |
22 |
Correct |
20 ms |
49364 KB |
Output is correct |
23 |
Correct |
20 ms |
49336 KB |
Output is correct |
24 |
Correct |
21 ms |
49404 KB |
Output is correct |
25 |
Correct |
19 ms |
49312 KB |
Output is correct |
26 |
Correct |
22 ms |
49388 KB |
Output is correct |
27 |
Correct |
20 ms |
49328 KB |
Output is correct |
28 |
Correct |
20 ms |
49364 KB |
Output is correct |
29 |
Correct |
23 ms |
49364 KB |
Output is correct |
30 |
Correct |
23 ms |
49364 KB |
Output is correct |
31 |
Correct |
21 ms |
49340 KB |
Output is correct |
32 |
Correct |
24 ms |
49364 KB |
Output is correct |
33 |
Correct |
19 ms |
49348 KB |
Output is correct |
34 |
Correct |
19 ms |
49388 KB |
Output is correct |
35 |
Correct |
19 ms |
49364 KB |
Output is correct |
36 |
Correct |
19 ms |
49364 KB |
Output is correct |
37 |
Correct |
20 ms |
49312 KB |
Output is correct |
38 |
Correct |
20 ms |
49364 KB |
Output is correct |
39 |
Correct |
19 ms |
49364 KB |
Output is correct |
40 |
Correct |
24 ms |
49320 KB |
Output is correct |
41 |
Correct |
23 ms |
49412 KB |
Output is correct |
42 |
Correct |
22 ms |
49320 KB |
Output is correct |
43 |
Correct |
21 ms |
49536 KB |
Output is correct |
44 |
Correct |
21 ms |
49408 KB |
Output is correct |
45 |
Correct |
22 ms |
49364 KB |
Output is correct |
46 |
Correct |
20 ms |
49364 KB |
Output is correct |
47 |
Correct |
21 ms |
49300 KB |
Output is correct |
48 |
Correct |
22 ms |
49356 KB |
Output is correct |
49 |
Correct |
21 ms |
49324 KB |
Output is correct |
50 |
Correct |
26 ms |
49324 KB |
Output is correct |
51 |
Correct |
24 ms |
49412 KB |
Output is correct |
52 |
Correct |
24 ms |
49424 KB |
Output is correct |
53 |
Correct |
21 ms |
49364 KB |
Output is correct |
54 |
Correct |
26 ms |
49396 KB |
Output is correct |
55 |
Correct |
48 ms |
49364 KB |
Output is correct |
56 |
Correct |
55 ms |
49364 KB |
Output is correct |
57 |
Correct |
53 ms |
49364 KB |
Output is correct |
58 |
Correct |
54 ms |
49408 KB |
Output is correct |
59 |
Correct |
49 ms |
49364 KB |
Output is correct |
60 |
Correct |
49 ms |
49412 KB |
Output is correct |
61 |
Correct |
67 ms |
49464 KB |
Output is correct |
62 |
Correct |
66 ms |
49404 KB |
Output is correct |
63 |
Correct |
57 ms |
49408 KB |
Output is correct |
64 |
Correct |
55 ms |
49364 KB |
Output is correct |
65 |
Correct |
65 ms |
49408 KB |
Output is correct |
66 |
Correct |
72 ms |
49408 KB |
Output is correct |
67 |
Correct |
56 ms |
49348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49384 KB |
Output is correct |
3 |
Correct |
19 ms |
49392 KB |
Output is correct |
4 |
Correct |
19 ms |
49308 KB |
Output is correct |
5 |
Correct |
22 ms |
49300 KB |
Output is correct |
6 |
Correct |
23 ms |
49364 KB |
Output is correct |
7 |
Correct |
25 ms |
49332 KB |
Output is correct |
8 |
Correct |
25 ms |
49344 KB |
Output is correct |
9 |
Correct |
21 ms |
49388 KB |
Output is correct |
10 |
Correct |
20 ms |
49364 KB |
Output is correct |
11 |
Correct |
19 ms |
49404 KB |
Output is correct |
12 |
Correct |
20 ms |
49296 KB |
Output is correct |
13 |
Correct |
168 ms |
49372 KB |
Output is correct |
14 |
Correct |
211 ms |
49420 KB |
Output is correct |
15 |
Correct |
257 ms |
49424 KB |
Output is correct |
16 |
Correct |
264 ms |
49364 KB |
Output is correct |
17 |
Correct |
390 ms |
49436 KB |
Output is correct |
18 |
Correct |
307 ms |
49420 KB |
Output is correct |
19 |
Correct |
331 ms |
49424 KB |
Output is correct |
20 |
Correct |
19 ms |
49364 KB |
Output is correct |
21 |
Correct |
18 ms |
49364 KB |
Output is correct |
22 |
Correct |
20 ms |
49328 KB |
Output is correct |
23 |
Correct |
20 ms |
49332 KB |
Output is correct |
24 |
Correct |
20 ms |
49388 KB |
Output is correct |
25 |
Correct |
19 ms |
49348 KB |
Output is correct |
26 |
Correct |
19 ms |
49304 KB |
Output is correct |
27 |
Correct |
20 ms |
49364 KB |
Output is correct |
28 |
Correct |
18 ms |
49364 KB |
Output is correct |
29 |
Correct |
19 ms |
49312 KB |
Output is correct |
30 |
Correct |
20 ms |
49324 KB |
Output is correct |
31 |
Correct |
20 ms |
49376 KB |
Output is correct |
32 |
Correct |
19 ms |
49364 KB |
Output is correct |
33 |
Correct |
21 ms |
49324 KB |
Output is correct |
34 |
Correct |
25 ms |
49364 KB |
Output is correct |
35 |
Correct |
21 ms |
49364 KB |
Output is correct |
36 |
Correct |
20 ms |
49336 KB |
Output is correct |
37 |
Correct |
21 ms |
49312 KB |
Output is correct |
38 |
Correct |
22 ms |
49336 KB |
Output is correct |
39 |
Correct |
20 ms |
49364 KB |
Output is correct |
40 |
Correct |
20 ms |
49336 KB |
Output is correct |
41 |
Correct |
21 ms |
49404 KB |
Output is correct |
42 |
Correct |
19 ms |
49312 KB |
Output is correct |
43 |
Correct |
22 ms |
49388 KB |
Output is correct |
44 |
Correct |
20 ms |
49328 KB |
Output is correct |
45 |
Correct |
20 ms |
49364 KB |
Output is correct |
46 |
Correct |
23 ms |
49364 KB |
Output is correct |
47 |
Correct |
23 ms |
49364 KB |
Output is correct |
48 |
Correct |
21 ms |
49340 KB |
Output is correct |
49 |
Correct |
24 ms |
49364 KB |
Output is correct |
50 |
Correct |
19 ms |
49348 KB |
Output is correct |
51 |
Correct |
19 ms |
49388 KB |
Output is correct |
52 |
Correct |
19 ms |
49364 KB |
Output is correct |
53 |
Correct |
19 ms |
49364 KB |
Output is correct |
54 |
Correct |
20 ms |
49312 KB |
Output is correct |
55 |
Correct |
20 ms |
49364 KB |
Output is correct |
56 |
Correct |
19 ms |
49364 KB |
Output is correct |
57 |
Correct |
24 ms |
49320 KB |
Output is correct |
58 |
Correct |
23 ms |
49412 KB |
Output is correct |
59 |
Correct |
22 ms |
49320 KB |
Output is correct |
60 |
Correct |
21 ms |
49536 KB |
Output is correct |
61 |
Correct |
21 ms |
49408 KB |
Output is correct |
62 |
Correct |
22 ms |
49364 KB |
Output is correct |
63 |
Correct |
20 ms |
49364 KB |
Output is correct |
64 |
Correct |
21 ms |
49300 KB |
Output is correct |
65 |
Correct |
22 ms |
49356 KB |
Output is correct |
66 |
Correct |
21 ms |
49324 KB |
Output is correct |
67 |
Correct |
26 ms |
49324 KB |
Output is correct |
68 |
Correct |
24 ms |
49412 KB |
Output is correct |
69 |
Correct |
24 ms |
49424 KB |
Output is correct |
70 |
Correct |
21 ms |
49364 KB |
Output is correct |
71 |
Correct |
26 ms |
49396 KB |
Output is correct |
72 |
Correct |
48 ms |
49364 KB |
Output is correct |
73 |
Correct |
55 ms |
49364 KB |
Output is correct |
74 |
Correct |
53 ms |
49364 KB |
Output is correct |
75 |
Correct |
54 ms |
49408 KB |
Output is correct |
76 |
Correct |
49 ms |
49364 KB |
Output is correct |
77 |
Correct |
49 ms |
49412 KB |
Output is correct |
78 |
Correct |
67 ms |
49464 KB |
Output is correct |
79 |
Correct |
66 ms |
49404 KB |
Output is correct |
80 |
Correct |
57 ms |
49408 KB |
Output is correct |
81 |
Correct |
55 ms |
49364 KB |
Output is correct |
82 |
Correct |
65 ms |
49408 KB |
Output is correct |
83 |
Correct |
72 ms |
49408 KB |
Output is correct |
84 |
Correct |
56 ms |
49348 KB |
Output is correct |
85 |
Correct |
110 ms |
49416 KB |
Output is correct |
86 |
Correct |
258 ms |
49424 KB |
Output is correct |
87 |
Correct |
288 ms |
49424 KB |
Output is correct |
88 |
Correct |
245 ms |
49420 KB |
Output is correct |
89 |
Correct |
273 ms |
49416 KB |
Output is correct |
90 |
Correct |
244 ms |
49416 KB |
Output is correct |
91 |
Correct |
389 ms |
49420 KB |
Output is correct |
92 |
Correct |
408 ms |
49420 KB |
Output is correct |
93 |
Correct |
284 ms |
49420 KB |
Output is correct |
94 |
Correct |
348 ms |
49424 KB |
Output is correct |
95 |
Correct |
423 ms |
49420 KB |
Output is correct |
96 |
Correct |
391 ms |
49500 KB |
Output is correct |
97 |
Incorrect |
273 ms |
49420 KB |
Output isn't correct |
98 |
Halted |
0 ms |
0 KB |
- |