#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 2501;
ll dp[N][N];
int nxt[N][N], z[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
memset(nxt, -1, sizeof(nxt));
int n;
cin >> n;
string s;
cin >> s;
int A, B, C;
cin >> A >> B >> C;
auto z_function = [&](int i) {
memset(z, 0, sizeof(z));
int l = i, r = i;
for (int j = i + 1; j < n; ++j) {
int k = 0;
if (r > j) {
k = min(r - j, z[i + (j - l)]);
}
while (j + k < n && s[j + k] == s[i + k]) {
k += 1;
}
z[j] = k;
if (j + k > r) {
r = j + k;
l = j;
}
}
};
memset(dp, 0x3f, sizeof(dp));
for (int i = n - 1; i >= 0; --i) {
z_function(i);
int pnt = 1;
for (int j = i + 1; j < n; ++j) {
while (pnt <= z[j] && i + pnt <= j) {
nxt[i][pnt++] = j;
}
}
if (i + 1 < n) {
for (int j = i + 1; j < n; ++j) {
dp[i][j] = dp[i + 1][j] + A;
}
}
dp[i][i] = A;
for (int j = i; j < n; ++j) {
if (j > 0) {
dp[i][j] = min(dp[i][j], dp[i][j - 1] + A);
}
int len = j - i + 1;
int x = nxt[i][len];
int cnt = 1;
while (x != -1) {
cnt += 1;
int r = x + len - 1;
dp[i][r] = min(dp[i][r], 1LL * (r - i + 1 - cnt * len) * A + dp[i][j] + B + 1LL * C * cnt);
x = nxt[x][len];
}
}
}
cout << dp[0][n - 1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73684 KB |
Output is correct |
2 |
Correct |
29 ms |
73684 KB |
Output is correct |
3 |
Correct |
30 ms |
73716 KB |
Output is correct |
4 |
Correct |
28 ms |
73676 KB |
Output is correct |
5 |
Correct |
28 ms |
73684 KB |
Output is correct |
6 |
Correct |
29 ms |
73752 KB |
Output is correct |
7 |
Correct |
32 ms |
73676 KB |
Output is correct |
8 |
Correct |
29 ms |
73744 KB |
Output is correct |
9 |
Correct |
29 ms |
73672 KB |
Output is correct |
10 |
Correct |
29 ms |
73728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
73676 KB |
Output is correct |
2 |
Correct |
31 ms |
73676 KB |
Output is correct |
3 |
Correct |
201 ms |
73748 KB |
Output is correct |
4 |
Correct |
242 ms |
73712 KB |
Output is correct |
5 |
Correct |
294 ms |
73684 KB |
Output is correct |
6 |
Correct |
340 ms |
73760 KB |
Output is correct |
7 |
Correct |
440 ms |
73764 KB |
Output is correct |
8 |
Correct |
421 ms |
73760 KB |
Output is correct |
9 |
Correct |
442 ms |
73672 KB |
Output is correct |
10 |
Correct |
34 ms |
73684 KB |
Output is correct |
11 |
Correct |
29 ms |
73716 KB |
Output is correct |
12 |
Correct |
29 ms |
73736 KB |
Output is correct |
13 |
Correct |
30 ms |
73664 KB |
Output is correct |
14 |
Correct |
30 ms |
73680 KB |
Output is correct |
15 |
Correct |
31 ms |
73676 KB |
Output is correct |
16 |
Correct |
34 ms |
73692 KB |
Output is correct |
17 |
Correct |
32 ms |
73732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73684 KB |
Output is correct |
2 |
Correct |
29 ms |
73684 KB |
Output is correct |
3 |
Correct |
30 ms |
73716 KB |
Output is correct |
4 |
Correct |
28 ms |
73676 KB |
Output is correct |
5 |
Correct |
28 ms |
73684 KB |
Output is correct |
6 |
Correct |
29 ms |
73752 KB |
Output is correct |
7 |
Correct |
32 ms |
73676 KB |
Output is correct |
8 |
Correct |
29 ms |
73744 KB |
Output is correct |
9 |
Correct |
29 ms |
73672 KB |
Output is correct |
10 |
Correct |
29 ms |
73728 KB |
Output is correct |
11 |
Correct |
34 ms |
73644 KB |
Output is correct |
12 |
Correct |
30 ms |
73684 KB |
Output is correct |
13 |
Correct |
29 ms |
73752 KB |
Output is correct |
14 |
Correct |
30 ms |
73724 KB |
Output is correct |
15 |
Correct |
30 ms |
73752 KB |
Output is correct |
16 |
Correct |
30 ms |
73716 KB |
Output is correct |
17 |
Correct |
32 ms |
73676 KB |
Output is correct |
18 |
Correct |
32 ms |
73756 KB |
Output is correct |
19 |
Correct |
30 ms |
73728 KB |
Output is correct |
20 |
Correct |
28 ms |
73684 KB |
Output is correct |
21 |
Correct |
29 ms |
73756 KB |
Output is correct |
22 |
Correct |
32 ms |
73696 KB |
Output is correct |
23 |
Correct |
29 ms |
73668 KB |
Output is correct |
24 |
Correct |
29 ms |
73764 KB |
Output is correct |
25 |
Correct |
28 ms |
73660 KB |
Output is correct |
26 |
Correct |
37 ms |
73720 KB |
Output is correct |
27 |
Correct |
34 ms |
73680 KB |
Output is correct |
28 |
Correct |
29 ms |
73724 KB |
Output is correct |
29 |
Correct |
30 ms |
73744 KB |
Output is correct |
30 |
Correct |
36 ms |
73724 KB |
Output is correct |
31 |
Correct |
32 ms |
73804 KB |
Output is correct |
32 |
Correct |
30 ms |
73744 KB |
Output is correct |
33 |
Correct |
31 ms |
73760 KB |
Output is correct |
34 |
Correct |
30 ms |
73628 KB |
Output is correct |
35 |
Correct |
32 ms |
73688 KB |
Output is correct |
36 |
Correct |
29 ms |
73684 KB |
Output is correct |
37 |
Correct |
30 ms |
73696 KB |
Output is correct |
38 |
Correct |
29 ms |
73756 KB |
Output is correct |
39 |
Correct |
34 ms |
73712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73684 KB |
Output is correct |
2 |
Correct |
29 ms |
73684 KB |
Output is correct |
3 |
Correct |
30 ms |
73716 KB |
Output is correct |
4 |
Correct |
28 ms |
73676 KB |
Output is correct |
5 |
Correct |
28 ms |
73684 KB |
Output is correct |
6 |
Correct |
29 ms |
73752 KB |
Output is correct |
7 |
Correct |
32 ms |
73676 KB |
Output is correct |
8 |
Correct |
29 ms |
73744 KB |
Output is correct |
9 |
Correct |
29 ms |
73672 KB |
Output is correct |
10 |
Correct |
29 ms |
73728 KB |
Output is correct |
11 |
Correct |
34 ms |
73644 KB |
Output is correct |
12 |
Correct |
30 ms |
73684 KB |
Output is correct |
13 |
Correct |
29 ms |
73752 KB |
Output is correct |
14 |
Correct |
30 ms |
73724 KB |
Output is correct |
15 |
Correct |
30 ms |
73752 KB |
Output is correct |
16 |
Correct |
30 ms |
73716 KB |
Output is correct |
17 |
Correct |
32 ms |
73676 KB |
Output is correct |
18 |
Correct |
32 ms |
73756 KB |
Output is correct |
19 |
Correct |
30 ms |
73728 KB |
Output is correct |
20 |
Correct |
28 ms |
73684 KB |
Output is correct |
21 |
Correct |
29 ms |
73756 KB |
Output is correct |
22 |
Correct |
32 ms |
73696 KB |
Output is correct |
23 |
Correct |
29 ms |
73668 KB |
Output is correct |
24 |
Correct |
29 ms |
73764 KB |
Output is correct |
25 |
Correct |
28 ms |
73660 KB |
Output is correct |
26 |
Correct |
37 ms |
73720 KB |
Output is correct |
27 |
Correct |
34 ms |
73680 KB |
Output is correct |
28 |
Correct |
29 ms |
73724 KB |
Output is correct |
29 |
Correct |
30 ms |
73744 KB |
Output is correct |
30 |
Correct |
36 ms |
73724 KB |
Output is correct |
31 |
Correct |
32 ms |
73804 KB |
Output is correct |
32 |
Correct |
30 ms |
73744 KB |
Output is correct |
33 |
Correct |
31 ms |
73760 KB |
Output is correct |
34 |
Correct |
30 ms |
73628 KB |
Output is correct |
35 |
Correct |
32 ms |
73688 KB |
Output is correct |
36 |
Correct |
29 ms |
73684 KB |
Output is correct |
37 |
Correct |
30 ms |
73696 KB |
Output is correct |
38 |
Correct |
29 ms |
73756 KB |
Output is correct |
39 |
Correct |
34 ms |
73712 KB |
Output is correct |
40 |
Correct |
32 ms |
73684 KB |
Output is correct |
41 |
Correct |
31 ms |
73724 KB |
Output is correct |
42 |
Correct |
30 ms |
73684 KB |
Output is correct |
43 |
Correct |
31 ms |
73804 KB |
Output is correct |
44 |
Correct |
31 ms |
73708 KB |
Output is correct |
45 |
Correct |
28 ms |
73772 KB |
Output is correct |
46 |
Correct |
29 ms |
73684 KB |
Output is correct |
47 |
Correct |
29 ms |
73684 KB |
Output is correct |
48 |
Correct |
29 ms |
73736 KB |
Output is correct |
49 |
Correct |
30 ms |
73708 KB |
Output is correct |
50 |
Correct |
30 ms |
73668 KB |
Output is correct |
51 |
Correct |
30 ms |
73740 KB |
Output is correct |
52 |
Correct |
30 ms |
73644 KB |
Output is correct |
53 |
Correct |
30 ms |
73744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73684 KB |
Output is correct |
2 |
Correct |
29 ms |
73684 KB |
Output is correct |
3 |
Correct |
30 ms |
73716 KB |
Output is correct |
4 |
Correct |
28 ms |
73676 KB |
Output is correct |
5 |
Correct |
28 ms |
73684 KB |
Output is correct |
6 |
Correct |
29 ms |
73752 KB |
Output is correct |
7 |
Correct |
32 ms |
73676 KB |
Output is correct |
8 |
Correct |
29 ms |
73744 KB |
Output is correct |
9 |
Correct |
29 ms |
73672 KB |
Output is correct |
10 |
Correct |
29 ms |
73728 KB |
Output is correct |
11 |
Correct |
34 ms |
73644 KB |
Output is correct |
12 |
Correct |
30 ms |
73684 KB |
Output is correct |
13 |
Correct |
29 ms |
73752 KB |
Output is correct |
14 |
Correct |
30 ms |
73724 KB |
Output is correct |
15 |
Correct |
30 ms |
73752 KB |
Output is correct |
16 |
Correct |
30 ms |
73716 KB |
Output is correct |
17 |
Correct |
32 ms |
73676 KB |
Output is correct |
18 |
Correct |
32 ms |
73756 KB |
Output is correct |
19 |
Correct |
30 ms |
73728 KB |
Output is correct |
20 |
Correct |
28 ms |
73684 KB |
Output is correct |
21 |
Correct |
29 ms |
73756 KB |
Output is correct |
22 |
Correct |
32 ms |
73696 KB |
Output is correct |
23 |
Correct |
29 ms |
73668 KB |
Output is correct |
24 |
Correct |
29 ms |
73764 KB |
Output is correct |
25 |
Correct |
28 ms |
73660 KB |
Output is correct |
26 |
Correct |
37 ms |
73720 KB |
Output is correct |
27 |
Correct |
34 ms |
73680 KB |
Output is correct |
28 |
Correct |
29 ms |
73724 KB |
Output is correct |
29 |
Correct |
30 ms |
73744 KB |
Output is correct |
30 |
Correct |
36 ms |
73724 KB |
Output is correct |
31 |
Correct |
32 ms |
73804 KB |
Output is correct |
32 |
Correct |
30 ms |
73744 KB |
Output is correct |
33 |
Correct |
31 ms |
73760 KB |
Output is correct |
34 |
Correct |
30 ms |
73628 KB |
Output is correct |
35 |
Correct |
32 ms |
73688 KB |
Output is correct |
36 |
Correct |
29 ms |
73684 KB |
Output is correct |
37 |
Correct |
30 ms |
73696 KB |
Output is correct |
38 |
Correct |
29 ms |
73756 KB |
Output is correct |
39 |
Correct |
34 ms |
73712 KB |
Output is correct |
40 |
Correct |
32 ms |
73684 KB |
Output is correct |
41 |
Correct |
31 ms |
73724 KB |
Output is correct |
42 |
Correct |
30 ms |
73684 KB |
Output is correct |
43 |
Correct |
31 ms |
73804 KB |
Output is correct |
44 |
Correct |
31 ms |
73708 KB |
Output is correct |
45 |
Correct |
28 ms |
73772 KB |
Output is correct |
46 |
Correct |
29 ms |
73684 KB |
Output is correct |
47 |
Correct |
29 ms |
73684 KB |
Output is correct |
48 |
Correct |
29 ms |
73736 KB |
Output is correct |
49 |
Correct |
30 ms |
73708 KB |
Output is correct |
50 |
Correct |
30 ms |
73668 KB |
Output is correct |
51 |
Correct |
30 ms |
73740 KB |
Output is correct |
52 |
Correct |
30 ms |
73644 KB |
Output is correct |
53 |
Correct |
30 ms |
73744 KB |
Output is correct |
54 |
Correct |
31 ms |
73720 KB |
Output is correct |
55 |
Correct |
60 ms |
73756 KB |
Output is correct |
56 |
Correct |
38 ms |
73676 KB |
Output is correct |
57 |
Correct |
39 ms |
73684 KB |
Output is correct |
58 |
Correct |
35 ms |
73660 KB |
Output is correct |
59 |
Correct |
36 ms |
73680 KB |
Output is correct |
60 |
Correct |
36 ms |
73708 KB |
Output is correct |
61 |
Correct |
39 ms |
73832 KB |
Output is correct |
62 |
Correct |
54 ms |
73836 KB |
Output is correct |
63 |
Correct |
41 ms |
73700 KB |
Output is correct |
64 |
Correct |
38 ms |
73684 KB |
Output is correct |
65 |
Correct |
43 ms |
73732 KB |
Output is correct |
66 |
Correct |
43 ms |
73744 KB |
Output is correct |
67 |
Correct |
39 ms |
73672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73684 KB |
Output is correct |
2 |
Correct |
29 ms |
73684 KB |
Output is correct |
3 |
Correct |
30 ms |
73716 KB |
Output is correct |
4 |
Correct |
28 ms |
73676 KB |
Output is correct |
5 |
Correct |
28 ms |
73684 KB |
Output is correct |
6 |
Correct |
29 ms |
73752 KB |
Output is correct |
7 |
Correct |
32 ms |
73676 KB |
Output is correct |
8 |
Correct |
29 ms |
73744 KB |
Output is correct |
9 |
Correct |
29 ms |
73672 KB |
Output is correct |
10 |
Correct |
29 ms |
73728 KB |
Output is correct |
11 |
Correct |
29 ms |
73676 KB |
Output is correct |
12 |
Correct |
31 ms |
73676 KB |
Output is correct |
13 |
Correct |
201 ms |
73748 KB |
Output is correct |
14 |
Correct |
242 ms |
73712 KB |
Output is correct |
15 |
Correct |
294 ms |
73684 KB |
Output is correct |
16 |
Correct |
340 ms |
73760 KB |
Output is correct |
17 |
Correct |
440 ms |
73764 KB |
Output is correct |
18 |
Correct |
421 ms |
73760 KB |
Output is correct |
19 |
Correct |
442 ms |
73672 KB |
Output is correct |
20 |
Correct |
34 ms |
73684 KB |
Output is correct |
21 |
Correct |
29 ms |
73716 KB |
Output is correct |
22 |
Correct |
29 ms |
73736 KB |
Output is correct |
23 |
Correct |
30 ms |
73664 KB |
Output is correct |
24 |
Correct |
30 ms |
73680 KB |
Output is correct |
25 |
Correct |
31 ms |
73676 KB |
Output is correct |
26 |
Correct |
34 ms |
73692 KB |
Output is correct |
27 |
Correct |
32 ms |
73732 KB |
Output is correct |
28 |
Correct |
34 ms |
73644 KB |
Output is correct |
29 |
Correct |
30 ms |
73684 KB |
Output is correct |
30 |
Correct |
29 ms |
73752 KB |
Output is correct |
31 |
Correct |
30 ms |
73724 KB |
Output is correct |
32 |
Correct |
30 ms |
73752 KB |
Output is correct |
33 |
Correct |
30 ms |
73716 KB |
Output is correct |
34 |
Correct |
32 ms |
73676 KB |
Output is correct |
35 |
Correct |
32 ms |
73756 KB |
Output is correct |
36 |
Correct |
30 ms |
73728 KB |
Output is correct |
37 |
Correct |
28 ms |
73684 KB |
Output is correct |
38 |
Correct |
29 ms |
73756 KB |
Output is correct |
39 |
Correct |
32 ms |
73696 KB |
Output is correct |
40 |
Correct |
29 ms |
73668 KB |
Output is correct |
41 |
Correct |
29 ms |
73764 KB |
Output is correct |
42 |
Correct |
28 ms |
73660 KB |
Output is correct |
43 |
Correct |
37 ms |
73720 KB |
Output is correct |
44 |
Correct |
34 ms |
73680 KB |
Output is correct |
45 |
Correct |
29 ms |
73724 KB |
Output is correct |
46 |
Correct |
30 ms |
73744 KB |
Output is correct |
47 |
Correct |
36 ms |
73724 KB |
Output is correct |
48 |
Correct |
32 ms |
73804 KB |
Output is correct |
49 |
Correct |
30 ms |
73744 KB |
Output is correct |
50 |
Correct |
31 ms |
73760 KB |
Output is correct |
51 |
Correct |
30 ms |
73628 KB |
Output is correct |
52 |
Correct |
32 ms |
73688 KB |
Output is correct |
53 |
Correct |
29 ms |
73684 KB |
Output is correct |
54 |
Correct |
30 ms |
73696 KB |
Output is correct |
55 |
Correct |
29 ms |
73756 KB |
Output is correct |
56 |
Correct |
34 ms |
73712 KB |
Output is correct |
57 |
Correct |
32 ms |
73684 KB |
Output is correct |
58 |
Correct |
31 ms |
73724 KB |
Output is correct |
59 |
Correct |
30 ms |
73684 KB |
Output is correct |
60 |
Correct |
31 ms |
73804 KB |
Output is correct |
61 |
Correct |
31 ms |
73708 KB |
Output is correct |
62 |
Correct |
28 ms |
73772 KB |
Output is correct |
63 |
Correct |
29 ms |
73684 KB |
Output is correct |
64 |
Correct |
29 ms |
73684 KB |
Output is correct |
65 |
Correct |
29 ms |
73736 KB |
Output is correct |
66 |
Correct |
30 ms |
73708 KB |
Output is correct |
67 |
Correct |
30 ms |
73668 KB |
Output is correct |
68 |
Correct |
30 ms |
73740 KB |
Output is correct |
69 |
Correct |
30 ms |
73644 KB |
Output is correct |
70 |
Correct |
30 ms |
73744 KB |
Output is correct |
71 |
Correct |
31 ms |
73720 KB |
Output is correct |
72 |
Correct |
60 ms |
73756 KB |
Output is correct |
73 |
Correct |
38 ms |
73676 KB |
Output is correct |
74 |
Correct |
39 ms |
73684 KB |
Output is correct |
75 |
Correct |
35 ms |
73660 KB |
Output is correct |
76 |
Correct |
36 ms |
73680 KB |
Output is correct |
77 |
Correct |
36 ms |
73708 KB |
Output is correct |
78 |
Correct |
39 ms |
73832 KB |
Output is correct |
79 |
Correct |
54 ms |
73836 KB |
Output is correct |
80 |
Correct |
41 ms |
73700 KB |
Output is correct |
81 |
Correct |
38 ms |
73684 KB |
Output is correct |
82 |
Correct |
43 ms |
73732 KB |
Output is correct |
83 |
Correct |
43 ms |
73744 KB |
Output is correct |
84 |
Correct |
39 ms |
73672 KB |
Output is correct |
85 |
Correct |
65 ms |
73696 KB |
Output is correct |
86 |
Correct |
120 ms |
73760 KB |
Output is correct |
87 |
Correct |
99 ms |
73772 KB |
Output is correct |
88 |
Correct |
81 ms |
73732 KB |
Output is correct |
89 |
Correct |
71 ms |
73744 KB |
Output is correct |
90 |
Correct |
67 ms |
73756 KB |
Output is correct |
91 |
Correct |
123 ms |
73760 KB |
Output is correct |
92 |
Correct |
145 ms |
73768 KB |
Output is correct |
93 |
Correct |
69 ms |
73760 KB |
Output is correct |
94 |
Correct |
77 ms |
73736 KB |
Output is correct |
95 |
Correct |
125 ms |
73768 KB |
Output is correct |
96 |
Correct |
133 ms |
73764 KB |
Output is correct |
97 |
Correct |
61 ms |
73684 KB |
Output is correct |