#include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int n = ri();
(void) n;
std::string s;
std::cin >> s;
int64_t a = ri();
int64_t b = ri();
int64_t c = ri();
/*
next[l][r] :
smallest k such that k >= r && s.substr(l, r - l) == s.substr(k, r - l)
n if no such k exists
*/
int next[n][n + 1];
auto z = [] (char *s) { // z algorithm : returns the Z array of the string starting at `s`
int n = strlen(s);
std::vector<int> res(n);
int head = 0;
int start = 0;
for (int i = 1; i < n; i++) {
if (res[i - start] >= head - i) {
head = std::max(head, i);
while (head < n && s[head] == s[head - i]) head++;
res[i] = head - i;
start = i;
} else res[i] = std::min(n - i, res[i - start]);
}
return res;
};
for (int i = 0; i < n; i++) {
auto tmp = z(&s[i]);
int max_len = 0;
for (int j = i + 1; j < n; j++) {
int cur_max_len = std::min(j - i, tmp[j - i]);
while (max_len < cur_max_len) next[i][i + ++max_len] = j;
}
while (max_len < n - i) next[i][i + ++max_len] = n;
}
int64_t dp[n][n + 1]; // dp[l][r] : the lowest cost to make x == "" && y == s.substr(l, r - l)
for (auto &i : dp) for (auto &j : i) j = 1000000000000000000;
for (int i = 0; i < n; i++) dp[i][i + 1] = a + b;
for (int len = 1; len < n; len++) {
for (int l = 0; l + len <= n; l++) {
int r = l + len;
if (l) dp[l - 1][r] = std::min(dp[l - 1][r], dp[l][r] + a);
if (r + 1 <= n) dp[l][r + 1] = std::min(dp[l][r + 1], dp[l][r] + a);
int cur = l;
int cnt = 1; // number of pasting
while (next[cur][cur + len] < n) {
cur = next[cur][cur + len];
++cnt;
dp[l][cur + len] = std::min(dp[l][cur + len], dp[l][r] + c * cnt + a * (cur + len - l - len * cnt) + b);
}
}
}
// dp[0][n] is the cost to make x == "" && y == s (immediately after cutting).
// To make x == s, the last cutting is unnecessary
printf("%" PRId64 "\n", dp[0][n] - b);
return 0;
}
Compilation message
copypaste3.cpp: In function 'int ri()':
copypaste3.cpp:5:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
294 ms |
39560 KB |
Output is correct |
4 |
Correct |
350 ms |
46216 KB |
Output is correct |
5 |
Correct |
481 ms |
54004 KB |
Output is correct |
6 |
Correct |
621 ms |
63044 KB |
Output is correct |
7 |
Correct |
798 ms |
73720 KB |
Output is correct |
8 |
Correct |
672 ms |
73728 KB |
Output is correct |
9 |
Correct |
736 ms |
73720 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
2 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
2 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
724 KB |
Output is correct |
44 |
Correct |
1 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
724 KB |
Output is correct |
46 |
Correct |
4 ms |
724 KB |
Output is correct |
47 |
Correct |
2 ms |
724 KB |
Output is correct |
48 |
Correct |
1 ms |
724 KB |
Output is correct |
49 |
Correct |
1 ms |
724 KB |
Output is correct |
50 |
Correct |
1 ms |
724 KB |
Output is correct |
51 |
Correct |
2 ms |
724 KB |
Output is correct |
52 |
Correct |
4 ms |
792 KB |
Output is correct |
53 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
2 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
724 KB |
Output is correct |
44 |
Correct |
1 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
724 KB |
Output is correct |
46 |
Correct |
4 ms |
724 KB |
Output is correct |
47 |
Correct |
2 ms |
724 KB |
Output is correct |
48 |
Correct |
1 ms |
724 KB |
Output is correct |
49 |
Correct |
1 ms |
724 KB |
Output is correct |
50 |
Correct |
1 ms |
724 KB |
Output is correct |
51 |
Correct |
2 ms |
724 KB |
Output is correct |
52 |
Correct |
4 ms |
792 KB |
Output is correct |
53 |
Correct |
1 ms |
724 KB |
Output is correct |
54 |
Correct |
5 ms |
2644 KB |
Output is correct |
55 |
Correct |
56 ms |
12044 KB |
Output is correct |
56 |
Correct |
29 ms |
12080 KB |
Output is correct |
57 |
Correct |
26 ms |
12060 KB |
Output is correct |
58 |
Correct |
21 ms |
12084 KB |
Output is correct |
59 |
Correct |
20 ms |
12072 KB |
Output is correct |
60 |
Correct |
25 ms |
12044 KB |
Output is correct |
61 |
Correct |
25 ms |
11988 KB |
Output is correct |
62 |
Correct |
57 ms |
12064 KB |
Output is correct |
63 |
Correct |
25 ms |
11988 KB |
Output is correct |
64 |
Correct |
26 ms |
11984 KB |
Output is correct |
65 |
Correct |
41 ms |
12064 KB |
Output is correct |
66 |
Correct |
36 ms |
12056 KB |
Output is correct |
67 |
Correct |
17 ms |
12084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
294 ms |
39560 KB |
Output is correct |
14 |
Correct |
350 ms |
46216 KB |
Output is correct |
15 |
Correct |
481 ms |
54004 KB |
Output is correct |
16 |
Correct |
621 ms |
63044 KB |
Output is correct |
17 |
Correct |
798 ms |
73720 KB |
Output is correct |
18 |
Correct |
672 ms |
73728 KB |
Output is correct |
19 |
Correct |
736 ms |
73720 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
2 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
2 ms |
724 KB |
Output is correct |
59 |
Correct |
1 ms |
724 KB |
Output is correct |
60 |
Correct |
2 ms |
724 KB |
Output is correct |
61 |
Correct |
1 ms |
724 KB |
Output is correct |
62 |
Correct |
2 ms |
724 KB |
Output is correct |
63 |
Correct |
4 ms |
724 KB |
Output is correct |
64 |
Correct |
2 ms |
724 KB |
Output is correct |
65 |
Correct |
1 ms |
724 KB |
Output is correct |
66 |
Correct |
1 ms |
724 KB |
Output is correct |
67 |
Correct |
1 ms |
724 KB |
Output is correct |
68 |
Correct |
2 ms |
724 KB |
Output is correct |
69 |
Correct |
4 ms |
792 KB |
Output is correct |
70 |
Correct |
1 ms |
724 KB |
Output is correct |
71 |
Correct |
5 ms |
2644 KB |
Output is correct |
72 |
Correct |
56 ms |
12044 KB |
Output is correct |
73 |
Correct |
29 ms |
12080 KB |
Output is correct |
74 |
Correct |
26 ms |
12060 KB |
Output is correct |
75 |
Correct |
21 ms |
12084 KB |
Output is correct |
76 |
Correct |
20 ms |
12072 KB |
Output is correct |
77 |
Correct |
25 ms |
12044 KB |
Output is correct |
78 |
Correct |
25 ms |
11988 KB |
Output is correct |
79 |
Correct |
57 ms |
12064 KB |
Output is correct |
80 |
Correct |
25 ms |
11988 KB |
Output is correct |
81 |
Correct |
26 ms |
11984 KB |
Output is correct |
82 |
Correct |
41 ms |
12064 KB |
Output is correct |
83 |
Correct |
36 ms |
12056 KB |
Output is correct |
84 |
Correct |
17 ms |
12084 KB |
Output is correct |
85 |
Correct |
74 ms |
29664 KB |
Output is correct |
86 |
Correct |
211 ms |
73724 KB |
Output is correct |
87 |
Correct |
205 ms |
73720 KB |
Output is correct |
88 |
Correct |
167 ms |
73724 KB |
Output is correct |
89 |
Correct |
137 ms |
73656 KB |
Output is correct |
90 |
Correct |
137 ms |
73668 KB |
Output is correct |
91 |
Correct |
355 ms |
73720 KB |
Output is correct |
92 |
Correct |
522 ms |
73724 KB |
Output is correct |
93 |
Correct |
182 ms |
73728 KB |
Output is correct |
94 |
Correct |
190 ms |
73720 KB |
Output is correct |
95 |
Correct |
389 ms |
73720 KB |
Output is correct |
96 |
Correct |
448 ms |
73720 KB |
Output is correct |
97 |
Correct |
121 ms |
73720 KB |
Output is correct |