#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, mod = 1000000321, p = 31;
ll dp[N][N], mem[N][N];
int to[N][N], nw[N][N], hsh[N], pdeg[N], pinv[N];
vector<pair<int, int>> upd[N];
int add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
int sub(int a, int b) {
return a >= b ? a - b : a - b + mod;
}
int mul(int a, int b) {
return 1ll * a * b % mod;
}
int bin_pow(int a, int x) {
int res = 1;
while (x) {
if (x & 1) {
res = mul(res, a);
}
a = mul(a, a);
x >>= 1;
}
return res;
}
int inv(int 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]);
}
}
int get_hash(int l, int r) {
return mul(sub(hsh[r + 1], hsh[l]), pinv[l]);
}
struct segment_tree {
vector<ll> st;
int size;
segment_tree(int sz = N) {
st.resize(2 * sz, 0);
size = sz;
}
void add(int i, ll v) {
i += size;
st[i] += v;
i >>= 1;
while (i) {
st[i] = min(st[2 * i], st[2 * i + 1]);
i >>= 1;
}
}
ll get_min(int l, int r) {
l += size;
r += size;
ll res = INT64_MAX;
while (l <= r) {
if (l & 1) {
res = min(res, st[l++]);
}
if (~r & 1) {
res = min(res, st[r--]);
}
l >>= 1;
r >>= 1;
}
return res;
}
};
segment_tree st[N];
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<int, int> mp;
for (int i = 0; i < n - l; i++) {
int j = i + l;
int 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;
st[i].add(i, b + c);
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++) {
assert(upd[l].size() <= 20 * N);
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] += b - 1ll * (j - i + 1) * a;
// st[j].add(i, c - 1ll * (j - i + 1) * a);
mem[j][i] += b - 1ll * (j - i + 1) * a;
}
for (auto [i, j] : upd[l]) {
if (mem[j][i]) {
st[j].add(i, c - 1ll * (j - i + 1) * a);
mem[j][i] = 0;
}
}
for (int i = 0; i < n - l; i++) {
int j = i + l;
dp[i][j] = min(1ll * (j - i + 1) * a + min(0ll, st[j].get_min(i + 1, j)), dp[i][j - 1] + a);
st[j].add(i, dp[i][j] + b + c - 1ll * (j - i + 1) * a);
nw[i][j] = to[i][j];
if (nw[i][j] != -1) {
upd[j - nw[i][j]].emplace_back(i, j);
}
}
}
// cout << to[3][3] << '\n';
// for (int i = 0; i < n; i++) {
// for (int j = i; j < n; j++) {
// cout << dp[i][j] << ' ';
// }
// cout << '\n';
// }
cout << dp[0][n - 1] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
108632 KB |
Output is correct |
2 |
Correct |
39 ms |
108628 KB |
Output is correct |
3 |
Correct |
40 ms |
108628 KB |
Output is correct |
4 |
Correct |
38 ms |
108624 KB |
Output is correct |
5 |
Correct |
38 ms |
108636 KB |
Output is correct |
6 |
Correct |
39 ms |
106588 KB |
Output is correct |
7 |
Correct |
40 ms |
106580 KB |
Output is correct |
8 |
Correct |
38 ms |
108632 KB |
Output is correct |
9 |
Correct |
38 ms |
108636 KB |
Output is correct |
10 |
Correct |
39 ms |
108688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
108592 KB |
Output is correct |
2 |
Correct |
38 ms |
108628 KB |
Output is correct |
3 |
Correct |
1248 ms |
300368 KB |
Output is correct |
4 |
Correct |
1514 ms |
326072 KB |
Output is correct |
5 |
Correct |
1911 ms |
354392 KB |
Output is correct |
6 |
Correct |
2356 ms |
381732 KB |
Output is correct |
7 |
Runtime error |
1791 ms |
576972 KB |
Execution killed with signal 6 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
108632 KB |
Output is correct |
2 |
Correct |
39 ms |
108628 KB |
Output is correct |
3 |
Correct |
40 ms |
108628 KB |
Output is correct |
4 |
Correct |
38 ms |
108624 KB |
Output is correct |
5 |
Correct |
38 ms |
108636 KB |
Output is correct |
6 |
Correct |
39 ms |
106588 KB |
Output is correct |
7 |
Correct |
40 ms |
106580 KB |
Output is correct |
8 |
Correct |
38 ms |
108632 KB |
Output is correct |
9 |
Correct |
38 ms |
108636 KB |
Output is correct |
10 |
Correct |
39 ms |
108688 KB |
Output is correct |
11 |
Correct |
39 ms |
108640 KB |
Output is correct |
12 |
Correct |
38 ms |
108648 KB |
Output is correct |
13 |
Correct |
39 ms |
108648 KB |
Output is correct |
14 |
Correct |
38 ms |
108648 KB |
Output is correct |
15 |
Correct |
40 ms |
108648 KB |
Output is correct |
16 |
Correct |
39 ms |
108544 KB |
Output is correct |
17 |
Correct |
38 ms |
108640 KB |
Output is correct |
18 |
Correct |
37 ms |
104544 KB |
Output is correct |
19 |
Correct |
39 ms |
108660 KB |
Output is correct |
20 |
Correct |
39 ms |
108640 KB |
Output is correct |
21 |
Correct |
38 ms |
108736 KB |
Output is correct |
22 |
Correct |
39 ms |
108640 KB |
Output is correct |
23 |
Correct |
39 ms |
108640 KB |
Output is correct |
24 |
Correct |
39 ms |
108696 KB |
Output is correct |
25 |
Correct |
39 ms |
108764 KB |
Output is correct |
26 |
Correct |
38 ms |
108640 KB |
Output is correct |
27 |
Correct |
41 ms |
108692 KB |
Output is correct |
28 |
Correct |
38 ms |
108640 KB |
Output is correct |
29 |
Correct |
38 ms |
108628 KB |
Output is correct |
30 |
Correct |
44 ms |
108612 KB |
Output is correct |
31 |
Correct |
40 ms |
108628 KB |
Output is correct |
32 |
Correct |
41 ms |
108624 KB |
Output is correct |
33 |
Correct |
38 ms |
108812 KB |
Output is correct |
34 |
Correct |
41 ms |
104528 KB |
Output is correct |
35 |
Correct |
38 ms |
108628 KB |
Output is correct |
36 |
Correct |
39 ms |
108656 KB |
Output is correct |
37 |
Correct |
39 ms |
108624 KB |
Output is correct |
38 |
Correct |
38 ms |
108624 KB |
Output is correct |
39 |
Correct |
38 ms |
108596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
108632 KB |
Output is correct |
2 |
Correct |
39 ms |
108628 KB |
Output is correct |
3 |
Correct |
40 ms |
108628 KB |
Output is correct |
4 |
Correct |
38 ms |
108624 KB |
Output is correct |
5 |
Correct |
38 ms |
108636 KB |
Output is correct |
6 |
Correct |
39 ms |
106588 KB |
Output is correct |
7 |
Correct |
40 ms |
106580 KB |
Output is correct |
8 |
Correct |
38 ms |
108632 KB |
Output is correct |
9 |
Correct |
38 ms |
108636 KB |
Output is correct |
10 |
Correct |
39 ms |
108688 KB |
Output is correct |
11 |
Correct |
39 ms |
108640 KB |
Output is correct |
12 |
Correct |
38 ms |
108648 KB |
Output is correct |
13 |
Correct |
39 ms |
108648 KB |
Output is correct |
14 |
Correct |
38 ms |
108648 KB |
Output is correct |
15 |
Correct |
40 ms |
108648 KB |
Output is correct |
16 |
Correct |
39 ms |
108544 KB |
Output is correct |
17 |
Correct |
38 ms |
108640 KB |
Output is correct |
18 |
Correct |
37 ms |
104544 KB |
Output is correct |
19 |
Correct |
39 ms |
108660 KB |
Output is correct |
20 |
Correct |
39 ms |
108640 KB |
Output is correct |
21 |
Correct |
38 ms |
108736 KB |
Output is correct |
22 |
Correct |
39 ms |
108640 KB |
Output is correct |
23 |
Correct |
39 ms |
108640 KB |
Output is correct |
24 |
Correct |
39 ms |
108696 KB |
Output is correct |
25 |
Correct |
39 ms |
108764 KB |
Output is correct |
26 |
Correct |
38 ms |
108640 KB |
Output is correct |
27 |
Correct |
41 ms |
108692 KB |
Output is correct |
28 |
Correct |
38 ms |
108640 KB |
Output is correct |
29 |
Correct |
38 ms |
108628 KB |
Output is correct |
30 |
Correct |
44 ms |
108612 KB |
Output is correct |
31 |
Correct |
40 ms |
108628 KB |
Output is correct |
32 |
Correct |
41 ms |
108624 KB |
Output is correct |
33 |
Correct |
38 ms |
108812 KB |
Output is correct |
34 |
Correct |
41 ms |
104528 KB |
Output is correct |
35 |
Correct |
38 ms |
108628 KB |
Output is correct |
36 |
Correct |
39 ms |
108656 KB |
Output is correct |
37 |
Correct |
39 ms |
108624 KB |
Output is correct |
38 |
Correct |
38 ms |
108624 KB |
Output is correct |
39 |
Correct |
38 ms |
108596 KB |
Output is correct |
40 |
Correct |
39 ms |
113232 KB |
Output is correct |
41 |
Correct |
46 ms |
120148 KB |
Output is correct |
42 |
Correct |
43 ms |
119884 KB |
Output is correct |
43 |
Correct |
45 ms |
119892 KB |
Output is correct |
44 |
Correct |
43 ms |
119892 KB |
Output is correct |
45 |
Correct |
43 ms |
119892 KB |
Output is correct |
46 |
Correct |
48 ms |
120040 KB |
Output is correct |
47 |
Correct |
43 ms |
119900 KB |
Output is correct |
48 |
Correct |
43 ms |
120012 KB |
Output is correct |
49 |
Correct |
43 ms |
119952 KB |
Output is correct |
50 |
Correct |
44 ms |
119916 KB |
Output is correct |
51 |
Correct |
43 ms |
119900 KB |
Output is correct |
52 |
Correct |
48 ms |
120032 KB |
Output is correct |
53 |
Correct |
43 ms |
119888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
108632 KB |
Output is correct |
2 |
Correct |
39 ms |
108628 KB |
Output is correct |
3 |
Correct |
40 ms |
108628 KB |
Output is correct |
4 |
Correct |
38 ms |
108624 KB |
Output is correct |
5 |
Correct |
38 ms |
108636 KB |
Output is correct |
6 |
Correct |
39 ms |
106588 KB |
Output is correct |
7 |
Correct |
40 ms |
106580 KB |
Output is correct |
8 |
Correct |
38 ms |
108632 KB |
Output is correct |
9 |
Correct |
38 ms |
108636 KB |
Output is correct |
10 |
Correct |
39 ms |
108688 KB |
Output is correct |
11 |
Correct |
39 ms |
108640 KB |
Output is correct |
12 |
Correct |
38 ms |
108648 KB |
Output is correct |
13 |
Correct |
39 ms |
108648 KB |
Output is correct |
14 |
Correct |
38 ms |
108648 KB |
Output is correct |
15 |
Correct |
40 ms |
108648 KB |
Output is correct |
16 |
Correct |
39 ms |
108544 KB |
Output is correct |
17 |
Correct |
38 ms |
108640 KB |
Output is correct |
18 |
Correct |
37 ms |
104544 KB |
Output is correct |
19 |
Correct |
39 ms |
108660 KB |
Output is correct |
20 |
Correct |
39 ms |
108640 KB |
Output is correct |
21 |
Correct |
38 ms |
108736 KB |
Output is correct |
22 |
Correct |
39 ms |
108640 KB |
Output is correct |
23 |
Correct |
39 ms |
108640 KB |
Output is correct |
24 |
Correct |
39 ms |
108696 KB |
Output is correct |
25 |
Correct |
39 ms |
108764 KB |
Output is correct |
26 |
Correct |
38 ms |
108640 KB |
Output is correct |
27 |
Correct |
41 ms |
108692 KB |
Output is correct |
28 |
Correct |
38 ms |
108640 KB |
Output is correct |
29 |
Correct |
38 ms |
108628 KB |
Output is correct |
30 |
Correct |
44 ms |
108612 KB |
Output is correct |
31 |
Correct |
40 ms |
108628 KB |
Output is correct |
32 |
Correct |
41 ms |
108624 KB |
Output is correct |
33 |
Correct |
38 ms |
108812 KB |
Output is correct |
34 |
Correct |
41 ms |
104528 KB |
Output is correct |
35 |
Correct |
38 ms |
108628 KB |
Output is correct |
36 |
Correct |
39 ms |
108656 KB |
Output is correct |
37 |
Correct |
39 ms |
108624 KB |
Output is correct |
38 |
Correct |
38 ms |
108624 KB |
Output is correct |
39 |
Correct |
38 ms |
108596 KB |
Output is correct |
40 |
Correct |
39 ms |
113232 KB |
Output is correct |
41 |
Correct |
46 ms |
120148 KB |
Output is correct |
42 |
Correct |
43 ms |
119884 KB |
Output is correct |
43 |
Correct |
45 ms |
119892 KB |
Output is correct |
44 |
Correct |
43 ms |
119892 KB |
Output is correct |
45 |
Correct |
43 ms |
119892 KB |
Output is correct |
46 |
Correct |
48 ms |
120040 KB |
Output is correct |
47 |
Correct |
43 ms |
119900 KB |
Output is correct |
48 |
Correct |
43 ms |
120012 KB |
Output is correct |
49 |
Correct |
43 ms |
119952 KB |
Output is correct |
50 |
Correct |
44 ms |
119916 KB |
Output is correct |
51 |
Correct |
43 ms |
119900 KB |
Output is correct |
52 |
Correct |
48 ms |
120032 KB |
Output is correct |
53 |
Correct |
43 ms |
119888 KB |
Output is correct |
54 |
Correct |
63 ms |
135608 KB |
Output is correct |
55 |
Correct |
299 ms |
189264 KB |
Output is correct |
56 |
Correct |
172 ms |
171360 KB |
Output is correct |
57 |
Correct |
141 ms |
169552 KB |
Output is correct |
58 |
Correct |
129 ms |
169568 KB |
Output is correct |
59 |
Correct |
123 ms |
169556 KB |
Output is correct |
60 |
Correct |
137 ms |
169800 KB |
Output is correct |
61 |
Correct |
143 ms |
170632 KB |
Output is correct |
62 |
Correct |
267 ms |
185048 KB |
Output is correct |
63 |
Correct |
138 ms |
170580 KB |
Output is correct |
64 |
Correct |
149 ms |
170836 KB |
Output is correct |
65 |
Correct |
214 ms |
177484 KB |
Output is correct |
66 |
Correct |
218 ms |
177444 KB |
Output is correct |
67 |
Correct |
122 ms |
169552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
108632 KB |
Output is correct |
2 |
Correct |
39 ms |
108628 KB |
Output is correct |
3 |
Correct |
40 ms |
108628 KB |
Output is correct |
4 |
Correct |
38 ms |
108624 KB |
Output is correct |
5 |
Correct |
38 ms |
108636 KB |
Output is correct |
6 |
Correct |
39 ms |
106588 KB |
Output is correct |
7 |
Correct |
40 ms |
106580 KB |
Output is correct |
8 |
Correct |
38 ms |
108632 KB |
Output is correct |
9 |
Correct |
38 ms |
108636 KB |
Output is correct |
10 |
Correct |
39 ms |
108688 KB |
Output is correct |
11 |
Correct |
38 ms |
108592 KB |
Output is correct |
12 |
Correct |
38 ms |
108628 KB |
Output is correct |
13 |
Correct |
1248 ms |
300368 KB |
Output is correct |
14 |
Correct |
1514 ms |
326072 KB |
Output is correct |
15 |
Correct |
1911 ms |
354392 KB |
Output is correct |
16 |
Correct |
2356 ms |
381732 KB |
Output is correct |
17 |
Runtime error |
1791 ms |
576972 KB |
Execution killed with signal 6 |
18 |
Halted |
0 ms |
0 KB |
- |