#include <bits/stdc++.h>
using namespace std;
const int BASE = 77777;
const int md = 1e9 + 7;
int ckadd(int a, int b) {
return a + b < md ? a + b : a + b - md;
}
int cksub(int a, int b) {
return a >= b ? a - b : a - b + md;
}
int ckmul(int a, int b) {
return a * 1LL * b % md;
}
int qpow(int x, int k) {
int ret = 1;
while (k) {
if (k & 1) {
ret = ckmul(ret, x);
}
x = ckmul(x, x);
k >>= 1;
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int a, b, c;
cin >> a >> b >> c;
vector<int> pw(n);
pw[0] = 1;
for (int i = 1; i < n; i++) {
pw[i] = ckmul(pw[i - 1], BASE);
}
vector<int> dv(n);
dv[n - 1] = qpow(pw[n - 1], md - 2);
for (int i = n - 2; i >= 0; i--) {
dv[i] = ckmul(dv[i + 1], BASE);
}
vector<int> pref(n);
for (int i = 0; i < n; i++) {
if (i > 0) {
pref[i] = pref[i - 1];
}
pref[i] = ckadd(pref[i], ckmul(pw[i], s[i] - 'a'));
}
auto Get = [&](int L, int R) {
int ret = pref[R];
if (L > 0) {
ret = cksub(ret, pref[L - 1]);
}
return ckmul(ret, dv[L]);
};
const long long inf = (long long) 1e18;
vector<vector<long long>> dp(n, vector<long long>(n, inf));
for (int len = 0; len < n; len++) {
vector<int> to(n, -1);
map<int, int> mp;
for (int l = n - len - 1; l >= 0; l--) {
int r = l + len;
if (r + 1 + len < n) {
mp[Get(r + 1, r + 1 + len)] = r + 1;
}
int h = Get(l, r);
if (mp[h] != 0) {
to[l] = mp[h];
}
if (l == r) {
dp[l][r] = a;
}
if (l < r) {
dp[l][r] = min(dp[l][r], min(dp[l + 1][r], dp[l][r - 1]) + a);
}
{
int x = l;
int cnt = 1;
while (to[x] != -1) {
x = to[x];
cnt += 1;
dp[l][x + len] = min(dp[l][x + len], dp[l][r] + b + c * 1LL * cnt + a * 1LL * ((x + len - l + 1) - cnt * (len + 1)));
}
}
}
}
cout << dp[0][n - 1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
138 ms |
26496 KB |
Output is correct |
4 |
Correct |
158 ms |
31128 KB |
Output is correct |
5 |
Correct |
170 ms |
36188 KB |
Output is correct |
6 |
Correct |
218 ms |
42324 KB |
Output is correct |
7 |
Correct |
278 ms |
49508 KB |
Output is correct |
8 |
Correct |
268 ms |
49508 KB |
Output is correct |
9 |
Correct |
270 ms |
49504 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
548 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
460 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
460 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
716 KB |
Output is correct |
42 |
Correct |
3 ms |
604 KB |
Output is correct |
43 |
Correct |
3 ms |
784 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
3 ms |
600 KB |
Output is correct |
46 |
Correct |
3 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
604 KB |
Output is correct |
48 |
Correct |
2 ms |
604 KB |
Output is correct |
49 |
Correct |
3 ms |
780 KB |
Output is correct |
50 |
Correct |
3 ms |
780 KB |
Output is correct |
51 |
Correct |
2 ms |
788 KB |
Output is correct |
52 |
Correct |
2 ms |
604 KB |
Output is correct |
53 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
460 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
716 KB |
Output is correct |
42 |
Correct |
3 ms |
604 KB |
Output is correct |
43 |
Correct |
3 ms |
784 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
3 ms |
600 KB |
Output is correct |
46 |
Correct |
3 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
604 KB |
Output is correct |
48 |
Correct |
2 ms |
604 KB |
Output is correct |
49 |
Correct |
3 ms |
780 KB |
Output is correct |
50 |
Correct |
3 ms |
780 KB |
Output is correct |
51 |
Correct |
2 ms |
788 KB |
Output is correct |
52 |
Correct |
2 ms |
604 KB |
Output is correct |
53 |
Correct |
3 ms |
604 KB |
Output is correct |
54 |
Correct |
16 ms |
1884 KB |
Output is correct |
55 |
Correct |
16 ms |
8284 KB |
Output is correct |
56 |
Correct |
84 ms |
8364 KB |
Output is correct |
57 |
Correct |
89 ms |
8364 KB |
Output is correct |
58 |
Correct |
83 ms |
8284 KB |
Output is correct |
59 |
Correct |
85 ms |
8360 KB |
Output is correct |
60 |
Correct |
83 ms |
8280 KB |
Output is correct |
61 |
Correct |
67 ms |
8284 KB |
Output is correct |
62 |
Correct |
16 ms |
8124 KB |
Output is correct |
63 |
Correct |
78 ms |
8344 KB |
Output is correct |
64 |
Correct |
79 ms |
8356 KB |
Output is correct |
65 |
Correct |
66 ms |
8336 KB |
Output is correct |
66 |
Correct |
70 ms |
8280 KB |
Output is correct |
67 |
Correct |
82 ms |
8360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
138 ms |
26496 KB |
Output is correct |
14 |
Correct |
158 ms |
31128 KB |
Output is correct |
15 |
Correct |
170 ms |
36188 KB |
Output is correct |
16 |
Correct |
218 ms |
42324 KB |
Output is correct |
17 |
Correct |
278 ms |
49508 KB |
Output is correct |
18 |
Correct |
268 ms |
49508 KB |
Output is correct |
19 |
Correct |
270 ms |
49504 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
548 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
460 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
460 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
460 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
2 ms |
716 KB |
Output is correct |
59 |
Correct |
3 ms |
604 KB |
Output is correct |
60 |
Correct |
3 ms |
784 KB |
Output is correct |
61 |
Correct |
3 ms |
604 KB |
Output is correct |
62 |
Correct |
3 ms |
600 KB |
Output is correct |
63 |
Correct |
3 ms |
604 KB |
Output is correct |
64 |
Correct |
2 ms |
604 KB |
Output is correct |
65 |
Correct |
2 ms |
604 KB |
Output is correct |
66 |
Correct |
3 ms |
780 KB |
Output is correct |
67 |
Correct |
3 ms |
780 KB |
Output is correct |
68 |
Correct |
2 ms |
788 KB |
Output is correct |
69 |
Correct |
2 ms |
604 KB |
Output is correct |
70 |
Correct |
3 ms |
604 KB |
Output is correct |
71 |
Correct |
16 ms |
1884 KB |
Output is correct |
72 |
Correct |
16 ms |
8284 KB |
Output is correct |
73 |
Correct |
84 ms |
8364 KB |
Output is correct |
74 |
Correct |
89 ms |
8364 KB |
Output is correct |
75 |
Correct |
83 ms |
8284 KB |
Output is correct |
76 |
Correct |
85 ms |
8360 KB |
Output is correct |
77 |
Correct |
83 ms |
8280 KB |
Output is correct |
78 |
Correct |
67 ms |
8284 KB |
Output is correct |
79 |
Correct |
16 ms |
8124 KB |
Output is correct |
80 |
Correct |
78 ms |
8344 KB |
Output is correct |
81 |
Correct |
79 ms |
8356 KB |
Output is correct |
82 |
Correct |
66 ms |
8336 KB |
Output is correct |
83 |
Correct |
70 ms |
8280 KB |
Output is correct |
84 |
Correct |
82 ms |
8360 KB |
Output is correct |
85 |
Correct |
219 ms |
20308 KB |
Output is correct |
86 |
Correct |
582 ms |
49616 KB |
Output is correct |
87 |
Correct |
562 ms |
49616 KB |
Output is correct |
88 |
Correct |
564 ms |
49860 KB |
Output is correct |
89 |
Incorrect |
560 ms |
49672 KB |
Output isn't correct |
90 |
Halted |
0 ms |
0 KB |
- |