#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
const int N = 2507;
int n;
string s;
ll a, b, c;
ll dp[N][N];
int lcp[N][N], nxt[N][N];
int main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n >> s >> a >> b >> c;
s = " " + s;
for (int i = n; i >= 1; i--)
for (int j = n; j > i; j--)
if (s[i] == s[j])
lcp[i][j] = min(lcp[i + 1][j + 1] + 1, j - i);
for (int i = 1; i <= n; i++) {
vector<int> bst(n + 1, n + 1);
int val = n + 1;
for (int j = n; j >= i; j--) {
nxt[i][j] = val;
bst[lcp[i][j]] = j;
if (lcp[i][j] >= j - i)
val = j;
val = min(val, bst[j - i]);
}
}
memset(dp, 0x3f, sizeof(dp));
for (int i = n; i >= 1; i--) {
dp[i][i] = a;
for (int j = i; j <= n; j++) {
dp[i - 1][j] = min(dp[i - 1][j], dp[i][j] + a);
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + a);
int r = j;
ll cost = b + c;
while (nxt[r - (j - i)][r] <= n) {
int skip = nxt[r - (j - i)][r] - r - 1;
cost += a * skip + c;
r += skip + 1 + (j - i);
dp[i][r] = min(dp[i][r], dp[i][j] + cost);
}
}
}
cout << dp[1][n] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
51288 KB |
Output is correct |
2 |
Correct |
7 ms |
51292 KB |
Output is correct |
3 |
Correct |
7 ms |
51460 KB |
Output is correct |
4 |
Correct |
7 ms |
51548 KB |
Output is correct |
5 |
Correct |
7 ms |
51292 KB |
Output is correct |
6 |
Correct |
7 ms |
51292 KB |
Output is correct |
7 |
Correct |
7 ms |
51316 KB |
Output is correct |
8 |
Correct |
7 ms |
51292 KB |
Output is correct |
9 |
Correct |
7 ms |
51284 KB |
Output is correct |
10 |
Correct |
7 ms |
51288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
51548 KB |
Output is correct |
2 |
Correct |
7 ms |
51292 KB |
Output is correct |
3 |
Correct |
121 ms |
89432 KB |
Output is correct |
4 |
Correct |
132 ms |
92768 KB |
Output is correct |
5 |
Correct |
166 ms |
96900 KB |
Output is correct |
6 |
Correct |
189 ms |
97668 KB |
Output is correct |
7 |
Correct |
232 ms |
97616 KB |
Output is correct |
8 |
Correct |
218 ms |
97628 KB |
Output is correct |
9 |
Correct |
222 ms |
97800 KB |
Output is correct |
10 |
Correct |
7 ms |
51292 KB |
Output is correct |
11 |
Correct |
6 ms |
51292 KB |
Output is correct |
12 |
Correct |
6 ms |
51292 KB |
Output is correct |
13 |
Correct |
7 ms |
51292 KB |
Output is correct |
14 |
Correct |
6 ms |
51548 KB |
Output is correct |
15 |
Correct |
7 ms |
51544 KB |
Output is correct |
16 |
Correct |
7 ms |
55640 KB |
Output is correct |
17 |
Correct |
7 ms |
51292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
51288 KB |
Output is correct |
2 |
Correct |
7 ms |
51292 KB |
Output is correct |
3 |
Correct |
7 ms |
51460 KB |
Output is correct |
4 |
Correct |
7 ms |
51548 KB |
Output is correct |
5 |
Correct |
7 ms |
51292 KB |
Output is correct |
6 |
Correct |
7 ms |
51292 KB |
Output is correct |
7 |
Correct |
7 ms |
51316 KB |
Output is correct |
8 |
Correct |
7 ms |
51292 KB |
Output is correct |
9 |
Correct |
7 ms |
51284 KB |
Output is correct |
10 |
Correct |
7 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
51548 KB |
Output is correct |
12 |
Correct |
7 ms |
51556 KB |
Output is correct |
13 |
Correct |
7 ms |
53596 KB |
Output is correct |
14 |
Correct |
6 ms |
51412 KB |
Output is correct |
15 |
Correct |
7 ms |
53596 KB |
Output is correct |
16 |
Correct |
6 ms |
51496 KB |
Output is correct |
17 |
Correct |
7 ms |
51448 KB |
Output is correct |
18 |
Correct |
7 ms |
51288 KB |
Output is correct |
19 |
Correct |
7 ms |
51548 KB |
Output is correct |
20 |
Correct |
7 ms |
51288 KB |
Output is correct |
21 |
Correct |
8 ms |
55640 KB |
Output is correct |
22 |
Correct |
9 ms |
55648 KB |
Output is correct |
23 |
Correct |
7 ms |
55640 KB |
Output is correct |
24 |
Correct |
7 ms |
55604 KB |
Output is correct |
25 |
Correct |
7 ms |
55896 KB |
Output is correct |
26 |
Correct |
7 ms |
53584 KB |
Output is correct |
27 |
Correct |
7 ms |
55644 KB |
Output is correct |
28 |
Correct |
7 ms |
55644 KB |
Output is correct |
29 |
Correct |
7 ms |
53596 KB |
Output is correct |
30 |
Correct |
7 ms |
55608 KB |
Output is correct |
31 |
Correct |
7 ms |
55632 KB |
Output is correct |
32 |
Correct |
7 ms |
55644 KB |
Output is correct |
33 |
Correct |
7 ms |
53596 KB |
Output is correct |
34 |
Correct |
7 ms |
51288 KB |
Output is correct |
35 |
Correct |
7 ms |
51292 KB |
Output is correct |
36 |
Correct |
7 ms |
51292 KB |
Output is correct |
37 |
Correct |
6 ms |
51408 KB |
Output is correct |
38 |
Correct |
7 ms |
51548 KB |
Output is correct |
39 |
Correct |
7 ms |
55668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
51288 KB |
Output is correct |
2 |
Correct |
7 ms |
51292 KB |
Output is correct |
3 |
Correct |
7 ms |
51460 KB |
Output is correct |
4 |
Correct |
7 ms |
51548 KB |
Output is correct |
5 |
Correct |
7 ms |
51292 KB |
Output is correct |
6 |
Correct |
7 ms |
51292 KB |
Output is correct |
7 |
Correct |
7 ms |
51316 KB |
Output is correct |
8 |
Correct |
7 ms |
51292 KB |
Output is correct |
9 |
Correct |
7 ms |
51284 KB |
Output is correct |
10 |
Correct |
7 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
51548 KB |
Output is correct |
12 |
Correct |
7 ms |
51556 KB |
Output is correct |
13 |
Correct |
7 ms |
53596 KB |
Output is correct |
14 |
Correct |
6 ms |
51412 KB |
Output is correct |
15 |
Correct |
7 ms |
53596 KB |
Output is correct |
16 |
Correct |
6 ms |
51496 KB |
Output is correct |
17 |
Correct |
7 ms |
51448 KB |
Output is correct |
18 |
Correct |
7 ms |
51288 KB |
Output is correct |
19 |
Correct |
7 ms |
51548 KB |
Output is correct |
20 |
Correct |
7 ms |
51288 KB |
Output is correct |
21 |
Correct |
8 ms |
55640 KB |
Output is correct |
22 |
Correct |
9 ms |
55648 KB |
Output is correct |
23 |
Correct |
7 ms |
55640 KB |
Output is correct |
24 |
Correct |
7 ms |
55604 KB |
Output is correct |
25 |
Correct |
7 ms |
55896 KB |
Output is correct |
26 |
Correct |
7 ms |
53584 KB |
Output is correct |
27 |
Correct |
7 ms |
55644 KB |
Output is correct |
28 |
Correct |
7 ms |
55644 KB |
Output is correct |
29 |
Correct |
7 ms |
53596 KB |
Output is correct |
30 |
Correct |
7 ms |
55608 KB |
Output is correct |
31 |
Correct |
7 ms |
55632 KB |
Output is correct |
32 |
Correct |
7 ms |
55644 KB |
Output is correct |
33 |
Correct |
7 ms |
53596 KB |
Output is correct |
34 |
Correct |
7 ms |
51288 KB |
Output is correct |
35 |
Correct |
7 ms |
51292 KB |
Output is correct |
36 |
Correct |
7 ms |
51292 KB |
Output is correct |
37 |
Correct |
6 ms |
51408 KB |
Output is correct |
38 |
Correct |
7 ms |
51548 KB |
Output is correct |
39 |
Correct |
7 ms |
55668 KB |
Output is correct |
40 |
Correct |
8 ms |
55896 KB |
Output is correct |
41 |
Correct |
8 ms |
56268 KB |
Output is correct |
42 |
Correct |
8 ms |
56412 KB |
Output is correct |
43 |
Correct |
8 ms |
56412 KB |
Output is correct |
44 |
Correct |
8 ms |
56404 KB |
Output is correct |
45 |
Correct |
8 ms |
56584 KB |
Output is correct |
46 |
Correct |
7 ms |
56152 KB |
Output is correct |
47 |
Correct |
8 ms |
56156 KB |
Output is correct |
48 |
Correct |
7 ms |
56156 KB |
Output is correct |
49 |
Correct |
7 ms |
56156 KB |
Output is correct |
50 |
Correct |
7 ms |
56156 KB |
Output is correct |
51 |
Correct |
7 ms |
56388 KB |
Output is correct |
52 |
Correct |
8 ms |
56664 KB |
Output is correct |
53 |
Correct |
7 ms |
56156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
51288 KB |
Output is correct |
2 |
Correct |
7 ms |
51292 KB |
Output is correct |
3 |
Correct |
7 ms |
51460 KB |
Output is correct |
4 |
Correct |
7 ms |
51548 KB |
Output is correct |
5 |
Correct |
7 ms |
51292 KB |
Output is correct |
6 |
Correct |
7 ms |
51292 KB |
Output is correct |
7 |
Correct |
7 ms |
51316 KB |
Output is correct |
8 |
Correct |
7 ms |
51292 KB |
Output is correct |
9 |
Correct |
7 ms |
51284 KB |
Output is correct |
10 |
Correct |
7 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
51548 KB |
Output is correct |
12 |
Correct |
7 ms |
51556 KB |
Output is correct |
13 |
Correct |
7 ms |
53596 KB |
Output is correct |
14 |
Correct |
6 ms |
51412 KB |
Output is correct |
15 |
Correct |
7 ms |
53596 KB |
Output is correct |
16 |
Correct |
6 ms |
51496 KB |
Output is correct |
17 |
Correct |
7 ms |
51448 KB |
Output is correct |
18 |
Correct |
7 ms |
51288 KB |
Output is correct |
19 |
Correct |
7 ms |
51548 KB |
Output is correct |
20 |
Correct |
7 ms |
51288 KB |
Output is correct |
21 |
Correct |
8 ms |
55640 KB |
Output is correct |
22 |
Correct |
9 ms |
55648 KB |
Output is correct |
23 |
Correct |
7 ms |
55640 KB |
Output is correct |
24 |
Correct |
7 ms |
55604 KB |
Output is correct |
25 |
Correct |
7 ms |
55896 KB |
Output is correct |
26 |
Correct |
7 ms |
53584 KB |
Output is correct |
27 |
Correct |
7 ms |
55644 KB |
Output is correct |
28 |
Correct |
7 ms |
55644 KB |
Output is correct |
29 |
Correct |
7 ms |
53596 KB |
Output is correct |
30 |
Correct |
7 ms |
55608 KB |
Output is correct |
31 |
Correct |
7 ms |
55632 KB |
Output is correct |
32 |
Correct |
7 ms |
55644 KB |
Output is correct |
33 |
Correct |
7 ms |
53596 KB |
Output is correct |
34 |
Correct |
7 ms |
51288 KB |
Output is correct |
35 |
Correct |
7 ms |
51292 KB |
Output is correct |
36 |
Correct |
7 ms |
51292 KB |
Output is correct |
37 |
Correct |
6 ms |
51408 KB |
Output is correct |
38 |
Correct |
7 ms |
51548 KB |
Output is correct |
39 |
Correct |
7 ms |
55668 KB |
Output is correct |
40 |
Correct |
8 ms |
55896 KB |
Output is correct |
41 |
Correct |
8 ms |
56268 KB |
Output is correct |
42 |
Correct |
8 ms |
56412 KB |
Output is correct |
43 |
Correct |
8 ms |
56412 KB |
Output is correct |
44 |
Correct |
8 ms |
56404 KB |
Output is correct |
45 |
Correct |
8 ms |
56584 KB |
Output is correct |
46 |
Correct |
7 ms |
56152 KB |
Output is correct |
47 |
Correct |
8 ms |
56156 KB |
Output is correct |
48 |
Correct |
7 ms |
56156 KB |
Output is correct |
49 |
Correct |
7 ms |
56156 KB |
Output is correct |
50 |
Correct |
7 ms |
56156 KB |
Output is correct |
51 |
Correct |
7 ms |
56388 KB |
Output is correct |
52 |
Correct |
8 ms |
56664 KB |
Output is correct |
53 |
Correct |
7 ms |
56156 KB |
Output is correct |
54 |
Correct |
9 ms |
63832 KB |
Output is correct |
55 |
Correct |
32 ms |
72536 KB |
Output is correct |
56 |
Correct |
17 ms |
72928 KB |
Output is correct |
57 |
Correct |
16 ms |
72540 KB |
Output is correct |
58 |
Correct |
14 ms |
72712 KB |
Output is correct |
59 |
Correct |
13 ms |
72540 KB |
Output is correct |
60 |
Correct |
13 ms |
72564 KB |
Output is correct |
61 |
Correct |
13 ms |
72540 KB |
Output is correct |
62 |
Correct |
24 ms |
72540 KB |
Output is correct |
63 |
Correct |
14 ms |
72536 KB |
Output is correct |
64 |
Correct |
15 ms |
72540 KB |
Output is correct |
65 |
Correct |
18 ms |
72540 KB |
Output is correct |
66 |
Correct |
18 ms |
72540 KB |
Output is correct |
67 |
Correct |
13 ms |
72820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
51288 KB |
Output is correct |
2 |
Correct |
7 ms |
51292 KB |
Output is correct |
3 |
Correct |
7 ms |
51460 KB |
Output is correct |
4 |
Correct |
7 ms |
51548 KB |
Output is correct |
5 |
Correct |
7 ms |
51292 KB |
Output is correct |
6 |
Correct |
7 ms |
51292 KB |
Output is correct |
7 |
Correct |
7 ms |
51316 KB |
Output is correct |
8 |
Correct |
7 ms |
51292 KB |
Output is correct |
9 |
Correct |
7 ms |
51284 KB |
Output is correct |
10 |
Correct |
7 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
51548 KB |
Output is correct |
12 |
Correct |
7 ms |
51292 KB |
Output is correct |
13 |
Correct |
121 ms |
89432 KB |
Output is correct |
14 |
Correct |
132 ms |
92768 KB |
Output is correct |
15 |
Correct |
166 ms |
96900 KB |
Output is correct |
16 |
Correct |
189 ms |
97668 KB |
Output is correct |
17 |
Correct |
232 ms |
97616 KB |
Output is correct |
18 |
Correct |
218 ms |
97628 KB |
Output is correct |
19 |
Correct |
222 ms |
97800 KB |
Output is correct |
20 |
Correct |
7 ms |
51292 KB |
Output is correct |
21 |
Correct |
6 ms |
51292 KB |
Output is correct |
22 |
Correct |
6 ms |
51292 KB |
Output is correct |
23 |
Correct |
7 ms |
51292 KB |
Output is correct |
24 |
Correct |
6 ms |
51548 KB |
Output is correct |
25 |
Correct |
7 ms |
51544 KB |
Output is correct |
26 |
Correct |
7 ms |
55640 KB |
Output is correct |
27 |
Correct |
7 ms |
51292 KB |
Output is correct |
28 |
Correct |
7 ms |
51548 KB |
Output is correct |
29 |
Correct |
7 ms |
51556 KB |
Output is correct |
30 |
Correct |
7 ms |
53596 KB |
Output is correct |
31 |
Correct |
6 ms |
51412 KB |
Output is correct |
32 |
Correct |
7 ms |
53596 KB |
Output is correct |
33 |
Correct |
6 ms |
51496 KB |
Output is correct |
34 |
Correct |
7 ms |
51448 KB |
Output is correct |
35 |
Correct |
7 ms |
51288 KB |
Output is correct |
36 |
Correct |
7 ms |
51548 KB |
Output is correct |
37 |
Correct |
7 ms |
51288 KB |
Output is correct |
38 |
Correct |
8 ms |
55640 KB |
Output is correct |
39 |
Correct |
9 ms |
55648 KB |
Output is correct |
40 |
Correct |
7 ms |
55640 KB |
Output is correct |
41 |
Correct |
7 ms |
55604 KB |
Output is correct |
42 |
Correct |
7 ms |
55896 KB |
Output is correct |
43 |
Correct |
7 ms |
53584 KB |
Output is correct |
44 |
Correct |
7 ms |
55644 KB |
Output is correct |
45 |
Correct |
7 ms |
55644 KB |
Output is correct |
46 |
Correct |
7 ms |
53596 KB |
Output is correct |
47 |
Correct |
7 ms |
55608 KB |
Output is correct |
48 |
Correct |
7 ms |
55632 KB |
Output is correct |
49 |
Correct |
7 ms |
55644 KB |
Output is correct |
50 |
Correct |
7 ms |
53596 KB |
Output is correct |
51 |
Correct |
7 ms |
51288 KB |
Output is correct |
52 |
Correct |
7 ms |
51292 KB |
Output is correct |
53 |
Correct |
7 ms |
51292 KB |
Output is correct |
54 |
Correct |
6 ms |
51408 KB |
Output is correct |
55 |
Correct |
7 ms |
51548 KB |
Output is correct |
56 |
Correct |
7 ms |
55668 KB |
Output is correct |
57 |
Correct |
8 ms |
55896 KB |
Output is correct |
58 |
Correct |
8 ms |
56268 KB |
Output is correct |
59 |
Correct |
8 ms |
56412 KB |
Output is correct |
60 |
Correct |
8 ms |
56412 KB |
Output is correct |
61 |
Correct |
8 ms |
56404 KB |
Output is correct |
62 |
Correct |
8 ms |
56584 KB |
Output is correct |
63 |
Correct |
7 ms |
56152 KB |
Output is correct |
64 |
Correct |
8 ms |
56156 KB |
Output is correct |
65 |
Correct |
7 ms |
56156 KB |
Output is correct |
66 |
Correct |
7 ms |
56156 KB |
Output is correct |
67 |
Correct |
7 ms |
56156 KB |
Output is correct |
68 |
Correct |
7 ms |
56388 KB |
Output is correct |
69 |
Correct |
8 ms |
56664 KB |
Output is correct |
70 |
Correct |
7 ms |
56156 KB |
Output is correct |
71 |
Correct |
9 ms |
63832 KB |
Output is correct |
72 |
Correct |
32 ms |
72536 KB |
Output is correct |
73 |
Correct |
17 ms |
72928 KB |
Output is correct |
74 |
Correct |
16 ms |
72540 KB |
Output is correct |
75 |
Correct |
14 ms |
72712 KB |
Output is correct |
76 |
Correct |
13 ms |
72540 KB |
Output is correct |
77 |
Correct |
13 ms |
72564 KB |
Output is correct |
78 |
Correct |
13 ms |
72540 KB |
Output is correct |
79 |
Correct |
24 ms |
72540 KB |
Output is correct |
80 |
Correct |
14 ms |
72536 KB |
Output is correct |
81 |
Correct |
15 ms |
72540 KB |
Output is correct |
82 |
Correct |
18 ms |
72540 KB |
Output is correct |
83 |
Correct |
18 ms |
72540 KB |
Output is correct |
84 |
Correct |
13 ms |
72820 KB |
Output is correct |
85 |
Correct |
29 ms |
84824 KB |
Output is correct |
86 |
Correct |
57 ms |
97620 KB |
Output is correct |
87 |
Correct |
53 ms |
97620 KB |
Output is correct |
88 |
Correct |
40 ms |
97624 KB |
Output is correct |
89 |
Correct |
39 ms |
97780 KB |
Output is correct |
90 |
Correct |
36 ms |
97628 KB |
Output is correct |
91 |
Correct |
64 ms |
97616 KB |
Output is correct |
92 |
Correct |
96 ms |
97616 KB |
Output is correct |
93 |
Correct |
39 ms |
97628 KB |
Output is correct |
94 |
Correct |
43 ms |
97628 KB |
Output is correct |
95 |
Correct |
82 ms |
97784 KB |
Output is correct |
96 |
Correct |
83 ms |
97636 KB |
Output is correct |
97 |
Correct |
38 ms |
97692 KB |
Output is correct |