#pragma GCC optimize("O3,unroll-loops")
#include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
template <typename T>
using ve = vector<T>;
const int N = 2001;
const ll B = 137, M = 1000000007,
B2 = 141, M2 = 1000000009;
int n;
ll dp[N][N], a, b, c;
char s[N];
ll hs[N], bp[N], ibp[N], bp2[N], ibp2[N], hs2[N];
ll bpow(ll a, ll b, ll m) {
if (!b) return 1;
ll r { bpow(a, b / 2, m) };
if (b & 1) return r * r % m * a % m;
return r * r % m;
}
void do_hash() {
bp[0] = ibp[0] = bp2[0] = ibp2[0] = 1;
for (int i = 1; i <= n; ++i) bp[i] = bp[i - 1] * B % M, ibp[i] = bpow(bp[i], M - 2, M);
for (int i = 1; i <= n; ++i) hs[i] = bp[i] * (s[i] - 'a' + 1) % M, hs[i] = (hs[i] + hs[i - 1]) % M;
for (int i = 1; i <= n; ++i) bp2[i] = bp2[i - 1] * B2 % M2, ibp2[i] = bpow(bp2[i], M2 - 2, M2);
for (int i = 1; i <= n; ++i) hs2[i] = bp2[i] * (s[i] - 'a' + 1) % M2, hs2[i] = (hs2[i] + hs2[i - 1]) % M2;
}
pair<ll, ll> get_hash(int l, int r) {
return {ibp[l] * (hs[r] - hs[l - 1] + M) % M, ibp2[l] * (hs2[r] - hs2[l - 1] + M2) % M2};
}
map<pair<ll, ll>, ve<int>> oo, op[N];
void init() {
do_hash();
memset(dp, 63, sizeof dp);
for (int i = 1; i <= n; ++i) {
dp[i][i] = a;
for (int j = i; j <= n; ++j)
oo[get_hash(i, j)].push_back(i);
}
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
auto x = get_hash(i, j);
auto &v = oo[x];
auto it = lower_bound(v.begin(), v.end(), i);
while (it != v.end()) {
op[i][x].push_back(*it);
it = lower_bound(v.begin(), v.end(), *it + j - i + 1);
}
}
}
}
int main() {
scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
init();
for (int l = 1; l <= n; ++l) {
for (int i = 1, j = l; j <= n; ++i, ++j) {
if (i < j) dp[i][j] = min(dp[i][j], dp[i + 1][j] + a);
if (j > i) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a);
auto x = get_hash(i, j);
auto &oc = op[i][x];
auto it = lower_bound(oc.begin(), oc.end(), i);
int cnt = 1;
for (; it != oc.end(); ++it, ++cnt) {
int jj = *it + l - 1;
dp[i][jj] = min(dp[i][jj], dp[i][j] + b + cnt * c + (jj - i + 1 - cnt * l) * a);
}
}
}
printf("%lld", dp[1][n]);
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
31832 KB |
Output is correct |
2 |
Correct |
4 ms |
31836 KB |
Output is correct |
3 |
Correct |
3 ms |
31836 KB |
Output is correct |
4 |
Correct |
3 ms |
31836 KB |
Output is correct |
5 |
Correct |
4 ms |
31836 KB |
Output is correct |
6 |
Correct |
4 ms |
31880 KB |
Output is correct |
7 |
Correct |
3 ms |
31828 KB |
Output is correct |
8 |
Correct |
4 ms |
31836 KB |
Output is correct |
9 |
Correct |
3 ms |
31836 KB |
Output is correct |
10 |
Correct |
3 ms |
31836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
31836 KB |
Output is correct |
2 |
Correct |
4 ms |
31836 KB |
Output is correct |
3 |
Correct |
2472 ms |
273740 KB |
Output is correct |
4 |
Execution timed out |
3004 ms |
314736 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
31832 KB |
Output is correct |
2 |
Correct |
4 ms |
31836 KB |
Output is correct |
3 |
Correct |
3 ms |
31836 KB |
Output is correct |
4 |
Correct |
3 ms |
31836 KB |
Output is correct |
5 |
Correct |
4 ms |
31836 KB |
Output is correct |
6 |
Correct |
4 ms |
31880 KB |
Output is correct |
7 |
Correct |
3 ms |
31828 KB |
Output is correct |
8 |
Correct |
4 ms |
31836 KB |
Output is correct |
9 |
Correct |
3 ms |
31836 KB |
Output is correct |
10 |
Correct |
3 ms |
31836 KB |
Output is correct |
11 |
Correct |
4 ms |
31832 KB |
Output is correct |
12 |
Correct |
4 ms |
31836 KB |
Output is correct |
13 |
Correct |
4 ms |
31836 KB |
Output is correct |
14 |
Correct |
4 ms |
31836 KB |
Output is correct |
15 |
Correct |
4 ms |
31836 KB |
Output is correct |
16 |
Correct |
3 ms |
31836 KB |
Output is correct |
17 |
Correct |
4 ms |
31836 KB |
Output is correct |
18 |
Correct |
3 ms |
31832 KB |
Output is correct |
19 |
Correct |
4 ms |
31836 KB |
Output is correct |
20 |
Correct |
3 ms |
31836 KB |
Output is correct |
21 |
Correct |
4 ms |
31832 KB |
Output is correct |
22 |
Correct |
4 ms |
31820 KB |
Output is correct |
23 |
Correct |
4 ms |
31836 KB |
Output is correct |
24 |
Correct |
4 ms |
31824 KB |
Output is correct |
25 |
Correct |
4 ms |
31836 KB |
Output is correct |
26 |
Correct |
4 ms |
31836 KB |
Output is correct |
27 |
Correct |
4 ms |
31872 KB |
Output is correct |
28 |
Correct |
4 ms |
31836 KB |
Output is correct |
29 |
Correct |
4 ms |
31836 KB |
Output is correct |
30 |
Correct |
5 ms |
31808 KB |
Output is correct |
31 |
Correct |
4 ms |
31836 KB |
Output is correct |
32 |
Correct |
4 ms |
31832 KB |
Output is correct |
33 |
Correct |
4 ms |
31836 KB |
Output is correct |
34 |
Correct |
3 ms |
31836 KB |
Output is correct |
35 |
Correct |
4 ms |
31836 KB |
Output is correct |
36 |
Correct |
4 ms |
31836 KB |
Output is correct |
37 |
Correct |
4 ms |
31824 KB |
Output is correct |
38 |
Correct |
3 ms |
31804 KB |
Output is correct |
39 |
Correct |
4 ms |
31836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
31832 KB |
Output is correct |
2 |
Correct |
4 ms |
31836 KB |
Output is correct |
3 |
Correct |
3 ms |
31836 KB |
Output is correct |
4 |
Correct |
3 ms |
31836 KB |
Output is correct |
5 |
Correct |
4 ms |
31836 KB |
Output is correct |
6 |
Correct |
4 ms |
31880 KB |
Output is correct |
7 |
Correct |
3 ms |
31828 KB |
Output is correct |
8 |
Correct |
4 ms |
31836 KB |
Output is correct |
9 |
Correct |
3 ms |
31836 KB |
Output is correct |
10 |
Correct |
3 ms |
31836 KB |
Output is correct |
11 |
Correct |
4 ms |
31832 KB |
Output is correct |
12 |
Correct |
4 ms |
31836 KB |
Output is correct |
13 |
Correct |
4 ms |
31836 KB |
Output is correct |
14 |
Correct |
4 ms |
31836 KB |
Output is correct |
15 |
Correct |
4 ms |
31836 KB |
Output is correct |
16 |
Correct |
3 ms |
31836 KB |
Output is correct |
17 |
Correct |
4 ms |
31836 KB |
Output is correct |
18 |
Correct |
3 ms |
31832 KB |
Output is correct |
19 |
Correct |
4 ms |
31836 KB |
Output is correct |
20 |
Correct |
3 ms |
31836 KB |
Output is correct |
21 |
Correct |
4 ms |
31832 KB |
Output is correct |
22 |
Correct |
4 ms |
31820 KB |
Output is correct |
23 |
Correct |
4 ms |
31836 KB |
Output is correct |
24 |
Correct |
4 ms |
31824 KB |
Output is correct |
25 |
Correct |
4 ms |
31836 KB |
Output is correct |
26 |
Correct |
4 ms |
31836 KB |
Output is correct |
27 |
Correct |
4 ms |
31872 KB |
Output is correct |
28 |
Correct |
4 ms |
31836 KB |
Output is correct |
29 |
Correct |
4 ms |
31836 KB |
Output is correct |
30 |
Correct |
5 ms |
31808 KB |
Output is correct |
31 |
Correct |
4 ms |
31836 KB |
Output is correct |
32 |
Correct |
4 ms |
31832 KB |
Output is correct |
33 |
Correct |
4 ms |
31836 KB |
Output is correct |
34 |
Correct |
3 ms |
31836 KB |
Output is correct |
35 |
Correct |
4 ms |
31836 KB |
Output is correct |
36 |
Correct |
4 ms |
31836 KB |
Output is correct |
37 |
Correct |
4 ms |
31824 KB |
Output is correct |
38 |
Correct |
3 ms |
31804 KB |
Output is correct |
39 |
Correct |
4 ms |
31836 KB |
Output is correct |
40 |
Correct |
5 ms |
32604 KB |
Output is correct |
41 |
Correct |
15 ms |
34396 KB |
Output is correct |
42 |
Correct |
21 ms |
36284 KB |
Output is correct |
43 |
Correct |
17 ms |
36196 KB |
Output is correct |
44 |
Correct |
17 ms |
36188 KB |
Output is correct |
45 |
Correct |
18 ms |
36188 KB |
Output is correct |
46 |
Correct |
23 ms |
36060 KB |
Output is correct |
47 |
Correct |
17 ms |
34652 KB |
Output is correct |
48 |
Correct |
22 ms |
35264 KB |
Output is correct |
49 |
Correct |
18 ms |
35676 KB |
Output is correct |
50 |
Correct |
18 ms |
35676 KB |
Output is correct |
51 |
Correct |
17 ms |
35376 KB |
Output is correct |
52 |
Correct |
17 ms |
35164 KB |
Output is correct |
53 |
Correct |
19 ms |
36188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
31832 KB |
Output is correct |
2 |
Correct |
4 ms |
31836 KB |
Output is correct |
3 |
Correct |
3 ms |
31836 KB |
Output is correct |
4 |
Correct |
3 ms |
31836 KB |
Output is correct |
5 |
Correct |
4 ms |
31836 KB |
Output is correct |
6 |
Correct |
4 ms |
31880 KB |
Output is correct |
7 |
Correct |
3 ms |
31828 KB |
Output is correct |
8 |
Correct |
4 ms |
31836 KB |
Output is correct |
9 |
Correct |
3 ms |
31836 KB |
Output is correct |
10 |
Correct |
3 ms |
31836 KB |
Output is correct |
11 |
Correct |
4 ms |
31832 KB |
Output is correct |
12 |
Correct |
4 ms |
31836 KB |
Output is correct |
13 |
Correct |
4 ms |
31836 KB |
Output is correct |
14 |
Correct |
4 ms |
31836 KB |
Output is correct |
15 |
Correct |
4 ms |
31836 KB |
Output is correct |
16 |
Correct |
3 ms |
31836 KB |
Output is correct |
17 |
Correct |
4 ms |
31836 KB |
Output is correct |
18 |
Correct |
3 ms |
31832 KB |
Output is correct |
19 |
Correct |
4 ms |
31836 KB |
Output is correct |
20 |
Correct |
3 ms |
31836 KB |
Output is correct |
21 |
Correct |
4 ms |
31832 KB |
Output is correct |
22 |
Correct |
4 ms |
31820 KB |
Output is correct |
23 |
Correct |
4 ms |
31836 KB |
Output is correct |
24 |
Correct |
4 ms |
31824 KB |
Output is correct |
25 |
Correct |
4 ms |
31836 KB |
Output is correct |
26 |
Correct |
4 ms |
31836 KB |
Output is correct |
27 |
Correct |
4 ms |
31872 KB |
Output is correct |
28 |
Correct |
4 ms |
31836 KB |
Output is correct |
29 |
Correct |
4 ms |
31836 KB |
Output is correct |
30 |
Correct |
5 ms |
31808 KB |
Output is correct |
31 |
Correct |
4 ms |
31836 KB |
Output is correct |
32 |
Correct |
4 ms |
31832 KB |
Output is correct |
33 |
Correct |
4 ms |
31836 KB |
Output is correct |
34 |
Correct |
3 ms |
31836 KB |
Output is correct |
35 |
Correct |
4 ms |
31836 KB |
Output is correct |
36 |
Correct |
4 ms |
31836 KB |
Output is correct |
37 |
Correct |
4 ms |
31824 KB |
Output is correct |
38 |
Correct |
3 ms |
31804 KB |
Output is correct |
39 |
Correct |
4 ms |
31836 KB |
Output is correct |
40 |
Correct |
5 ms |
32604 KB |
Output is correct |
41 |
Correct |
15 ms |
34396 KB |
Output is correct |
42 |
Correct |
21 ms |
36284 KB |
Output is correct |
43 |
Correct |
17 ms |
36196 KB |
Output is correct |
44 |
Correct |
17 ms |
36188 KB |
Output is correct |
45 |
Correct |
18 ms |
36188 KB |
Output is correct |
46 |
Correct |
23 ms |
36060 KB |
Output is correct |
47 |
Correct |
17 ms |
34652 KB |
Output is correct |
48 |
Correct |
22 ms |
35264 KB |
Output is correct |
49 |
Correct |
18 ms |
35676 KB |
Output is correct |
50 |
Correct |
18 ms |
35676 KB |
Output is correct |
51 |
Correct |
17 ms |
35376 KB |
Output is correct |
52 |
Correct |
17 ms |
35164 KB |
Output is correct |
53 |
Correct |
19 ms |
36188 KB |
Output is correct |
54 |
Correct |
106 ms |
53964 KB |
Output is correct |
55 |
Correct |
646 ms |
102224 KB |
Output is correct |
56 |
Correct |
891 ms |
142932 KB |
Output is correct |
57 |
Correct |
936 ms |
142300 KB |
Output is correct |
58 |
Correct |
867 ms |
141652 KB |
Output is correct |
59 |
Correct |
921 ms |
141560 KB |
Output is correct |
60 |
Correct |
893 ms |
141392 KB |
Output is correct |
61 |
Correct |
709 ms |
110160 KB |
Output is correct |
62 |
Correct |
589 ms |
100184 KB |
Output is correct |
63 |
Correct |
811 ms |
127824 KB |
Output is correct |
64 |
Correct |
851 ms |
129324 KB |
Output is correct |
65 |
Correct |
889 ms |
118808 KB |
Output is correct |
66 |
Correct |
873 ms |
118716 KB |
Output is correct |
67 |
Correct |
925 ms |
141396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
31832 KB |
Output is correct |
2 |
Correct |
4 ms |
31836 KB |
Output is correct |
3 |
Correct |
3 ms |
31836 KB |
Output is correct |
4 |
Correct |
3 ms |
31836 KB |
Output is correct |
5 |
Correct |
4 ms |
31836 KB |
Output is correct |
6 |
Correct |
4 ms |
31880 KB |
Output is correct |
7 |
Correct |
3 ms |
31828 KB |
Output is correct |
8 |
Correct |
4 ms |
31836 KB |
Output is correct |
9 |
Correct |
3 ms |
31836 KB |
Output is correct |
10 |
Correct |
3 ms |
31836 KB |
Output is correct |
11 |
Correct |
3 ms |
31836 KB |
Output is correct |
12 |
Correct |
4 ms |
31836 KB |
Output is correct |
13 |
Correct |
2472 ms |
273740 KB |
Output is correct |
14 |
Execution timed out |
3004 ms |
314736 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |