#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) {
// cout << z[j] << " ";
while (pnt <= z[j] && i + pnt <= j) {
nxt[i][pnt++] = j;
}
}
// for (int len = 1; len <= n - i; ++len) {
// cout << nxt[i][len] << " ";
// }
//
// cout << endl;
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];
}
}
// for (int j = i; j < n; ++j) {
// cout << dp[i][j] << " ";
// }
//
// cout << endl;
}
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 |
73708 KB |
Output is correct |
3 |
Correct |
28 ms |
73712 KB |
Output is correct |
4 |
Correct |
28 ms |
73716 KB |
Output is correct |
5 |
Correct |
30 ms |
73688 KB |
Output is correct |
6 |
Correct |
32 ms |
73716 KB |
Output is correct |
7 |
Correct |
30 ms |
73728 KB |
Output is correct |
8 |
Correct |
27 ms |
73676 KB |
Output is correct |
9 |
Correct |
29 ms |
73676 KB |
Output is correct |
10 |
Correct |
30 ms |
73692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
73680 KB |
Output is correct |
2 |
Correct |
28 ms |
73680 KB |
Output is correct |
3 |
Correct |
195 ms |
73760 KB |
Output is correct |
4 |
Correct |
222 ms |
73876 KB |
Output is correct |
5 |
Correct |
278 ms |
73812 KB |
Output is correct |
6 |
Correct |
359 ms |
73764 KB |
Output is correct |
7 |
Correct |
408 ms |
73764 KB |
Output is correct |
8 |
Correct |
432 ms |
73764 KB |
Output is correct |
9 |
Correct |
417 ms |
73804 KB |
Output is correct |
10 |
Correct |
30 ms |
73708 KB |
Output is correct |
11 |
Correct |
33 ms |
73640 KB |
Output is correct |
12 |
Correct |
28 ms |
73664 KB |
Output is correct |
13 |
Correct |
28 ms |
73692 KB |
Output is correct |
14 |
Correct |
28 ms |
73744 KB |
Output is correct |
15 |
Correct |
30 ms |
73680 KB |
Output is correct |
16 |
Correct |
29 ms |
73704 KB |
Output is correct |
17 |
Correct |
27 ms |
73752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73684 KB |
Output is correct |
2 |
Correct |
29 ms |
73708 KB |
Output is correct |
3 |
Correct |
28 ms |
73712 KB |
Output is correct |
4 |
Correct |
28 ms |
73716 KB |
Output is correct |
5 |
Correct |
30 ms |
73688 KB |
Output is correct |
6 |
Correct |
32 ms |
73716 KB |
Output is correct |
7 |
Correct |
30 ms |
73728 KB |
Output is correct |
8 |
Correct |
27 ms |
73676 KB |
Output is correct |
9 |
Correct |
29 ms |
73676 KB |
Output is correct |
10 |
Correct |
30 ms |
73692 KB |
Output is correct |
11 |
Correct |
31 ms |
73692 KB |
Output is correct |
12 |
Correct |
29 ms |
73708 KB |
Output is correct |
13 |
Correct |
30 ms |
73700 KB |
Output is correct |
14 |
Correct |
28 ms |
73740 KB |
Output is correct |
15 |
Correct |
28 ms |
73700 KB |
Output is correct |
16 |
Correct |
29 ms |
73740 KB |
Output is correct |
17 |
Correct |
29 ms |
73700 KB |
Output is correct |
18 |
Correct |
28 ms |
73740 KB |
Output is correct |
19 |
Correct |
29 ms |
73676 KB |
Output is correct |
20 |
Correct |
29 ms |
73712 KB |
Output is correct |
21 |
Correct |
28 ms |
73644 KB |
Output is correct |
22 |
Correct |
27 ms |
73684 KB |
Output is correct |
23 |
Correct |
29 ms |
73720 KB |
Output is correct |
24 |
Correct |
29 ms |
73672 KB |
Output is correct |
25 |
Correct |
34 ms |
73724 KB |
Output is correct |
26 |
Correct |
32 ms |
73684 KB |
Output is correct |
27 |
Correct |
29 ms |
73712 KB |
Output is correct |
28 |
Correct |
29 ms |
73696 KB |
Output is correct |
29 |
Correct |
29 ms |
73708 KB |
Output is correct |
30 |
Correct |
29 ms |
73732 KB |
Output is correct |
31 |
Correct |
29 ms |
73640 KB |
Output is correct |
32 |
Correct |
30 ms |
73716 KB |
Output is correct |
33 |
Correct |
34 ms |
73728 KB |
Output is correct |
34 |
Correct |
29 ms |
73676 KB |
Output is correct |
35 |
Correct |
28 ms |
73684 KB |
Output is correct |
36 |
Correct |
34 ms |
73744 KB |
Output is correct |
37 |
Correct |
32 ms |
73832 KB |
Output is correct |
38 |
Correct |
31 ms |
73676 KB |
Output is correct |
39 |
Correct |
29 ms |
73660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73684 KB |
Output is correct |
2 |
Correct |
29 ms |
73708 KB |
Output is correct |
3 |
Correct |
28 ms |
73712 KB |
Output is correct |
4 |
Correct |
28 ms |
73716 KB |
Output is correct |
5 |
Correct |
30 ms |
73688 KB |
Output is correct |
6 |
Correct |
32 ms |
73716 KB |
Output is correct |
7 |
Correct |
30 ms |
73728 KB |
Output is correct |
8 |
Correct |
27 ms |
73676 KB |
Output is correct |
9 |
Correct |
29 ms |
73676 KB |
Output is correct |
10 |
Correct |
30 ms |
73692 KB |
Output is correct |
11 |
Correct |
31 ms |
73692 KB |
Output is correct |
12 |
Correct |
29 ms |
73708 KB |
Output is correct |
13 |
Correct |
30 ms |
73700 KB |
Output is correct |
14 |
Correct |
28 ms |
73740 KB |
Output is correct |
15 |
Correct |
28 ms |
73700 KB |
Output is correct |
16 |
Correct |
29 ms |
73740 KB |
Output is correct |
17 |
Correct |
29 ms |
73700 KB |
Output is correct |
18 |
Correct |
28 ms |
73740 KB |
Output is correct |
19 |
Correct |
29 ms |
73676 KB |
Output is correct |
20 |
Correct |
29 ms |
73712 KB |
Output is correct |
21 |
Correct |
28 ms |
73644 KB |
Output is correct |
22 |
Correct |
27 ms |
73684 KB |
Output is correct |
23 |
Correct |
29 ms |
73720 KB |
Output is correct |
24 |
Correct |
29 ms |
73672 KB |
Output is correct |
25 |
Correct |
34 ms |
73724 KB |
Output is correct |
26 |
Correct |
32 ms |
73684 KB |
Output is correct |
27 |
Correct |
29 ms |
73712 KB |
Output is correct |
28 |
Correct |
29 ms |
73696 KB |
Output is correct |
29 |
Correct |
29 ms |
73708 KB |
Output is correct |
30 |
Correct |
29 ms |
73732 KB |
Output is correct |
31 |
Correct |
29 ms |
73640 KB |
Output is correct |
32 |
Correct |
30 ms |
73716 KB |
Output is correct |
33 |
Correct |
34 ms |
73728 KB |
Output is correct |
34 |
Correct |
29 ms |
73676 KB |
Output is correct |
35 |
Correct |
28 ms |
73684 KB |
Output is correct |
36 |
Correct |
34 ms |
73744 KB |
Output is correct |
37 |
Correct |
32 ms |
73832 KB |
Output is correct |
38 |
Correct |
31 ms |
73676 KB |
Output is correct |
39 |
Correct |
29 ms |
73660 KB |
Output is correct |
40 |
Correct |
32 ms |
73644 KB |
Output is correct |
41 |
Correct |
30 ms |
73708 KB |
Output is correct |
42 |
Correct |
31 ms |
73684 KB |
Output is correct |
43 |
Correct |
29 ms |
73708 KB |
Output is correct |
44 |
Correct |
30 ms |
73756 KB |
Output is correct |
45 |
Correct |
34 ms |
73684 KB |
Output is correct |
46 |
Correct |
30 ms |
73684 KB |
Output is correct |
47 |
Correct |
31 ms |
73656 KB |
Output is correct |
48 |
Correct |
31 ms |
73704 KB |
Output is correct |
49 |
Correct |
36 ms |
73656 KB |
Output is correct |
50 |
Correct |
31 ms |
73680 KB |
Output is correct |
51 |
Correct |
29 ms |
73728 KB |
Output is correct |
52 |
Correct |
29 ms |
73692 KB |
Output is correct |
53 |
Correct |
28 ms |
73720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73684 KB |
Output is correct |
2 |
Correct |
29 ms |
73708 KB |
Output is correct |
3 |
Correct |
28 ms |
73712 KB |
Output is correct |
4 |
Correct |
28 ms |
73716 KB |
Output is correct |
5 |
Correct |
30 ms |
73688 KB |
Output is correct |
6 |
Correct |
32 ms |
73716 KB |
Output is correct |
7 |
Correct |
30 ms |
73728 KB |
Output is correct |
8 |
Correct |
27 ms |
73676 KB |
Output is correct |
9 |
Correct |
29 ms |
73676 KB |
Output is correct |
10 |
Correct |
30 ms |
73692 KB |
Output is correct |
11 |
Correct |
31 ms |
73692 KB |
Output is correct |
12 |
Correct |
29 ms |
73708 KB |
Output is correct |
13 |
Correct |
30 ms |
73700 KB |
Output is correct |
14 |
Correct |
28 ms |
73740 KB |
Output is correct |
15 |
Correct |
28 ms |
73700 KB |
Output is correct |
16 |
Correct |
29 ms |
73740 KB |
Output is correct |
17 |
Correct |
29 ms |
73700 KB |
Output is correct |
18 |
Correct |
28 ms |
73740 KB |
Output is correct |
19 |
Correct |
29 ms |
73676 KB |
Output is correct |
20 |
Correct |
29 ms |
73712 KB |
Output is correct |
21 |
Correct |
28 ms |
73644 KB |
Output is correct |
22 |
Correct |
27 ms |
73684 KB |
Output is correct |
23 |
Correct |
29 ms |
73720 KB |
Output is correct |
24 |
Correct |
29 ms |
73672 KB |
Output is correct |
25 |
Correct |
34 ms |
73724 KB |
Output is correct |
26 |
Correct |
32 ms |
73684 KB |
Output is correct |
27 |
Correct |
29 ms |
73712 KB |
Output is correct |
28 |
Correct |
29 ms |
73696 KB |
Output is correct |
29 |
Correct |
29 ms |
73708 KB |
Output is correct |
30 |
Correct |
29 ms |
73732 KB |
Output is correct |
31 |
Correct |
29 ms |
73640 KB |
Output is correct |
32 |
Correct |
30 ms |
73716 KB |
Output is correct |
33 |
Correct |
34 ms |
73728 KB |
Output is correct |
34 |
Correct |
29 ms |
73676 KB |
Output is correct |
35 |
Correct |
28 ms |
73684 KB |
Output is correct |
36 |
Correct |
34 ms |
73744 KB |
Output is correct |
37 |
Correct |
32 ms |
73832 KB |
Output is correct |
38 |
Correct |
31 ms |
73676 KB |
Output is correct |
39 |
Correct |
29 ms |
73660 KB |
Output is correct |
40 |
Correct |
32 ms |
73644 KB |
Output is correct |
41 |
Correct |
30 ms |
73708 KB |
Output is correct |
42 |
Correct |
31 ms |
73684 KB |
Output is correct |
43 |
Correct |
29 ms |
73708 KB |
Output is correct |
44 |
Correct |
30 ms |
73756 KB |
Output is correct |
45 |
Correct |
34 ms |
73684 KB |
Output is correct |
46 |
Correct |
30 ms |
73684 KB |
Output is correct |
47 |
Correct |
31 ms |
73656 KB |
Output is correct |
48 |
Correct |
31 ms |
73704 KB |
Output is correct |
49 |
Correct |
36 ms |
73656 KB |
Output is correct |
50 |
Correct |
31 ms |
73680 KB |
Output is correct |
51 |
Correct |
29 ms |
73728 KB |
Output is correct |
52 |
Correct |
29 ms |
73692 KB |
Output is correct |
53 |
Correct |
28 ms |
73720 KB |
Output is correct |
54 |
Correct |
31 ms |
73676 KB |
Output is correct |
55 |
Correct |
67 ms |
73768 KB |
Output is correct |
56 |
Correct |
42 ms |
73676 KB |
Output is correct |
57 |
Correct |
38 ms |
73720 KB |
Output is correct |
58 |
Correct |
38 ms |
73752 KB |
Output is correct |
59 |
Correct |
35 ms |
73752 KB |
Output is correct |
60 |
Correct |
34 ms |
73744 KB |
Output is correct |
61 |
Correct |
36 ms |
73664 KB |
Output is correct |
62 |
Correct |
50 ms |
73672 KB |
Output is correct |
63 |
Correct |
35 ms |
73668 KB |
Output is correct |
64 |
Correct |
39 ms |
73828 KB |
Output is correct |
65 |
Correct |
45 ms |
73764 KB |
Output is correct |
66 |
Correct |
43 ms |
73804 KB |
Output is correct |
67 |
Correct |
36 ms |
73668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
73684 KB |
Output is correct |
2 |
Correct |
29 ms |
73708 KB |
Output is correct |
3 |
Correct |
28 ms |
73712 KB |
Output is correct |
4 |
Correct |
28 ms |
73716 KB |
Output is correct |
5 |
Correct |
30 ms |
73688 KB |
Output is correct |
6 |
Correct |
32 ms |
73716 KB |
Output is correct |
7 |
Correct |
30 ms |
73728 KB |
Output is correct |
8 |
Correct |
27 ms |
73676 KB |
Output is correct |
9 |
Correct |
29 ms |
73676 KB |
Output is correct |
10 |
Correct |
30 ms |
73692 KB |
Output is correct |
11 |
Correct |
32 ms |
73680 KB |
Output is correct |
12 |
Correct |
28 ms |
73680 KB |
Output is correct |
13 |
Correct |
195 ms |
73760 KB |
Output is correct |
14 |
Correct |
222 ms |
73876 KB |
Output is correct |
15 |
Correct |
278 ms |
73812 KB |
Output is correct |
16 |
Correct |
359 ms |
73764 KB |
Output is correct |
17 |
Correct |
408 ms |
73764 KB |
Output is correct |
18 |
Correct |
432 ms |
73764 KB |
Output is correct |
19 |
Correct |
417 ms |
73804 KB |
Output is correct |
20 |
Correct |
30 ms |
73708 KB |
Output is correct |
21 |
Correct |
33 ms |
73640 KB |
Output is correct |
22 |
Correct |
28 ms |
73664 KB |
Output is correct |
23 |
Correct |
28 ms |
73692 KB |
Output is correct |
24 |
Correct |
28 ms |
73744 KB |
Output is correct |
25 |
Correct |
30 ms |
73680 KB |
Output is correct |
26 |
Correct |
29 ms |
73704 KB |
Output is correct |
27 |
Correct |
27 ms |
73752 KB |
Output is correct |
28 |
Correct |
31 ms |
73692 KB |
Output is correct |
29 |
Correct |
29 ms |
73708 KB |
Output is correct |
30 |
Correct |
30 ms |
73700 KB |
Output is correct |
31 |
Correct |
28 ms |
73740 KB |
Output is correct |
32 |
Correct |
28 ms |
73700 KB |
Output is correct |
33 |
Correct |
29 ms |
73740 KB |
Output is correct |
34 |
Correct |
29 ms |
73700 KB |
Output is correct |
35 |
Correct |
28 ms |
73740 KB |
Output is correct |
36 |
Correct |
29 ms |
73676 KB |
Output is correct |
37 |
Correct |
29 ms |
73712 KB |
Output is correct |
38 |
Correct |
28 ms |
73644 KB |
Output is correct |
39 |
Correct |
27 ms |
73684 KB |
Output is correct |
40 |
Correct |
29 ms |
73720 KB |
Output is correct |
41 |
Correct |
29 ms |
73672 KB |
Output is correct |
42 |
Correct |
34 ms |
73724 KB |
Output is correct |
43 |
Correct |
32 ms |
73684 KB |
Output is correct |
44 |
Correct |
29 ms |
73712 KB |
Output is correct |
45 |
Correct |
29 ms |
73696 KB |
Output is correct |
46 |
Correct |
29 ms |
73708 KB |
Output is correct |
47 |
Correct |
29 ms |
73732 KB |
Output is correct |
48 |
Correct |
29 ms |
73640 KB |
Output is correct |
49 |
Correct |
30 ms |
73716 KB |
Output is correct |
50 |
Correct |
34 ms |
73728 KB |
Output is correct |
51 |
Correct |
29 ms |
73676 KB |
Output is correct |
52 |
Correct |
28 ms |
73684 KB |
Output is correct |
53 |
Correct |
34 ms |
73744 KB |
Output is correct |
54 |
Correct |
32 ms |
73832 KB |
Output is correct |
55 |
Correct |
31 ms |
73676 KB |
Output is correct |
56 |
Correct |
29 ms |
73660 KB |
Output is correct |
57 |
Correct |
32 ms |
73644 KB |
Output is correct |
58 |
Correct |
30 ms |
73708 KB |
Output is correct |
59 |
Correct |
31 ms |
73684 KB |
Output is correct |
60 |
Correct |
29 ms |
73708 KB |
Output is correct |
61 |
Correct |
30 ms |
73756 KB |
Output is correct |
62 |
Correct |
34 ms |
73684 KB |
Output is correct |
63 |
Correct |
30 ms |
73684 KB |
Output is correct |
64 |
Correct |
31 ms |
73656 KB |
Output is correct |
65 |
Correct |
31 ms |
73704 KB |
Output is correct |
66 |
Correct |
36 ms |
73656 KB |
Output is correct |
67 |
Correct |
31 ms |
73680 KB |
Output is correct |
68 |
Correct |
29 ms |
73728 KB |
Output is correct |
69 |
Correct |
29 ms |
73692 KB |
Output is correct |
70 |
Correct |
28 ms |
73720 KB |
Output is correct |
71 |
Correct |
31 ms |
73676 KB |
Output is correct |
72 |
Correct |
67 ms |
73768 KB |
Output is correct |
73 |
Correct |
42 ms |
73676 KB |
Output is correct |
74 |
Correct |
38 ms |
73720 KB |
Output is correct |
75 |
Correct |
38 ms |
73752 KB |
Output is correct |
76 |
Correct |
35 ms |
73752 KB |
Output is correct |
77 |
Correct |
34 ms |
73744 KB |
Output is correct |
78 |
Correct |
36 ms |
73664 KB |
Output is correct |
79 |
Correct |
50 ms |
73672 KB |
Output is correct |
80 |
Correct |
35 ms |
73668 KB |
Output is correct |
81 |
Correct |
39 ms |
73828 KB |
Output is correct |
82 |
Correct |
45 ms |
73764 KB |
Output is correct |
83 |
Correct |
43 ms |
73804 KB |
Output is correct |
84 |
Correct |
36 ms |
73668 KB |
Output is correct |
85 |
Correct |
58 ms |
73684 KB |
Output is correct |
86 |
Correct |
116 ms |
73760 KB |
Output is correct |
87 |
Correct |
91 ms |
73684 KB |
Output is correct |
88 |
Correct |
72 ms |
73764 KB |
Output is correct |
89 |
Correct |
62 ms |
73760 KB |
Output is correct |
90 |
Correct |
59 ms |
73684 KB |
Output is correct |
91 |
Correct |
107 ms |
73768 KB |
Output is correct |
92 |
Correct |
148 ms |
73676 KB |
Output is correct |
93 |
Correct |
68 ms |
73764 KB |
Output is correct |
94 |
Correct |
76 ms |
73764 KB |
Output is correct |
95 |
Correct |
126 ms |
73668 KB |
Output is correct |
96 |
Correct |
127 ms |
73856 KB |
Output is correct |
97 |
Correct |
59 ms |
73644 KB |
Output is correct |