#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 2510
#define MAXS 20
#define INF 1000000000000000001
#define MOD 998244353
#define bb ' '
#define ln '\n'
#define Ln '\n'
ll dp[MAX][MAX];
#define base 1557
ll mpow[MAX];
ll hv[MAX];
int nxt[MAX];
#define hf(x) ((x)-'a'+1)
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int N;
cin >> N;
string s;
cin >> s;
ll A, B, C;
cin >> A >> B >> C;
int k, i, j;
mpow[0] = 1;
for (i = 0; i < N; i++) for (j = 0; j < N; j++) dp[i][j] = INF;
for (i = 1; i <= N; i++) mpow[i] = 1ll * mpow[i - 1] * base, mpow[i] %= MOD;
for (i = 0; i < N; i++) dp[i][i] = A;
for (k = 1; k <= N; k++) {
ll hash = 0;
for (i = 0; i < k; i++) hash *= base, hash += hf(s[i]), hash %= MOD;
map<ll, vector<int>> vall;
if (k > 1) {
for (i = 0; i < (N - k + 1); i++) {
int j = i + k - 1;
dp[i][j] = min(dp[i][j], min(dp[i + 1][j], dp[i][j - 1]) + A);
}
}
for (i = 0; i < (N - k + 1); i++) {
vall[hash].push_back(i);
hv[i] = hash;
hash *= base;
if (i + k < N) hash += hf(s[i + k]);
hash -= 1ll * mpow[k] * hf(s[i]);
hash %= MOD;
while (hash < 0) hash += MOD;
}
for (auto it = vall.begin(); it != vall.end(); it++) {
ll h = it->first;
int M = vall[h].size();
int e = 0;
for (int p = 0; p < M; p++) {
while (e < M && vall[h][e] < vall[h][p] + k) e++;
if (e >= M) nxt[vall[h][p]] = 0;
else nxt[vall[h][p]] = vall[h][e];
}
}
for (i = 0; i < (N - k + 1); i++) {
if (!nxt[i]) continue;
int r = nxt[i] + k - 1;
int j = i + k - 1;
int cnt = 2;
while (1) {
dp[i][r] = min(dp[i][r], dp[i][j] + B + C * cnt + A * ((ll)r - i + 1 - (ll)cnt * ((ll)j - i + 1)));
if (!nxt[r - k + 1]) break;
r = nxt[r - k + 1] + k - 1;
cnt++;
}
}
}
cout << dp[0][N - 1] << ln;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
252 ms |
33832 KB |
Output is correct |
4 |
Correct |
280 ms |
38868 KB |
Output is correct |
5 |
Correct |
321 ms |
42396 KB |
Output is correct |
6 |
Correct |
425 ms |
45804 KB |
Output is correct |
7 |
Correct |
442 ms |
49508 KB |
Output is correct |
8 |
Correct |
443 ms |
49504 KB |
Output is correct |
9 |
Correct |
464 ms |
49492 KB |
Output is correct |
10 |
Correct |
0 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
3 ms |
1444 KB |
Output is correct |
42 |
Correct |
6 ms |
1468 KB |
Output is correct |
43 |
Correct |
6 ms |
1364 KB |
Output is correct |
44 |
Correct |
6 ms |
1364 KB |
Output is correct |
45 |
Correct |
6 ms |
1364 KB |
Output is correct |
46 |
Correct |
6 ms |
1364 KB |
Output is correct |
47 |
Correct |
4 ms |
1364 KB |
Output is correct |
48 |
Correct |
6 ms |
1364 KB |
Output is correct |
49 |
Correct |
6 ms |
1464 KB |
Output is correct |
50 |
Correct |
6 ms |
1364 KB |
Output is correct |
51 |
Correct |
5 ms |
1364 KB |
Output is correct |
52 |
Correct |
5 ms |
1364 KB |
Output is correct |
53 |
Correct |
6 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
3 ms |
1444 KB |
Output is correct |
42 |
Correct |
6 ms |
1468 KB |
Output is correct |
43 |
Correct |
6 ms |
1364 KB |
Output is correct |
44 |
Correct |
6 ms |
1364 KB |
Output is correct |
45 |
Correct |
6 ms |
1364 KB |
Output is correct |
46 |
Correct |
6 ms |
1364 KB |
Output is correct |
47 |
Correct |
4 ms |
1364 KB |
Output is correct |
48 |
Correct |
6 ms |
1364 KB |
Output is correct |
49 |
Correct |
6 ms |
1464 KB |
Output is correct |
50 |
Correct |
6 ms |
1364 KB |
Output is correct |
51 |
Correct |
5 ms |
1364 KB |
Output is correct |
52 |
Correct |
5 ms |
1364 KB |
Output is correct |
53 |
Correct |
6 ms |
1364 KB |
Output is correct |
54 |
Correct |
30 ms |
3744 KB |
Output is correct |
55 |
Correct |
54 ms |
12180 KB |
Output is correct |
56 |
Correct |
157 ms |
12292 KB |
Output is correct |
57 |
Correct |
150 ms |
12288 KB |
Output is correct |
58 |
Correct |
157 ms |
12292 KB |
Output is correct |
59 |
Correct |
152 ms |
12284 KB |
Output is correct |
60 |
Correct |
151 ms |
12244 KB |
Output is correct |
61 |
Correct |
122 ms |
12116 KB |
Output is correct |
62 |
Correct |
51 ms |
12236 KB |
Output is correct |
63 |
Correct |
142 ms |
12244 KB |
Output is correct |
64 |
Correct |
146 ms |
12244 KB |
Output is correct |
65 |
Correct |
133 ms |
12228 KB |
Output is correct |
66 |
Correct |
129 ms |
12212 KB |
Output is correct |
67 |
Correct |
148 ms |
12280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
252 ms |
33832 KB |
Output is correct |
14 |
Correct |
280 ms |
38868 KB |
Output is correct |
15 |
Correct |
321 ms |
42396 KB |
Output is correct |
16 |
Correct |
425 ms |
45804 KB |
Output is correct |
17 |
Correct |
442 ms |
49508 KB |
Output is correct |
18 |
Correct |
443 ms |
49504 KB |
Output is correct |
19 |
Correct |
464 ms |
49492 KB |
Output is correct |
20 |
Correct |
0 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
724 KB |
Output is correct |
58 |
Correct |
3 ms |
1444 KB |
Output is correct |
59 |
Correct |
6 ms |
1468 KB |
Output is correct |
60 |
Correct |
6 ms |
1364 KB |
Output is correct |
61 |
Correct |
6 ms |
1364 KB |
Output is correct |
62 |
Correct |
6 ms |
1364 KB |
Output is correct |
63 |
Correct |
6 ms |
1364 KB |
Output is correct |
64 |
Correct |
4 ms |
1364 KB |
Output is correct |
65 |
Correct |
6 ms |
1364 KB |
Output is correct |
66 |
Correct |
6 ms |
1464 KB |
Output is correct |
67 |
Correct |
6 ms |
1364 KB |
Output is correct |
68 |
Correct |
5 ms |
1364 KB |
Output is correct |
69 |
Correct |
5 ms |
1364 KB |
Output is correct |
70 |
Correct |
6 ms |
1364 KB |
Output is correct |
71 |
Correct |
30 ms |
3744 KB |
Output is correct |
72 |
Correct |
54 ms |
12180 KB |
Output is correct |
73 |
Correct |
157 ms |
12292 KB |
Output is correct |
74 |
Correct |
150 ms |
12288 KB |
Output is correct |
75 |
Correct |
157 ms |
12292 KB |
Output is correct |
76 |
Correct |
152 ms |
12284 KB |
Output is correct |
77 |
Correct |
151 ms |
12244 KB |
Output is correct |
78 |
Correct |
122 ms |
12116 KB |
Output is correct |
79 |
Correct |
51 ms |
12236 KB |
Output is correct |
80 |
Correct |
142 ms |
12244 KB |
Output is correct |
81 |
Correct |
146 ms |
12244 KB |
Output is correct |
82 |
Correct |
133 ms |
12228 KB |
Output is correct |
83 |
Correct |
129 ms |
12212 KB |
Output is correct |
84 |
Correct |
148 ms |
12280 KB |
Output is correct |
85 |
Correct |
402 ms |
26432 KB |
Output is correct |
86 |
Correct |
1108 ms |
49776 KB |
Output is correct |
87 |
Correct |
1072 ms |
49772 KB |
Output is correct |
88 |
Correct |
1104 ms |
49764 KB |
Output is correct |
89 |
Correct |
1070 ms |
49768 KB |
Output is correct |
90 |
Correct |
1058 ms |
49760 KB |
Output is correct |
91 |
Correct |
528 ms |
49492 KB |
Output is correct |
92 |
Correct |
437 ms |
49508 KB |
Output is correct |
93 |
Correct |
953 ms |
49832 KB |
Output is correct |
94 |
Correct |
982 ms |
49840 KB |
Output is correct |
95 |
Correct |
974 ms |
49692 KB |
Output is correct |
96 |
Correct |
927 ms |
49704 KB |
Output is correct |
97 |
Correct |
1087 ms |
49764 KB |
Output is correct |