#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#ifndef LOCAL
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
mt19937_64 mt(time(0));
void solve();
void init();
int32_t main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cout << fixed << setprecision(30);
init();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
const int N = 2525;
const ll mod = (1ll << 61) - 1, p = 31;
ll dp[N][N], save[N][N], mn[N];
int to[N][N], nw[N][N];
ll hsh[N], pdeg[N], pinv[N];
vector<pair<int, int>> upd[N];
ll add(ll a, ll b) {
return a + b >= mod ? a + b - mod : a + b;
}
ll sub(ll a, ll b) {
return a >= b ? a - b : a - b + mod;
}
ll mul(ll a, ll b) {
return (__int128_t)1 * a * b % mod;
}
ll bin_pow(ll a, ll x) {
ll res = 1;
while (x) {
if (x & 1) {
res = mul(res, a);
}
a = mul(a, a);
x >>= 1;
}
return res;
}
ll inv(ll a) {
return bin_pow(a, mod - 2);
}
void init() {
pdeg[0] = 1;
pdeg[1] = p;
pinv[0] = 1;
pinv[1] = inv(p);
for (int i = 2; i < N; i++) {
pdeg[i] = mul(pdeg[i - 1], p);
pinv[i] = mul(pinv[i - 1], pinv[1]);
}
}
ll get_hash(int l, int r) {
return mul(sub(hsh[r + 1], hsh[l]), pinv[l]);
}
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int a, b, c;
cin >> a >> b >> c;
hsh[0] = 0;
for (int i = 0; i < n; i++) {
hsh[i + 1] = add(hsh[i], mul(s[i] - 'a' + 1, pdeg[i]));
upd[i].reserve(40 * N);
}
for (int l = 0; l < n; l++) {
unordered_map<ll, int> mp;
for (int i = 0; i < n - l; i++) {
int j = i + l;
ll nw = get_hash(i, j);
if (mp.find(nw) == mp.end()) {
to[i][j] = -1;
} else {
to[i][j] = mp[nw];
}
if (i >= l) {
mp[get_hash(i - l, i)] = i - l;
}
}
}
for (int i = 0; i < n; i++) {
dp[i][i] = a;
save[i][i] = b + c;
mn[i] = min(mn[i], save[i][i]);
nw[i][i] = to[i][i];
if (to[i][i] != -1) {
upd[i - to[i][i]].emplace_back(i, i);
}
}
for (int l = 1; l < n; l++) {
for (auto [i, j] : upd[l]) {
int nxt = to[nw[i][j]][nw[i][j] + j - i];
nw[i][j] = nxt;
if (nxt >= 0) {
upd[j - nxt].emplace_back(i, j);
}
save[j][i] += c - 1ll * (j - i + 1) * a;
mn[j] = min(mn[j], save[j][i]);
}
for (int i = 0; i < n - l; i++) {
int j = i + l;
dp[i][j] = min(1ll * (j - i + 1) * a + mn[j], dp[i][j - 1] + a);
save[j][i] += dp[i][j] + b + c - 1ll * (j - i + 1) * a;
mn[j] = min(mn[j], save[j][i]);
nw[i][j] = to[i][j];
if (nw[i][j] != -1) {
upd[j - nw[i][j]].emplace_back(i, j);
}
}
}
cout << dp[0][n - 1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
429 ms |
198668 KB |
Output is correct |
4 |
Correct |
477 ms |
224348 KB |
Output is correct |
5 |
Correct |
608 ms |
252752 KB |
Output is correct |
6 |
Correct |
688 ms |
284240 KB |
Output is correct |
7 |
Correct |
787 ms |
315800 KB |
Output is correct |
8 |
Correct |
790 ms |
315728 KB |
Output is correct |
9 |
Correct |
828 ms |
315812 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
7004 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
7000 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
7000 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
7000 KB |
Output is correct |
22 |
Correct |
1 ms |
7004 KB |
Output is correct |
23 |
Correct |
1 ms |
7004 KB |
Output is correct |
24 |
Correct |
2 ms |
7004 KB |
Output is correct |
25 |
Correct |
2 ms |
7004 KB |
Output is correct |
26 |
Correct |
1 ms |
7004 KB |
Output is correct |
27 |
Correct |
1 ms |
7004 KB |
Output is correct |
28 |
Correct |
2 ms |
7004 KB |
Output is correct |
29 |
Correct |
1 ms |
7004 KB |
Output is correct |
30 |
Correct |
1 ms |
7004 KB |
Output is correct |
31 |
Correct |
1 ms |
7004 KB |
Output is correct |
32 |
Correct |
1 ms |
7004 KB |
Output is correct |
33 |
Correct |
1 ms |
7004 KB |
Output is correct |
34 |
Correct |
1 ms |
4696 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
1 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
6748 KB |
Output is correct |
38 |
Correct |
1 ms |
6748 KB |
Output is correct |
39 |
Correct |
1 ms |
7004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
7000 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
7000 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
7000 KB |
Output is correct |
22 |
Correct |
1 ms |
7004 KB |
Output is correct |
23 |
Correct |
1 ms |
7004 KB |
Output is correct |
24 |
Correct |
2 ms |
7004 KB |
Output is correct |
25 |
Correct |
2 ms |
7004 KB |
Output is correct |
26 |
Correct |
1 ms |
7004 KB |
Output is correct |
27 |
Correct |
1 ms |
7004 KB |
Output is correct |
28 |
Correct |
2 ms |
7004 KB |
Output is correct |
29 |
Correct |
1 ms |
7004 KB |
Output is correct |
30 |
Correct |
1 ms |
7004 KB |
Output is correct |
31 |
Correct |
1 ms |
7004 KB |
Output is correct |
32 |
Correct |
1 ms |
7004 KB |
Output is correct |
33 |
Correct |
1 ms |
7004 KB |
Output is correct |
34 |
Correct |
1 ms |
4696 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
1 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
6748 KB |
Output is correct |
38 |
Correct |
1 ms |
6748 KB |
Output is correct |
39 |
Correct |
1 ms |
7004 KB |
Output is correct |
40 |
Correct |
3 ms |
11612 KB |
Output is correct |
41 |
Correct |
5 ms |
18524 KB |
Output is correct |
42 |
Correct |
5 ms |
18268 KB |
Output is correct |
43 |
Correct |
6 ms |
18340 KB |
Output is correct |
44 |
Correct |
5 ms |
18428 KB |
Output is correct |
45 |
Correct |
6 ms |
18264 KB |
Output is correct |
46 |
Correct |
5 ms |
18264 KB |
Output is correct |
47 |
Correct |
5 ms |
18268 KB |
Output is correct |
48 |
Correct |
5 ms |
18268 KB |
Output is correct |
49 |
Correct |
6 ms |
18268 KB |
Output is correct |
50 |
Correct |
5 ms |
18268 KB |
Output is correct |
51 |
Correct |
5 ms |
18268 KB |
Output is correct |
52 |
Correct |
5 ms |
18264 KB |
Output is correct |
53 |
Correct |
5 ms |
18268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
7000 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
7000 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
7000 KB |
Output is correct |
22 |
Correct |
1 ms |
7004 KB |
Output is correct |
23 |
Correct |
1 ms |
7004 KB |
Output is correct |
24 |
Correct |
2 ms |
7004 KB |
Output is correct |
25 |
Correct |
2 ms |
7004 KB |
Output is correct |
26 |
Correct |
1 ms |
7004 KB |
Output is correct |
27 |
Correct |
1 ms |
7004 KB |
Output is correct |
28 |
Correct |
2 ms |
7004 KB |
Output is correct |
29 |
Correct |
1 ms |
7004 KB |
Output is correct |
30 |
Correct |
1 ms |
7004 KB |
Output is correct |
31 |
Correct |
1 ms |
7004 KB |
Output is correct |
32 |
Correct |
1 ms |
7004 KB |
Output is correct |
33 |
Correct |
1 ms |
7004 KB |
Output is correct |
34 |
Correct |
1 ms |
4696 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
1 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
6748 KB |
Output is correct |
38 |
Correct |
1 ms |
6748 KB |
Output is correct |
39 |
Correct |
1 ms |
7004 KB |
Output is correct |
40 |
Correct |
3 ms |
11612 KB |
Output is correct |
41 |
Correct |
5 ms |
18524 KB |
Output is correct |
42 |
Correct |
5 ms |
18268 KB |
Output is correct |
43 |
Correct |
6 ms |
18340 KB |
Output is correct |
44 |
Correct |
5 ms |
18428 KB |
Output is correct |
45 |
Correct |
6 ms |
18264 KB |
Output is correct |
46 |
Correct |
5 ms |
18264 KB |
Output is correct |
47 |
Correct |
5 ms |
18268 KB |
Output is correct |
48 |
Correct |
5 ms |
18268 KB |
Output is correct |
49 |
Correct |
6 ms |
18268 KB |
Output is correct |
50 |
Correct |
5 ms |
18268 KB |
Output is correct |
51 |
Correct |
5 ms |
18268 KB |
Output is correct |
52 |
Correct |
5 ms |
18264 KB |
Output is correct |
53 |
Correct |
5 ms |
18268 KB |
Output is correct |
54 |
Correct |
18 ms |
34136 KB |
Output is correct |
55 |
Correct |
111 ms |
87900 KB |
Output is correct |
56 |
Correct |
77 ms |
69672 KB |
Output is correct |
57 |
Correct |
74 ms |
67920 KB |
Output is correct |
58 |
Correct |
70 ms |
67924 KB |
Output is correct |
59 |
Correct |
70 ms |
67924 KB |
Output is correct |
60 |
Correct |
70 ms |
68128 KB |
Output is correct |
61 |
Correct |
62 ms |
69180 KB |
Output is correct |
62 |
Correct |
92 ms |
83544 KB |
Output is correct |
63 |
Correct |
67 ms |
68432 KB |
Output is correct |
64 |
Correct |
70 ms |
69456 KB |
Output is correct |
65 |
Correct |
80 ms |
76112 KB |
Output is correct |
66 |
Correct |
79 ms |
75768 KB |
Output is correct |
67 |
Correct |
68 ms |
67920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
6744 KB |
Output is correct |
12 |
Correct |
1 ms |
4696 KB |
Output is correct |
13 |
Correct |
429 ms |
198668 KB |
Output is correct |
14 |
Correct |
477 ms |
224348 KB |
Output is correct |
15 |
Correct |
608 ms |
252752 KB |
Output is correct |
16 |
Correct |
688 ms |
284240 KB |
Output is correct |
17 |
Correct |
787 ms |
315800 KB |
Output is correct |
18 |
Correct |
790 ms |
315728 KB |
Output is correct |
19 |
Correct |
828 ms |
315812 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
7004 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
2 ms |
7000 KB |
Output is correct |
30 |
Correct |
1 ms |
6748 KB |
Output is correct |
31 |
Correct |
1 ms |
6748 KB |
Output is correct |
32 |
Correct |
1 ms |
7000 KB |
Output is correct |
33 |
Correct |
1 ms |
6748 KB |
Output is correct |
34 |
Correct |
1 ms |
4700 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
1 ms |
6748 KB |
Output is correct |
37 |
Correct |
1 ms |
4700 KB |
Output is correct |
38 |
Correct |
1 ms |
7000 KB |
Output is correct |
39 |
Correct |
1 ms |
7004 KB |
Output is correct |
40 |
Correct |
1 ms |
7004 KB |
Output is correct |
41 |
Correct |
2 ms |
7004 KB |
Output is correct |
42 |
Correct |
2 ms |
7004 KB |
Output is correct |
43 |
Correct |
1 ms |
7004 KB |
Output is correct |
44 |
Correct |
1 ms |
7004 KB |
Output is correct |
45 |
Correct |
2 ms |
7004 KB |
Output is correct |
46 |
Correct |
1 ms |
7004 KB |
Output is correct |
47 |
Correct |
1 ms |
7004 KB |
Output is correct |
48 |
Correct |
1 ms |
7004 KB |
Output is correct |
49 |
Correct |
1 ms |
7004 KB |
Output is correct |
50 |
Correct |
1 ms |
7004 KB |
Output is correct |
51 |
Correct |
1 ms |
4696 KB |
Output is correct |
52 |
Correct |
1 ms |
4700 KB |
Output is correct |
53 |
Correct |
1 ms |
4700 KB |
Output is correct |
54 |
Correct |
1 ms |
6748 KB |
Output is correct |
55 |
Correct |
1 ms |
6748 KB |
Output is correct |
56 |
Correct |
1 ms |
7004 KB |
Output is correct |
57 |
Correct |
3 ms |
11612 KB |
Output is correct |
58 |
Correct |
5 ms |
18524 KB |
Output is correct |
59 |
Correct |
5 ms |
18268 KB |
Output is correct |
60 |
Correct |
6 ms |
18340 KB |
Output is correct |
61 |
Correct |
5 ms |
18428 KB |
Output is correct |
62 |
Correct |
6 ms |
18264 KB |
Output is correct |
63 |
Correct |
5 ms |
18264 KB |
Output is correct |
64 |
Correct |
5 ms |
18268 KB |
Output is correct |
65 |
Correct |
5 ms |
18268 KB |
Output is correct |
66 |
Correct |
6 ms |
18268 KB |
Output is correct |
67 |
Correct |
5 ms |
18268 KB |
Output is correct |
68 |
Correct |
5 ms |
18268 KB |
Output is correct |
69 |
Correct |
5 ms |
18264 KB |
Output is correct |
70 |
Correct |
5 ms |
18268 KB |
Output is correct |
71 |
Correct |
18 ms |
34136 KB |
Output is correct |
72 |
Correct |
111 ms |
87900 KB |
Output is correct |
73 |
Correct |
77 ms |
69672 KB |
Output is correct |
74 |
Correct |
74 ms |
67920 KB |
Output is correct |
75 |
Correct |
70 ms |
67924 KB |
Output is correct |
76 |
Correct |
70 ms |
67924 KB |
Output is correct |
77 |
Correct |
70 ms |
68128 KB |
Output is correct |
78 |
Correct |
62 ms |
69180 KB |
Output is correct |
79 |
Correct |
92 ms |
83544 KB |
Output is correct |
80 |
Correct |
67 ms |
68432 KB |
Output is correct |
81 |
Correct |
70 ms |
69456 KB |
Output is correct |
82 |
Correct |
80 ms |
76112 KB |
Output is correct |
83 |
Correct |
79 ms |
75768 KB |
Output is correct |
84 |
Correct |
68 ms |
67920 KB |
Output is correct |
85 |
Correct |
200 ms |
112212 KB |
Output is correct |
86 |
Correct |
502 ms |
180572 KB |
Output is correct |
87 |
Correct |
458 ms |
170044 KB |
Output is correct |
88 |
Correct |
435 ms |
162788 KB |
Output is correct |
89 |
Correct |
414 ms |
162664 KB |
Output is correct |
90 |
Correct |
414 ms |
162940 KB |
Output is correct |
91 |
Correct |
532 ms |
218192 KB |
Output is correct |
92 |
Correct |
632 ms |
254544 KB |
Output is correct |
93 |
Correct |
420 ms |
171604 KB |
Output is correct |
94 |
Correct |
440 ms |
177488 KB |
Output is correct |
95 |
Correct |
596 ms |
233232 KB |
Output is correct |
96 |
Correct |
635 ms |
233044 KB |
Output is correct |
97 |
Correct |
411 ms |
162644 KB |
Output is correct |