#include <bits/stdc++.h>
using namespace std;
#define int int64_t
int mod = 100000000003LL;
int B = 41;
const int inf = 2e18;
const int max_len = 2500;
int h[max_len + 1][max_len + 1];
int dp[max_len + 1][max_len + 1];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int a, b, c;
cin >> a >> b >> c;
for (int l = 0; l <= n; l++) {
for (int r = l + 1; r <= n; r++) {
h[l][r] = (h[l][r - 1] * B + s[r - 1] - 'a' + 1) % mod;
}
}
for (int l = 0; l <= n; l++) {
for (int r = l; r <= n; r++) {
dp[l][r] = a * (r - l);
}
}
for (int len = 1; len <= n; len++) {
for (int l = 0, r = len; r <= n; l++, r++) {
dp[l][r] = min(dp[l][r], dp[l + 1][r] + a);
dp[l][r] = min(dp[l][r], dp[l][r - 1] + a);
}
map<int, vector<int>> where;
for (int l = 0, r = len; r <= n; l++, r++) {
where[h[l][r]].push_back(l);
}
vector<int> nxt(n, -1);
for (auto it = where.begin(); it != where.end(); it++) {
auto& cur_list = it->second;
int j = 0;
for (int i = 0; i < cur_list.size(); i++) {
while (j < cur_list.size() && cur_list[j] - cur_list[i] < len) {
j++;
}
if (j < cur_list.size()) {
nxt[cur_list[i]] = cur_list[j];
}
}
}
for (int l = 0, r = len; r <= n; l++, r++) {
int cur = l;
int cnt = 1;
while (nxt[cur] != -1) {
cur = nxt[cur];
cnt++;
int R = cur + len;
dp[l][R] = min(dp[l][cur + len], dp[l][r] + b + c * cnt + a * (R - l - cnt * (r - l)));
}
}
}
cout << dp[0][n] << '\n';
}
Compilation message
copypaste3.cpp: In function 'int32_t main()':
copypaste3.cpp:47:31: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 0; i < cur_list.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
copypaste3.cpp:48:26: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | while (j < cur_list.size() && cur_list[j] - cur_list[i] < len) {
| ~~^~~~~~~~~~~~~~~~~
copypaste3.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | if (j < cur_list.size()) {
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
118 ms |
41164 KB |
Output is correct |
4 |
Correct |
159 ms |
46796 KB |
Output is correct |
5 |
Correct |
193 ms |
53188 KB |
Output is correct |
6 |
Correct |
227 ms |
59960 KB |
Output is correct |
7 |
Correct |
289 ms |
67260 KB |
Output is correct |
8 |
Correct |
281 ms |
67360 KB |
Output is correct |
9 |
Correct |
279 ms |
67348 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
584 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
584 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
972 KB |
Output is correct |
41 |
Correct |
2 ms |
2260 KB |
Output is correct |
42 |
Correct |
4 ms |
2260 KB |
Output is correct |
43 |
Correct |
4 ms |
2256 KB |
Output is correct |
44 |
Correct |
4 ms |
2260 KB |
Output is correct |
45 |
Correct |
4 ms |
2260 KB |
Output is correct |
46 |
Correct |
5 ms |
2260 KB |
Output is correct |
47 |
Correct |
3 ms |
2260 KB |
Output is correct |
48 |
Correct |
4 ms |
2260 KB |
Output is correct |
49 |
Correct |
4 ms |
2260 KB |
Output is correct |
50 |
Correct |
4 ms |
2256 KB |
Output is correct |
51 |
Correct |
3 ms |
2260 KB |
Output is correct |
52 |
Correct |
3 ms |
2260 KB |
Output is correct |
53 |
Correct |
4 ms |
2248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
584 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
972 KB |
Output is correct |
41 |
Correct |
2 ms |
2260 KB |
Output is correct |
42 |
Correct |
4 ms |
2260 KB |
Output is correct |
43 |
Correct |
4 ms |
2256 KB |
Output is correct |
44 |
Correct |
4 ms |
2260 KB |
Output is correct |
45 |
Correct |
4 ms |
2260 KB |
Output is correct |
46 |
Correct |
5 ms |
2260 KB |
Output is correct |
47 |
Correct |
3 ms |
2260 KB |
Output is correct |
48 |
Correct |
4 ms |
2260 KB |
Output is correct |
49 |
Correct |
4 ms |
2260 KB |
Output is correct |
50 |
Correct |
4 ms |
2256 KB |
Output is correct |
51 |
Correct |
3 ms |
2260 KB |
Output is correct |
52 |
Correct |
3 ms |
2260 KB |
Output is correct |
53 |
Correct |
4 ms |
2248 KB |
Output is correct |
54 |
Correct |
17 ms |
5572 KB |
Output is correct |
55 |
Correct |
32 ms |
16216 KB |
Output is correct |
56 |
Correct |
87 ms |
16332 KB |
Output is correct |
57 |
Correct |
87 ms |
16340 KB |
Output is correct |
58 |
Correct |
88 ms |
16340 KB |
Output is correct |
59 |
Correct |
95 ms |
16336 KB |
Output is correct |
60 |
Correct |
86 ms |
16332 KB |
Output is correct |
61 |
Correct |
75 ms |
16264 KB |
Output is correct |
62 |
Correct |
24 ms |
16212 KB |
Output is correct |
63 |
Correct |
79 ms |
16284 KB |
Output is correct |
64 |
Correct |
79 ms |
16220 KB |
Output is correct |
65 |
Correct |
70 ms |
16400 KB |
Output is correct |
66 |
Correct |
77 ms |
16268 KB |
Output is correct |
67 |
Correct |
86 ms |
16340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
118 ms |
41164 KB |
Output is correct |
14 |
Correct |
159 ms |
46796 KB |
Output is correct |
15 |
Correct |
193 ms |
53188 KB |
Output is correct |
16 |
Correct |
227 ms |
59960 KB |
Output is correct |
17 |
Correct |
289 ms |
67260 KB |
Output is correct |
18 |
Correct |
281 ms |
67360 KB |
Output is correct |
19 |
Correct |
279 ms |
67348 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 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 |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
584 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
588 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
336 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
972 KB |
Output is correct |
58 |
Correct |
2 ms |
2260 KB |
Output is correct |
59 |
Correct |
4 ms |
2260 KB |
Output is correct |
60 |
Correct |
4 ms |
2256 KB |
Output is correct |
61 |
Correct |
4 ms |
2260 KB |
Output is correct |
62 |
Correct |
4 ms |
2260 KB |
Output is correct |
63 |
Correct |
5 ms |
2260 KB |
Output is correct |
64 |
Correct |
3 ms |
2260 KB |
Output is correct |
65 |
Correct |
4 ms |
2260 KB |
Output is correct |
66 |
Correct |
4 ms |
2260 KB |
Output is correct |
67 |
Correct |
4 ms |
2256 KB |
Output is correct |
68 |
Correct |
3 ms |
2260 KB |
Output is correct |
69 |
Correct |
3 ms |
2260 KB |
Output is correct |
70 |
Correct |
4 ms |
2248 KB |
Output is correct |
71 |
Correct |
17 ms |
5572 KB |
Output is correct |
72 |
Correct |
32 ms |
16216 KB |
Output is correct |
73 |
Correct |
87 ms |
16332 KB |
Output is correct |
74 |
Correct |
87 ms |
16340 KB |
Output is correct |
75 |
Correct |
88 ms |
16340 KB |
Output is correct |
76 |
Correct |
95 ms |
16336 KB |
Output is correct |
77 |
Correct |
86 ms |
16332 KB |
Output is correct |
78 |
Correct |
75 ms |
16264 KB |
Output is correct |
79 |
Correct |
24 ms |
16212 KB |
Output is correct |
80 |
Correct |
79 ms |
16284 KB |
Output is correct |
81 |
Correct |
79 ms |
16220 KB |
Output is correct |
82 |
Correct |
70 ms |
16400 KB |
Output is correct |
83 |
Correct |
77 ms |
16268 KB |
Output is correct |
84 |
Correct |
86 ms |
16340 KB |
Output is correct |
85 |
Correct |
233 ms |
32820 KB |
Output is correct |
86 |
Correct |
648 ms |
67608 KB |
Output is correct |
87 |
Correct |
635 ms |
67616 KB |
Output is correct |
88 |
Correct |
681 ms |
67620 KB |
Output is correct |
89 |
Correct |
635 ms |
67616 KB |
Output is correct |
90 |
Correct |
634 ms |
67596 KB |
Output is correct |
91 |
Correct |
292 ms |
67388 KB |
Output is correct |
92 |
Correct |
292 ms |
67308 KB |
Output is correct |
93 |
Correct |
564 ms |
67468 KB |
Output is correct |
94 |
Correct |
580 ms |
67420 KB |
Output is correct |
95 |
Correct |
552 ms |
67448 KB |
Output is correct |
96 |
Correct |
558 ms |
67552 KB |
Output is correct |
97 |
Correct |
633 ms |
67616 KB |
Output is correct |