#pragma GCC optimize("O3,unroll-loops")
#include <deque>
#include <string>
#include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
template <typename T>
using ve = vector<T>;
template <typename T>
using bs = basic_string<T>;
const int N = 2505;
const ll B = 137, M = 1000000007,
B2 = 141, M2 = 1000000009;
int n, nxt[N][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;
}
inline 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);
}
ve<ll> chocolate;
ve<bs<int>> oo;
int get_pos(int l, int r) {
return lower_bound(chocolate.begin(), chocolate.end(), get_hash(l, r)) - chocolate.begin();
}
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)
chocolate.push_back(get_hash(i, j));
}
sort(chocolate.begin(), chocolate.end());
chocolate.erase(unique(chocolate.begin(), chocolate.end()), chocolate.end());
for (int l = 1; l <= n; ++l) {
map<ll, deque<int>> w;
ll x;
for (int i = n - l + 1, j = n; i >= 1; --i, --j) {
x = get_hash(i, j);
auto it = lower_bound(w[x].begin(), w[x].end(), i + l);
if (it == w[x].end()) nxt[i][j] = -1;
else nxt[i][j] = *it;
w[x].push_front(i);
}
}
}
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);
int at = i;
int cnt = 1;
for (; ~at; ++cnt, at = nxt[at][at + l - 1]) {
int jj = at + 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:82:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
5 ms |
49704 KB |
Output is correct |
4 |
Correct |
5 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49672 KB |
Output is correct |
6 |
Correct |
6 ms |
49752 KB |
Output is correct |
7 |
Correct |
6 ms |
49756 KB |
Output is correct |
8 |
Correct |
6 ms |
49756 KB |
Output is correct |
9 |
Correct |
5 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
50008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
49732 KB |
Output is correct |
2 |
Correct |
5 ms |
49680 KB |
Output is correct |
3 |
Correct |
199 ms |
82084 KB |
Output is correct |
4 |
Correct |
232 ms |
86180 KB |
Output is correct |
5 |
Correct |
272 ms |
91548 KB |
Output is correct |
6 |
Correct |
370 ms |
93856 KB |
Output is correct |
7 |
Correct |
398 ms |
98208 KB |
Output is correct |
8 |
Correct |
399 ms |
98928 KB |
Output is correct |
9 |
Correct |
393 ms |
98976 KB |
Output is correct |
10 |
Correct |
6 ms |
49756 KB |
Output is correct |
11 |
Correct |
5 ms |
49756 KB |
Output is correct |
12 |
Correct |
5 ms |
49756 KB |
Output is correct |
13 |
Correct |
6 ms |
49704 KB |
Output is correct |
14 |
Correct |
5 ms |
49756 KB |
Output is correct |
15 |
Correct |
5 ms |
49756 KB |
Output is correct |
16 |
Correct |
6 ms |
51744 KB |
Output is correct |
17 |
Correct |
5 ms |
49756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
5 ms |
49704 KB |
Output is correct |
4 |
Correct |
5 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49672 KB |
Output is correct |
6 |
Correct |
6 ms |
49752 KB |
Output is correct |
7 |
Correct |
6 ms |
49756 KB |
Output is correct |
8 |
Correct |
6 ms |
49756 KB |
Output is correct |
9 |
Correct |
5 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
50008 KB |
Output is correct |
11 |
Correct |
6 ms |
49756 KB |
Output is correct |
12 |
Correct |
5 ms |
49756 KB |
Output is correct |
13 |
Correct |
6 ms |
51804 KB |
Output is correct |
14 |
Correct |
5 ms |
49756 KB |
Output is correct |
15 |
Correct |
6 ms |
51804 KB |
Output is correct |
16 |
Correct |
6 ms |
49756 KB |
Output is correct |
17 |
Correct |
5 ms |
49732 KB |
Output is correct |
18 |
Correct |
7 ms |
49600 KB |
Output is correct |
19 |
Correct |
6 ms |
49756 KB |
Output is correct |
20 |
Correct |
6 ms |
49676 KB |
Output is correct |
21 |
Correct |
5 ms |
51804 KB |
Output is correct |
22 |
Correct |
6 ms |
51804 KB |
Output is correct |
23 |
Correct |
7 ms |
51804 KB |
Output is correct |
24 |
Correct |
6 ms |
51804 KB |
Output is correct |
25 |
Correct |
6 ms |
51804 KB |
Output is correct |
26 |
Correct |
7 ms |
51640 KB |
Output is correct |
27 |
Correct |
7 ms |
51804 KB |
Output is correct |
28 |
Correct |
6 ms |
51804 KB |
Output is correct |
29 |
Correct |
6 ms |
51816 KB |
Output is correct |
30 |
Correct |
6 ms |
51804 KB |
Output is correct |
31 |
Correct |
6 ms |
51804 KB |
Output is correct |
32 |
Correct |
6 ms |
51720 KB |
Output is correct |
33 |
Correct |
6 ms |
51804 KB |
Output is correct |
34 |
Correct |
6 ms |
49756 KB |
Output is correct |
35 |
Correct |
5 ms |
49756 KB |
Output is correct |
36 |
Correct |
6 ms |
49756 KB |
Output is correct |
37 |
Correct |
5 ms |
49756 KB |
Output is correct |
38 |
Correct |
5 ms |
49688 KB |
Output is correct |
39 |
Correct |
7 ms |
51804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
5 ms |
49704 KB |
Output is correct |
4 |
Correct |
5 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49672 KB |
Output is correct |
6 |
Correct |
6 ms |
49752 KB |
Output is correct |
7 |
Correct |
6 ms |
49756 KB |
Output is correct |
8 |
Correct |
6 ms |
49756 KB |
Output is correct |
9 |
Correct |
5 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
50008 KB |
Output is correct |
11 |
Correct |
6 ms |
49756 KB |
Output is correct |
12 |
Correct |
5 ms |
49756 KB |
Output is correct |
13 |
Correct |
6 ms |
51804 KB |
Output is correct |
14 |
Correct |
5 ms |
49756 KB |
Output is correct |
15 |
Correct |
6 ms |
51804 KB |
Output is correct |
16 |
Correct |
6 ms |
49756 KB |
Output is correct |
17 |
Correct |
5 ms |
49732 KB |
Output is correct |
18 |
Correct |
7 ms |
49600 KB |
Output is correct |
19 |
Correct |
6 ms |
49756 KB |
Output is correct |
20 |
Correct |
6 ms |
49676 KB |
Output is correct |
21 |
Correct |
5 ms |
51804 KB |
Output is correct |
22 |
Correct |
6 ms |
51804 KB |
Output is correct |
23 |
Correct |
7 ms |
51804 KB |
Output is correct |
24 |
Correct |
6 ms |
51804 KB |
Output is correct |
25 |
Correct |
6 ms |
51804 KB |
Output is correct |
26 |
Correct |
7 ms |
51640 KB |
Output is correct |
27 |
Correct |
7 ms |
51804 KB |
Output is correct |
28 |
Correct |
6 ms |
51804 KB |
Output is correct |
29 |
Correct |
6 ms |
51816 KB |
Output is correct |
30 |
Correct |
6 ms |
51804 KB |
Output is correct |
31 |
Correct |
6 ms |
51804 KB |
Output is correct |
32 |
Correct |
6 ms |
51720 KB |
Output is correct |
33 |
Correct |
6 ms |
51804 KB |
Output is correct |
34 |
Correct |
6 ms |
49756 KB |
Output is correct |
35 |
Correct |
5 ms |
49756 KB |
Output is correct |
36 |
Correct |
6 ms |
49756 KB |
Output is correct |
37 |
Correct |
5 ms |
49756 KB |
Output is correct |
38 |
Correct |
5 ms |
49688 KB |
Output is correct |
39 |
Correct |
7 ms |
51804 KB |
Output is correct |
40 |
Correct |
7 ms |
51800 KB |
Output is correct |
41 |
Correct |
7 ms |
52012 KB |
Output is correct |
42 |
Correct |
15 ms |
52184 KB |
Output is correct |
43 |
Correct |
12 ms |
52184 KB |
Output is correct |
44 |
Correct |
12 ms |
52108 KB |
Output is correct |
45 |
Correct |
12 ms |
52180 KB |
Output is correct |
46 |
Correct |
13 ms |
52184 KB |
Output is correct |
47 |
Correct |
9 ms |
51928 KB |
Output is correct |
48 |
Correct |
10 ms |
51932 KB |
Output is correct |
49 |
Correct |
11 ms |
51928 KB |
Output is correct |
50 |
Correct |
11 ms |
51928 KB |
Output is correct |
51 |
Correct |
10 ms |
51928 KB |
Output is correct |
52 |
Correct |
11 ms |
51928 KB |
Output is correct |
53 |
Correct |
12 ms |
52184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
5 ms |
49704 KB |
Output is correct |
4 |
Correct |
5 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49672 KB |
Output is correct |
6 |
Correct |
6 ms |
49752 KB |
Output is correct |
7 |
Correct |
6 ms |
49756 KB |
Output is correct |
8 |
Correct |
6 ms |
49756 KB |
Output is correct |
9 |
Correct |
5 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
50008 KB |
Output is correct |
11 |
Correct |
6 ms |
49756 KB |
Output is correct |
12 |
Correct |
5 ms |
49756 KB |
Output is correct |
13 |
Correct |
6 ms |
51804 KB |
Output is correct |
14 |
Correct |
5 ms |
49756 KB |
Output is correct |
15 |
Correct |
6 ms |
51804 KB |
Output is correct |
16 |
Correct |
6 ms |
49756 KB |
Output is correct |
17 |
Correct |
5 ms |
49732 KB |
Output is correct |
18 |
Correct |
7 ms |
49600 KB |
Output is correct |
19 |
Correct |
6 ms |
49756 KB |
Output is correct |
20 |
Correct |
6 ms |
49676 KB |
Output is correct |
21 |
Correct |
5 ms |
51804 KB |
Output is correct |
22 |
Correct |
6 ms |
51804 KB |
Output is correct |
23 |
Correct |
7 ms |
51804 KB |
Output is correct |
24 |
Correct |
6 ms |
51804 KB |
Output is correct |
25 |
Correct |
6 ms |
51804 KB |
Output is correct |
26 |
Correct |
7 ms |
51640 KB |
Output is correct |
27 |
Correct |
7 ms |
51804 KB |
Output is correct |
28 |
Correct |
6 ms |
51804 KB |
Output is correct |
29 |
Correct |
6 ms |
51816 KB |
Output is correct |
30 |
Correct |
6 ms |
51804 KB |
Output is correct |
31 |
Correct |
6 ms |
51804 KB |
Output is correct |
32 |
Correct |
6 ms |
51720 KB |
Output is correct |
33 |
Correct |
6 ms |
51804 KB |
Output is correct |
34 |
Correct |
6 ms |
49756 KB |
Output is correct |
35 |
Correct |
5 ms |
49756 KB |
Output is correct |
36 |
Correct |
6 ms |
49756 KB |
Output is correct |
37 |
Correct |
5 ms |
49756 KB |
Output is correct |
38 |
Correct |
5 ms |
49688 KB |
Output is correct |
39 |
Correct |
7 ms |
51804 KB |
Output is correct |
40 |
Correct |
7 ms |
51800 KB |
Output is correct |
41 |
Correct |
7 ms |
52012 KB |
Output is correct |
42 |
Correct |
15 ms |
52184 KB |
Output is correct |
43 |
Correct |
12 ms |
52184 KB |
Output is correct |
44 |
Correct |
12 ms |
52108 KB |
Output is correct |
45 |
Correct |
12 ms |
52180 KB |
Output is correct |
46 |
Correct |
13 ms |
52184 KB |
Output is correct |
47 |
Correct |
9 ms |
51928 KB |
Output is correct |
48 |
Correct |
10 ms |
51932 KB |
Output is correct |
49 |
Correct |
11 ms |
51928 KB |
Output is correct |
50 |
Correct |
11 ms |
51928 KB |
Output is correct |
51 |
Correct |
10 ms |
51928 KB |
Output is correct |
52 |
Correct |
11 ms |
51928 KB |
Output is correct |
53 |
Correct |
12 ms |
52184 KB |
Output is correct |
54 |
Correct |
42 ms |
57108 KB |
Output is correct |
55 |
Correct |
58 ms |
63932 KB |
Output is correct |
56 |
Correct |
213 ms |
65132 KB |
Output is correct |
57 |
Correct |
216 ms |
65208 KB |
Output is correct |
58 |
Correct |
221 ms |
65204 KB |
Output is correct |
59 |
Correct |
213 ms |
64960 KB |
Output is correct |
60 |
Correct |
211 ms |
65216 KB |
Output is correct |
61 |
Correct |
162 ms |
64456 KB |
Output is correct |
62 |
Correct |
57 ms |
63928 KB |
Output is correct |
63 |
Correct |
199 ms |
64612 KB |
Output is correct |
64 |
Correct |
193 ms |
64696 KB |
Output is correct |
65 |
Correct |
161 ms |
64440 KB |
Output is correct |
66 |
Correct |
158 ms |
64692 KB |
Output is correct |
67 |
Correct |
217 ms |
65208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
49756 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
5 ms |
49704 KB |
Output is correct |
4 |
Correct |
5 ms |
49756 KB |
Output is correct |
5 |
Correct |
5 ms |
49672 KB |
Output is correct |
6 |
Correct |
6 ms |
49752 KB |
Output is correct |
7 |
Correct |
6 ms |
49756 KB |
Output is correct |
8 |
Correct |
6 ms |
49756 KB |
Output is correct |
9 |
Correct |
5 ms |
49756 KB |
Output is correct |
10 |
Correct |
5 ms |
50008 KB |
Output is correct |
11 |
Correct |
6 ms |
49732 KB |
Output is correct |
12 |
Correct |
5 ms |
49680 KB |
Output is correct |
13 |
Correct |
199 ms |
82084 KB |
Output is correct |
14 |
Correct |
232 ms |
86180 KB |
Output is correct |
15 |
Correct |
272 ms |
91548 KB |
Output is correct |
16 |
Correct |
370 ms |
93856 KB |
Output is correct |
17 |
Correct |
398 ms |
98208 KB |
Output is correct |
18 |
Correct |
399 ms |
98928 KB |
Output is correct |
19 |
Correct |
393 ms |
98976 KB |
Output is correct |
20 |
Correct |
6 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
49756 KB |
Output is correct |
22 |
Correct |
5 ms |
49756 KB |
Output is correct |
23 |
Correct |
6 ms |
49704 KB |
Output is correct |
24 |
Correct |
5 ms |
49756 KB |
Output is correct |
25 |
Correct |
5 ms |
49756 KB |
Output is correct |
26 |
Correct |
6 ms |
51744 KB |
Output is correct |
27 |
Correct |
5 ms |
49756 KB |
Output is correct |
28 |
Correct |
6 ms |
49756 KB |
Output is correct |
29 |
Correct |
5 ms |
49756 KB |
Output is correct |
30 |
Correct |
6 ms |
51804 KB |
Output is correct |
31 |
Correct |
5 ms |
49756 KB |
Output is correct |
32 |
Correct |
6 ms |
51804 KB |
Output is correct |
33 |
Correct |
6 ms |
49756 KB |
Output is correct |
34 |
Correct |
5 ms |
49732 KB |
Output is correct |
35 |
Correct |
7 ms |
49600 KB |
Output is correct |
36 |
Correct |
6 ms |
49756 KB |
Output is correct |
37 |
Correct |
6 ms |
49676 KB |
Output is correct |
38 |
Correct |
5 ms |
51804 KB |
Output is correct |
39 |
Correct |
6 ms |
51804 KB |
Output is correct |
40 |
Correct |
7 ms |
51804 KB |
Output is correct |
41 |
Correct |
6 ms |
51804 KB |
Output is correct |
42 |
Correct |
6 ms |
51804 KB |
Output is correct |
43 |
Correct |
7 ms |
51640 KB |
Output is correct |
44 |
Correct |
7 ms |
51804 KB |
Output is correct |
45 |
Correct |
6 ms |
51804 KB |
Output is correct |
46 |
Correct |
6 ms |
51816 KB |
Output is correct |
47 |
Correct |
6 ms |
51804 KB |
Output is correct |
48 |
Correct |
6 ms |
51804 KB |
Output is correct |
49 |
Correct |
6 ms |
51720 KB |
Output is correct |
50 |
Correct |
6 ms |
51804 KB |
Output is correct |
51 |
Correct |
6 ms |
49756 KB |
Output is correct |
52 |
Correct |
5 ms |
49756 KB |
Output is correct |
53 |
Correct |
6 ms |
49756 KB |
Output is correct |
54 |
Correct |
5 ms |
49756 KB |
Output is correct |
55 |
Correct |
5 ms |
49688 KB |
Output is correct |
56 |
Correct |
7 ms |
51804 KB |
Output is correct |
57 |
Correct |
7 ms |
51800 KB |
Output is correct |
58 |
Correct |
7 ms |
52012 KB |
Output is correct |
59 |
Correct |
15 ms |
52184 KB |
Output is correct |
60 |
Correct |
12 ms |
52184 KB |
Output is correct |
61 |
Correct |
12 ms |
52108 KB |
Output is correct |
62 |
Correct |
12 ms |
52180 KB |
Output is correct |
63 |
Correct |
13 ms |
52184 KB |
Output is correct |
64 |
Correct |
9 ms |
51928 KB |
Output is correct |
65 |
Correct |
10 ms |
51932 KB |
Output is correct |
66 |
Correct |
11 ms |
51928 KB |
Output is correct |
67 |
Correct |
11 ms |
51928 KB |
Output is correct |
68 |
Correct |
10 ms |
51928 KB |
Output is correct |
69 |
Correct |
11 ms |
51928 KB |
Output is correct |
70 |
Correct |
12 ms |
52184 KB |
Output is correct |
71 |
Correct |
42 ms |
57108 KB |
Output is correct |
72 |
Correct |
58 ms |
63932 KB |
Output is correct |
73 |
Correct |
213 ms |
65132 KB |
Output is correct |
74 |
Correct |
216 ms |
65208 KB |
Output is correct |
75 |
Correct |
221 ms |
65204 KB |
Output is correct |
76 |
Correct |
213 ms |
64960 KB |
Output is correct |
77 |
Correct |
211 ms |
65216 KB |
Output is correct |
78 |
Correct |
162 ms |
64456 KB |
Output is correct |
79 |
Correct |
57 ms |
63928 KB |
Output is correct |
80 |
Correct |
199 ms |
64612 KB |
Output is correct |
81 |
Correct |
193 ms |
64696 KB |
Output is correct |
82 |
Correct |
161 ms |
64440 KB |
Output is correct |
83 |
Correct |
158 ms |
64692 KB |
Output is correct |
84 |
Correct |
217 ms |
65208 KB |
Output is correct |
85 |
Correct |
599 ms |
79100 KB |
Output is correct |
86 |
Correct |
1582 ms |
102044 KB |
Output is correct |
87 |
Correct |
1603 ms |
102428 KB |
Output is correct |
88 |
Correct |
1561 ms |
101732 KB |
Output is correct |
89 |
Correct |
1643 ms |
101272 KB |
Output is correct |
90 |
Correct |
1555 ms |
102136 KB |
Output is correct |
91 |
Correct |
502 ms |
98972 KB |
Output is correct |
92 |
Correct |
409 ms |
99688 KB |
Output is correct |
93 |
Correct |
1346 ms |
100512 KB |
Output is correct |
94 |
Correct |
1449 ms |
101012 KB |
Output is correct |
95 |
Correct |
1147 ms |
99540 KB |
Output is correct |
96 |
Correct |
1147 ms |
99232 KB |
Output is correct |
97 |
Correct |
1609 ms |
101532 KB |
Output is correct |