/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 2e5 + 5;
const int mod[] = {1000000007, 100000009};
const int base = 31;
const ll oo = 1e18;
string s;
int n, a, b, c, dp[2505][2505];
int hsh[2505][2], power[2505][2];
void gen_hash() {
power[0][0] = power[0][1] = 1;
for(int i = 1; i <= n; ++i) {
for(int j = 0; j < 2; ++j) {
hsh[i][j] = (hsh[i - 1][j] * base + s[i] - 'a') % mod[j];
power[i][j] = power[i - 1][j] * base % mod[j];
}
}
}
array<int, 2> get_hash(int l, int r) {
array<int, 2> res;
for(int i = 0; i < 2; ++i) {
res[i] = hsh[r][i];
res[i] -= hsh[l - 1][i] * power[r - l + 1][i] % mod[i];
res[i] = res[i] < 0 ? res[i] + mod[i] : res[i];
}
return res;
}
void solve() {
cin >> n >> s >> a >> b >> c;
memset(dp, 0x3f, sizeof dp);
s = " " + s;
for(int i = 1; i <= n; ++i)
dp[i][i] = a;
gen_hash();
for(int len = 1; len <= n; ++len) {
map<array<int, 2>, vi> mp;
vi nxt(n - len + 2);
for(int l = 1; l + len - 1 <= n; ++l) {
int r = l + len - 1; mp[get_hash(l, r)].emplace_back(l);
dp[l][r] = min({dp[l][r], dp[l][r - 1] + a, dp[l + 1][r] + a});
}
for(auto &[val_hash, v] : mp) {
int sz = isz(v), idx = 0;
for(int tmp : v) {
while(idx < sz && v[idx] < tmp + len) ++idx;
nxt[tmp] = (idx == sz ? 0 : v[idx]);
}
}
for(int l = 1; l + len - 1 <= n; ++l) {
int idx = nxt[l], val = dp[l][l + len - 1], cnt = 1;
while(idx) {
int idx_r = idx + len - 1; ++cnt, idx = nxt[idx];
dp[l][idx_r] = min(dp[l][idx_r], val + b + c * cnt + a * (idx_r - l + 1 - cnt * len));
}
}
}
cout << dp[1][n] << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49428 KB |
Output is correct |
3 |
Correct |
20 ms |
49364 KB |
Output is correct |
4 |
Correct |
18 ms |
49368 KB |
Output is correct |
5 |
Correct |
18 ms |
49364 KB |
Output is correct |
6 |
Correct |
19 ms |
49364 KB |
Output is correct |
7 |
Correct |
19 ms |
49448 KB |
Output is correct |
8 |
Correct |
19 ms |
49428 KB |
Output is correct |
9 |
Correct |
20 ms |
49364 KB |
Output is correct |
10 |
Correct |
18 ms |
49364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
49428 KB |
Output is correct |
2 |
Correct |
18 ms |
49392 KB |
Output is correct |
3 |
Correct |
113 ms |
49492 KB |
Output is correct |
4 |
Correct |
146 ms |
49544 KB |
Output is correct |
5 |
Correct |
180 ms |
49492 KB |
Output is correct |
6 |
Correct |
219 ms |
49576 KB |
Output is correct |
7 |
Correct |
269 ms |
49608 KB |
Output is correct |
8 |
Correct |
257 ms |
49584 KB |
Output is correct |
9 |
Correct |
258 ms |
49580 KB |
Output is correct |
10 |
Correct |
19 ms |
49404 KB |
Output is correct |
11 |
Correct |
19 ms |
49364 KB |
Output is correct |
12 |
Correct |
19 ms |
49364 KB |
Output is correct |
13 |
Correct |
19 ms |
49340 KB |
Output is correct |
14 |
Correct |
19 ms |
49340 KB |
Output is correct |
15 |
Correct |
20 ms |
49420 KB |
Output is correct |
16 |
Correct |
20 ms |
49384 KB |
Output is correct |
17 |
Correct |
19 ms |
49364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49428 KB |
Output is correct |
3 |
Correct |
20 ms |
49364 KB |
Output is correct |
4 |
Correct |
18 ms |
49368 KB |
Output is correct |
5 |
Correct |
18 ms |
49364 KB |
Output is correct |
6 |
Correct |
19 ms |
49364 KB |
Output is correct |
7 |
Correct |
19 ms |
49448 KB |
Output is correct |
8 |
Correct |
19 ms |
49428 KB |
Output is correct |
9 |
Correct |
20 ms |
49364 KB |
Output is correct |
10 |
Correct |
18 ms |
49364 KB |
Output is correct |
11 |
Correct |
19 ms |
49340 KB |
Output is correct |
12 |
Correct |
19 ms |
49368 KB |
Output is correct |
13 |
Correct |
19 ms |
49364 KB |
Output is correct |
14 |
Correct |
19 ms |
49384 KB |
Output is correct |
15 |
Correct |
19 ms |
49364 KB |
Output is correct |
16 |
Correct |
20 ms |
49428 KB |
Output is correct |
17 |
Correct |
19 ms |
49324 KB |
Output is correct |
18 |
Correct |
20 ms |
49396 KB |
Output is correct |
19 |
Correct |
19 ms |
49332 KB |
Output is correct |
20 |
Correct |
19 ms |
49348 KB |
Output is correct |
21 |
Correct |
19 ms |
49444 KB |
Output is correct |
22 |
Correct |
19 ms |
49332 KB |
Output is correct |
23 |
Correct |
19 ms |
49444 KB |
Output is correct |
24 |
Correct |
20 ms |
49364 KB |
Output is correct |
25 |
Correct |
20 ms |
49416 KB |
Output is correct |
26 |
Correct |
19 ms |
49344 KB |
Output is correct |
27 |
Correct |
20 ms |
49356 KB |
Output is correct |
28 |
Correct |
19 ms |
49364 KB |
Output is correct |
29 |
Correct |
19 ms |
49356 KB |
Output is correct |
30 |
Correct |
20 ms |
49364 KB |
Output is correct |
31 |
Correct |
20 ms |
49364 KB |
Output is correct |
32 |
Correct |
20 ms |
49356 KB |
Output is correct |
33 |
Correct |
20 ms |
49360 KB |
Output is correct |
34 |
Correct |
21 ms |
49456 KB |
Output is correct |
35 |
Correct |
20 ms |
49320 KB |
Output is correct |
36 |
Correct |
19 ms |
49364 KB |
Output is correct |
37 |
Correct |
20 ms |
49364 KB |
Output is correct |
38 |
Correct |
19 ms |
49352 KB |
Output is correct |
39 |
Correct |
18 ms |
49428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49428 KB |
Output is correct |
3 |
Correct |
20 ms |
49364 KB |
Output is correct |
4 |
Correct |
18 ms |
49368 KB |
Output is correct |
5 |
Correct |
18 ms |
49364 KB |
Output is correct |
6 |
Correct |
19 ms |
49364 KB |
Output is correct |
7 |
Correct |
19 ms |
49448 KB |
Output is correct |
8 |
Correct |
19 ms |
49428 KB |
Output is correct |
9 |
Correct |
20 ms |
49364 KB |
Output is correct |
10 |
Correct |
18 ms |
49364 KB |
Output is correct |
11 |
Correct |
19 ms |
49340 KB |
Output is correct |
12 |
Correct |
19 ms |
49368 KB |
Output is correct |
13 |
Correct |
19 ms |
49364 KB |
Output is correct |
14 |
Correct |
19 ms |
49384 KB |
Output is correct |
15 |
Correct |
19 ms |
49364 KB |
Output is correct |
16 |
Correct |
20 ms |
49428 KB |
Output is correct |
17 |
Correct |
19 ms |
49324 KB |
Output is correct |
18 |
Correct |
20 ms |
49396 KB |
Output is correct |
19 |
Correct |
19 ms |
49332 KB |
Output is correct |
20 |
Correct |
19 ms |
49348 KB |
Output is correct |
21 |
Correct |
19 ms |
49444 KB |
Output is correct |
22 |
Correct |
19 ms |
49332 KB |
Output is correct |
23 |
Correct |
19 ms |
49444 KB |
Output is correct |
24 |
Correct |
20 ms |
49364 KB |
Output is correct |
25 |
Correct |
20 ms |
49416 KB |
Output is correct |
26 |
Correct |
19 ms |
49344 KB |
Output is correct |
27 |
Correct |
20 ms |
49356 KB |
Output is correct |
28 |
Correct |
19 ms |
49364 KB |
Output is correct |
29 |
Correct |
19 ms |
49356 KB |
Output is correct |
30 |
Correct |
20 ms |
49364 KB |
Output is correct |
31 |
Correct |
20 ms |
49364 KB |
Output is correct |
32 |
Correct |
20 ms |
49356 KB |
Output is correct |
33 |
Correct |
20 ms |
49360 KB |
Output is correct |
34 |
Correct |
21 ms |
49456 KB |
Output is correct |
35 |
Correct |
20 ms |
49320 KB |
Output is correct |
36 |
Correct |
19 ms |
49364 KB |
Output is correct |
37 |
Correct |
20 ms |
49364 KB |
Output is correct |
38 |
Correct |
19 ms |
49352 KB |
Output is correct |
39 |
Correct |
18 ms |
49428 KB |
Output is correct |
40 |
Correct |
18 ms |
49432 KB |
Output is correct |
41 |
Correct |
19 ms |
49472 KB |
Output is correct |
42 |
Correct |
22 ms |
49408 KB |
Output is correct |
43 |
Correct |
22 ms |
49492 KB |
Output is correct |
44 |
Correct |
22 ms |
49460 KB |
Output is correct |
45 |
Correct |
22 ms |
49456 KB |
Output is correct |
46 |
Correct |
21 ms |
49500 KB |
Output is correct |
47 |
Correct |
21 ms |
49404 KB |
Output is correct |
48 |
Correct |
22 ms |
49380 KB |
Output is correct |
49 |
Correct |
22 ms |
49428 KB |
Output is correct |
50 |
Correct |
22 ms |
49404 KB |
Output is correct |
51 |
Correct |
21 ms |
49492 KB |
Output is correct |
52 |
Correct |
21 ms |
49496 KB |
Output is correct |
53 |
Correct |
23 ms |
49392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49428 KB |
Output is correct |
3 |
Correct |
20 ms |
49364 KB |
Output is correct |
4 |
Correct |
18 ms |
49368 KB |
Output is correct |
5 |
Correct |
18 ms |
49364 KB |
Output is correct |
6 |
Correct |
19 ms |
49364 KB |
Output is correct |
7 |
Correct |
19 ms |
49448 KB |
Output is correct |
8 |
Correct |
19 ms |
49428 KB |
Output is correct |
9 |
Correct |
20 ms |
49364 KB |
Output is correct |
10 |
Correct |
18 ms |
49364 KB |
Output is correct |
11 |
Correct |
19 ms |
49340 KB |
Output is correct |
12 |
Correct |
19 ms |
49368 KB |
Output is correct |
13 |
Correct |
19 ms |
49364 KB |
Output is correct |
14 |
Correct |
19 ms |
49384 KB |
Output is correct |
15 |
Correct |
19 ms |
49364 KB |
Output is correct |
16 |
Correct |
20 ms |
49428 KB |
Output is correct |
17 |
Correct |
19 ms |
49324 KB |
Output is correct |
18 |
Correct |
20 ms |
49396 KB |
Output is correct |
19 |
Correct |
19 ms |
49332 KB |
Output is correct |
20 |
Correct |
19 ms |
49348 KB |
Output is correct |
21 |
Correct |
19 ms |
49444 KB |
Output is correct |
22 |
Correct |
19 ms |
49332 KB |
Output is correct |
23 |
Correct |
19 ms |
49444 KB |
Output is correct |
24 |
Correct |
20 ms |
49364 KB |
Output is correct |
25 |
Correct |
20 ms |
49416 KB |
Output is correct |
26 |
Correct |
19 ms |
49344 KB |
Output is correct |
27 |
Correct |
20 ms |
49356 KB |
Output is correct |
28 |
Correct |
19 ms |
49364 KB |
Output is correct |
29 |
Correct |
19 ms |
49356 KB |
Output is correct |
30 |
Correct |
20 ms |
49364 KB |
Output is correct |
31 |
Correct |
20 ms |
49364 KB |
Output is correct |
32 |
Correct |
20 ms |
49356 KB |
Output is correct |
33 |
Correct |
20 ms |
49360 KB |
Output is correct |
34 |
Correct |
21 ms |
49456 KB |
Output is correct |
35 |
Correct |
20 ms |
49320 KB |
Output is correct |
36 |
Correct |
19 ms |
49364 KB |
Output is correct |
37 |
Correct |
20 ms |
49364 KB |
Output is correct |
38 |
Correct |
19 ms |
49352 KB |
Output is correct |
39 |
Correct |
18 ms |
49428 KB |
Output is correct |
40 |
Correct |
18 ms |
49432 KB |
Output is correct |
41 |
Correct |
19 ms |
49472 KB |
Output is correct |
42 |
Correct |
22 ms |
49408 KB |
Output is correct |
43 |
Correct |
22 ms |
49492 KB |
Output is correct |
44 |
Correct |
22 ms |
49460 KB |
Output is correct |
45 |
Correct |
22 ms |
49456 KB |
Output is correct |
46 |
Correct |
21 ms |
49500 KB |
Output is correct |
47 |
Correct |
21 ms |
49404 KB |
Output is correct |
48 |
Correct |
22 ms |
49380 KB |
Output is correct |
49 |
Correct |
22 ms |
49428 KB |
Output is correct |
50 |
Correct |
22 ms |
49404 KB |
Output is correct |
51 |
Correct |
21 ms |
49492 KB |
Output is correct |
52 |
Correct |
21 ms |
49496 KB |
Output is correct |
53 |
Correct |
23 ms |
49392 KB |
Output is correct |
54 |
Correct |
36 ms |
49476 KB |
Output is correct |
55 |
Correct |
34 ms |
49508 KB |
Output is correct |
56 |
Correct |
118 ms |
49612 KB |
Output is correct |
57 |
Correct |
119 ms |
49596 KB |
Output is correct |
58 |
Correct |
119 ms |
49492 KB |
Output is correct |
59 |
Correct |
116 ms |
49612 KB |
Output is correct |
60 |
Correct |
117 ms |
49584 KB |
Output is correct |
61 |
Correct |
90 ms |
49520 KB |
Output is correct |
62 |
Correct |
35 ms |
49492 KB |
Output is correct |
63 |
Correct |
105 ms |
49544 KB |
Output is correct |
64 |
Correct |
107 ms |
49528 KB |
Output is correct |
65 |
Correct |
90 ms |
49528 KB |
Output is correct |
66 |
Correct |
91 ms |
49460 KB |
Output is correct |
67 |
Correct |
117 ms |
49600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
49364 KB |
Output is correct |
2 |
Correct |
19 ms |
49428 KB |
Output is correct |
3 |
Correct |
20 ms |
49364 KB |
Output is correct |
4 |
Correct |
18 ms |
49368 KB |
Output is correct |
5 |
Correct |
18 ms |
49364 KB |
Output is correct |
6 |
Correct |
19 ms |
49364 KB |
Output is correct |
7 |
Correct |
19 ms |
49448 KB |
Output is correct |
8 |
Correct |
19 ms |
49428 KB |
Output is correct |
9 |
Correct |
20 ms |
49364 KB |
Output is correct |
10 |
Correct |
18 ms |
49364 KB |
Output is correct |
11 |
Correct |
19 ms |
49428 KB |
Output is correct |
12 |
Correct |
18 ms |
49392 KB |
Output is correct |
13 |
Correct |
113 ms |
49492 KB |
Output is correct |
14 |
Correct |
146 ms |
49544 KB |
Output is correct |
15 |
Correct |
180 ms |
49492 KB |
Output is correct |
16 |
Correct |
219 ms |
49576 KB |
Output is correct |
17 |
Correct |
269 ms |
49608 KB |
Output is correct |
18 |
Correct |
257 ms |
49584 KB |
Output is correct |
19 |
Correct |
258 ms |
49580 KB |
Output is correct |
20 |
Correct |
19 ms |
49404 KB |
Output is correct |
21 |
Correct |
19 ms |
49364 KB |
Output is correct |
22 |
Correct |
19 ms |
49364 KB |
Output is correct |
23 |
Correct |
19 ms |
49340 KB |
Output is correct |
24 |
Correct |
19 ms |
49340 KB |
Output is correct |
25 |
Correct |
20 ms |
49420 KB |
Output is correct |
26 |
Correct |
20 ms |
49384 KB |
Output is correct |
27 |
Correct |
19 ms |
49364 KB |
Output is correct |
28 |
Correct |
19 ms |
49340 KB |
Output is correct |
29 |
Correct |
19 ms |
49368 KB |
Output is correct |
30 |
Correct |
19 ms |
49364 KB |
Output is correct |
31 |
Correct |
19 ms |
49384 KB |
Output is correct |
32 |
Correct |
19 ms |
49364 KB |
Output is correct |
33 |
Correct |
20 ms |
49428 KB |
Output is correct |
34 |
Correct |
19 ms |
49324 KB |
Output is correct |
35 |
Correct |
20 ms |
49396 KB |
Output is correct |
36 |
Correct |
19 ms |
49332 KB |
Output is correct |
37 |
Correct |
19 ms |
49348 KB |
Output is correct |
38 |
Correct |
19 ms |
49444 KB |
Output is correct |
39 |
Correct |
19 ms |
49332 KB |
Output is correct |
40 |
Correct |
19 ms |
49444 KB |
Output is correct |
41 |
Correct |
20 ms |
49364 KB |
Output is correct |
42 |
Correct |
20 ms |
49416 KB |
Output is correct |
43 |
Correct |
19 ms |
49344 KB |
Output is correct |
44 |
Correct |
20 ms |
49356 KB |
Output is correct |
45 |
Correct |
19 ms |
49364 KB |
Output is correct |
46 |
Correct |
19 ms |
49356 KB |
Output is correct |
47 |
Correct |
20 ms |
49364 KB |
Output is correct |
48 |
Correct |
20 ms |
49364 KB |
Output is correct |
49 |
Correct |
20 ms |
49356 KB |
Output is correct |
50 |
Correct |
20 ms |
49360 KB |
Output is correct |
51 |
Correct |
21 ms |
49456 KB |
Output is correct |
52 |
Correct |
20 ms |
49320 KB |
Output is correct |
53 |
Correct |
19 ms |
49364 KB |
Output is correct |
54 |
Correct |
20 ms |
49364 KB |
Output is correct |
55 |
Correct |
19 ms |
49352 KB |
Output is correct |
56 |
Correct |
18 ms |
49428 KB |
Output is correct |
57 |
Correct |
18 ms |
49432 KB |
Output is correct |
58 |
Correct |
19 ms |
49472 KB |
Output is correct |
59 |
Correct |
22 ms |
49408 KB |
Output is correct |
60 |
Correct |
22 ms |
49492 KB |
Output is correct |
61 |
Correct |
22 ms |
49460 KB |
Output is correct |
62 |
Correct |
22 ms |
49456 KB |
Output is correct |
63 |
Correct |
21 ms |
49500 KB |
Output is correct |
64 |
Correct |
21 ms |
49404 KB |
Output is correct |
65 |
Correct |
22 ms |
49380 KB |
Output is correct |
66 |
Correct |
22 ms |
49428 KB |
Output is correct |
67 |
Correct |
22 ms |
49404 KB |
Output is correct |
68 |
Correct |
21 ms |
49492 KB |
Output is correct |
69 |
Correct |
21 ms |
49496 KB |
Output is correct |
70 |
Correct |
23 ms |
49392 KB |
Output is correct |
71 |
Correct |
36 ms |
49476 KB |
Output is correct |
72 |
Correct |
34 ms |
49508 KB |
Output is correct |
73 |
Correct |
118 ms |
49612 KB |
Output is correct |
74 |
Correct |
119 ms |
49596 KB |
Output is correct |
75 |
Correct |
119 ms |
49492 KB |
Output is correct |
76 |
Correct |
116 ms |
49612 KB |
Output is correct |
77 |
Correct |
117 ms |
49584 KB |
Output is correct |
78 |
Correct |
90 ms |
49520 KB |
Output is correct |
79 |
Correct |
35 ms |
49492 KB |
Output is correct |
80 |
Correct |
105 ms |
49544 KB |
Output is correct |
81 |
Correct |
107 ms |
49528 KB |
Output is correct |
82 |
Correct |
90 ms |
49528 KB |
Output is correct |
83 |
Correct |
91 ms |
49460 KB |
Output is correct |
84 |
Correct |
117 ms |
49600 KB |
Output is correct |
85 |
Correct |
279 ms |
49620 KB |
Output is correct |
86 |
Correct |
706 ms |
49852 KB |
Output is correct |
87 |
Correct |
687 ms |
49812 KB |
Output is correct |
88 |
Correct |
678 ms |
49816 KB |
Output is correct |
89 |
Correct |
693 ms |
49812 KB |
Output is correct |
90 |
Correct |
693 ms |
49816 KB |
Output is correct |
91 |
Correct |
278 ms |
49596 KB |
Output is correct |
92 |
Correct |
238 ms |
49696 KB |
Output is correct |
93 |
Correct |
592 ms |
49620 KB |
Output is correct |
94 |
Correct |
622 ms |
49704 KB |
Output is correct |
95 |
Correct |
568 ms |
49652 KB |
Output is correct |
96 |
Correct |
561 ms |
49612 KB |
Output is correct |
97 |
Correct |
680 ms |
49740 KB |
Output is correct |