#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
const ll base = 29, mod = 462'779'347'772'851;
int N;
char c[2505];
ll A, B, C, dp[2505][2505], h[2505][2505], nxt[2505][2505];
signed main()
{
cin >> N;
for(int i = 1; i <= N; i++)
cin >> c[i];
cin >> A >> B >> C;
for(int i = 1; i <= N; i++)
for(int j = i; j <= N; j++)
{
dp[i][j] = A * (j - i + 1);
h[i][j] = (h[i][j - 1] * base + c[j] - 'a') % mod;
}
for(int l = 1; l <= N; l++)
{
map<ll, int> mp;
for(int i = N - l + 1; i >= 1; i--)
if(i + 2 * l - 1 <= N)
{
mp[h[i + l][i + 2 * l - 1]] = i + l;
nxt[i][i + l - 1] = mp[h[i][i + l - 1]];
// cout << "[" << i << ", " << i + l - 1 << "] -> [" << nxt[i][i + l - 1] << "]\n";
}
}
for(int i = N; i >= 1; i--)
for(int k = i; k <= N; k++)
{
dp[i][k] = min({dp[i][k], dp[i + 1][k] + A, dp[i][k - 1] + A});
ll base = dp[i][k] + B, j = nxt[i][k], t = 2;
for(; j != 0; j = nxt[j][j + k - i])
{
dp[i][j + k - i] = min(dp[i][j + k - i], base + t * C + (j + k - i - i + 1 - t * (k - i + 1)) * A);
t++;
}
}
/* for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
if(j < i) cout << "- ";
else cout << dp[i][j] << " \n"[N == j];
*/
cout << dp[1][N] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
224 ms |
55028 KB |
Output is correct |
4 |
Correct |
253 ms |
62388 KB |
Output is correct |
5 |
Correct |
311 ms |
70620 KB |
Output is correct |
6 |
Correct |
432 ms |
79608 KB |
Output is correct |
7 |
Correct |
508 ms |
89536 KB |
Output is correct |
8 |
Correct |
514 ms |
89568 KB |
Output is correct |
9 |
Correct |
558 ms |
89532 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 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 |
0 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 |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 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 |
0 ms |
216 KB |
Output is correct |
35 |
Correct |
0 ms |
352 KB |
Output is correct |
36 |
Correct |
1 ms |
352 KB |
Output is correct |
37 |
Correct |
0 ms |
352 KB |
Output is correct |
38 |
Correct |
1 ms |
480 KB |
Output is correct |
39 |
Correct |
1 ms |
608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 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 |
0 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 |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 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 |
0 ms |
216 KB |
Output is correct |
35 |
Correct |
0 ms |
352 KB |
Output is correct |
36 |
Correct |
1 ms |
352 KB |
Output is correct |
37 |
Correct |
0 ms |
352 KB |
Output is correct |
38 |
Correct |
1 ms |
480 KB |
Output is correct |
39 |
Correct |
1 ms |
608 KB |
Output is correct |
40 |
Correct |
2 ms |
1376 KB |
Output is correct |
41 |
Correct |
4 ms |
3016 KB |
Output is correct |
42 |
Correct |
5 ms |
3028 KB |
Output is correct |
43 |
Correct |
4 ms |
3028 KB |
Output is correct |
44 |
Correct |
4 ms |
3028 KB |
Output is correct |
45 |
Correct |
5 ms |
3028 KB |
Output is correct |
46 |
Correct |
4 ms |
3028 KB |
Output is correct |
47 |
Correct |
4 ms |
3028 KB |
Output is correct |
48 |
Correct |
3 ms |
3028 KB |
Output is correct |
49 |
Correct |
5 ms |
3072 KB |
Output is correct |
50 |
Correct |
4 ms |
3048 KB |
Output is correct |
51 |
Correct |
4 ms |
3028 KB |
Output is correct |
52 |
Correct |
3 ms |
3028 KB |
Output is correct |
53 |
Correct |
5 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 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 |
0 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 |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 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 |
0 ms |
216 KB |
Output is correct |
35 |
Correct |
0 ms |
352 KB |
Output is correct |
36 |
Correct |
1 ms |
352 KB |
Output is correct |
37 |
Correct |
0 ms |
352 KB |
Output is correct |
38 |
Correct |
1 ms |
480 KB |
Output is correct |
39 |
Correct |
1 ms |
608 KB |
Output is correct |
40 |
Correct |
2 ms |
1376 KB |
Output is correct |
41 |
Correct |
4 ms |
3016 KB |
Output is correct |
42 |
Correct |
5 ms |
3028 KB |
Output is correct |
43 |
Correct |
4 ms |
3028 KB |
Output is correct |
44 |
Correct |
4 ms |
3028 KB |
Output is correct |
45 |
Correct |
5 ms |
3028 KB |
Output is correct |
46 |
Correct |
4 ms |
3028 KB |
Output is correct |
47 |
Correct |
4 ms |
3028 KB |
Output is correct |
48 |
Correct |
3 ms |
3028 KB |
Output is correct |
49 |
Correct |
5 ms |
3072 KB |
Output is correct |
50 |
Correct |
4 ms |
3048 KB |
Output is correct |
51 |
Correct |
4 ms |
3028 KB |
Output is correct |
52 |
Correct |
3 ms |
3028 KB |
Output is correct |
53 |
Correct |
5 ms |
3028 KB |
Output is correct |
54 |
Correct |
18 ms |
7732 KB |
Output is correct |
55 |
Correct |
55 ms |
22144 KB |
Output is correct |
56 |
Correct |
81 ms |
22112 KB |
Output is correct |
57 |
Correct |
89 ms |
22132 KB |
Output is correct |
58 |
Correct |
80 ms |
22208 KB |
Output is correct |
59 |
Correct |
78 ms |
22112 KB |
Output is correct |
60 |
Correct |
88 ms |
22120 KB |
Output is correct |
61 |
Correct |
61 ms |
22144 KB |
Output is correct |
62 |
Correct |
43 ms |
22132 KB |
Output is correct |
63 |
Correct |
72 ms |
22144 KB |
Output is correct |
64 |
Correct |
72 ms |
22128 KB |
Output is correct |
65 |
Correct |
62 ms |
22092 KB |
Output is correct |
66 |
Correct |
55 ms |
22112 KB |
Output is correct |
67 |
Correct |
76 ms |
22108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
224 ms |
55028 KB |
Output is correct |
14 |
Correct |
253 ms |
62388 KB |
Output is correct |
15 |
Correct |
311 ms |
70620 KB |
Output is correct |
16 |
Correct |
432 ms |
79608 KB |
Output is correct |
17 |
Correct |
508 ms |
89536 KB |
Output is correct |
18 |
Correct |
514 ms |
89568 KB |
Output is correct |
19 |
Correct |
558 ms |
89532 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
596 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 |
0 ms |
216 KB |
Output is correct |
52 |
Correct |
0 ms |
352 KB |
Output is correct |
53 |
Correct |
1 ms |
352 KB |
Output is correct |
54 |
Correct |
0 ms |
352 KB |
Output is correct |
55 |
Correct |
1 ms |
480 KB |
Output is correct |
56 |
Correct |
1 ms |
608 KB |
Output is correct |
57 |
Correct |
2 ms |
1376 KB |
Output is correct |
58 |
Correct |
4 ms |
3016 KB |
Output is correct |
59 |
Correct |
5 ms |
3028 KB |
Output is correct |
60 |
Correct |
4 ms |
3028 KB |
Output is correct |
61 |
Correct |
4 ms |
3028 KB |
Output is correct |
62 |
Correct |
5 ms |
3028 KB |
Output is correct |
63 |
Correct |
4 ms |
3028 KB |
Output is correct |
64 |
Correct |
4 ms |
3028 KB |
Output is correct |
65 |
Correct |
3 ms |
3028 KB |
Output is correct |
66 |
Correct |
5 ms |
3072 KB |
Output is correct |
67 |
Correct |
4 ms |
3048 KB |
Output is correct |
68 |
Correct |
4 ms |
3028 KB |
Output is correct |
69 |
Correct |
3 ms |
3028 KB |
Output is correct |
70 |
Correct |
5 ms |
3028 KB |
Output is correct |
71 |
Correct |
18 ms |
7732 KB |
Output is correct |
72 |
Correct |
55 ms |
22144 KB |
Output is correct |
73 |
Correct |
81 ms |
22112 KB |
Output is correct |
74 |
Correct |
89 ms |
22132 KB |
Output is correct |
75 |
Correct |
80 ms |
22208 KB |
Output is correct |
76 |
Correct |
78 ms |
22112 KB |
Output is correct |
77 |
Correct |
88 ms |
22120 KB |
Output is correct |
78 |
Correct |
61 ms |
22144 KB |
Output is correct |
79 |
Correct |
43 ms |
22132 KB |
Output is correct |
80 |
Correct |
72 ms |
22144 KB |
Output is correct |
81 |
Correct |
72 ms |
22128 KB |
Output is correct |
82 |
Correct |
62 ms |
22092 KB |
Output is correct |
83 |
Correct |
55 ms |
22112 KB |
Output is correct |
84 |
Correct |
76 ms |
22108 KB |
Output is correct |
85 |
Correct |
201 ms |
43908 KB |
Output is correct |
86 |
Correct |
591 ms |
89664 KB |
Output is correct |
87 |
Correct |
539 ms |
89584 KB |
Output is correct |
88 |
Correct |
495 ms |
89804 KB |
Output is correct |
89 |
Correct |
483 ms |
89568 KB |
Output is correct |
90 |
Correct |
510 ms |
89744 KB |
Output is correct |
91 |
Correct |
178 ms |
89552 KB |
Output is correct |
92 |
Correct |
192 ms |
89540 KB |
Output is correct |
93 |
Correct |
435 ms |
89500 KB |
Output is correct |
94 |
Correct |
417 ms |
89636 KB |
Output is correct |
95 |
Correct |
356 ms |
89544 KB |
Output is correct |
96 |
Correct |
354 ms |
89532 KB |
Output is correct |
97 |
Correct |
488 ms |
89700 KB |
Output is correct |