#include <bits/stdc++.h>
using namespace std;
const int BASE = 77777;
const int md1 = 1e9 + 7;
const int md2 = 1e9 + 9;
int ckadd1(int a, int b) {
return a + b < md1 ? a + b : a + b - md1;
}
int cksub1(int a, int b) {
return a >= b ? a - b : a - b + md1;
}
int ckmul1(int a, int b) {
return a * 1LL * b % md1;
}
int ckadd2(int a, int b) {
return a + b < md2 ? a + b : a + b - md2;
}
int cksub2(int a, int b) {
return a >= b ? a - b : a - b + md2;
}
int ckmul2(int a, int b) {
return a * 1LL * b % md2;
}
int qpow1(int x, int k) {
int ret = 1;
while (k) {
if (k & 1) {
ret = ckmul1(ret, x);
}
x = ckmul1(x, x);
k >>= 1;
}
return ret;
}
int qpow2(int x, int k) {
int ret = 1;
while (k) {
if (k & 1) {
ret = ckmul2(ret, x);
}
x = ckmul2(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> pw1(n);
pw1[0] = 1;
for (int i = 1; i < n; i++) {
pw1[i] = ckmul1(pw1[i - 1], BASE);
}
vector<int> dv1(n);
dv1[n - 1] = qpow1(pw1[n - 1], md1 - 2);
for (int i = n - 2; i >= 0; i--) {
dv1[i] = ckmul1(dv1[i + 1], BASE);
}
vector<int> pref1(n);
for (int i = 0; i < n; i++) {
if (i > 0) {
pref1[i] = pref1[i - 1];
}
pref1[i] = ckadd1(pref1[i], ckmul1(pw1[i], s[i] - 'a'));
}
vector<int> pw2(n);
pw2[0] = 1;
for (int i = 1; i < n; i++) {
pw2[i] = ckmul2(pw2[i - 1], BASE);
}
vector<int> dv2(n);
dv2[n - 1] = qpow2(pw2[n - 1], md2 - 2);
for (int i = n - 2; i >= 0; i--) {
dv2[i] = ckmul2(dv2[i + 1], BASE);
}
vector<int> pref2(n);
for (int i = 0; i < n; i++) {
if (i > 0) {
pref2[i] = pref2[i - 1];
}
pref2[i] = ckadd2(pref2[i], ckmul2(pw2[i], s[i] - 'a'));
}
auto Get1 = [&](int L, int R) {
int ret = pref1[R];
if (L > 0) {
ret = cksub1(ret, pref1[L - 1]);
}
return ckmul1(ret, dv1[L]);
};
auto Get2 = [&](int L, int R) {
int ret = pref2[R];
if (L > 0) {
ret = cksub2(ret, pref2[L - 1]);
}
return ckmul2(ret, dv2[L]);
};
auto Get = [&](int L, int R) {
return make_pair(Get1(L, R), Get2(L, R));
};
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<pair<int, 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;
}
pair<int, 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
856 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 |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
111 ms |
26716 KB |
Output is correct |
4 |
Correct |
153 ms |
31068 KB |
Output is correct |
5 |
Correct |
203 ms |
36184 KB |
Output is correct |
6 |
Correct |
240 ms |
42328 KB |
Output is correct |
7 |
Correct |
296 ms |
49496 KB |
Output is correct |
8 |
Correct |
295 ms |
49756 KB |
Output is correct |
9 |
Correct |
323 ms |
49492 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
712 KB |
Output is correct |
17 |
Correct |
0 ms |
360 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
856 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 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 |
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 |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
856 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 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 |
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 |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
3 ms |
604 KB |
Output is correct |
43 |
Correct |
3 ms |
604 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
3 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
604 KB |
Output is correct |
48 |
Correct |
3 ms |
788 KB |
Output is correct |
49 |
Correct |
3 ms |
604 KB |
Output is correct |
50 |
Correct |
3 ms |
604 KB |
Output is correct |
51 |
Correct |
3 ms |
708 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
856 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 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 |
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 |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
3 ms |
604 KB |
Output is correct |
43 |
Correct |
3 ms |
604 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
3 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
604 KB |
Output is correct |
48 |
Correct |
3 ms |
788 KB |
Output is correct |
49 |
Correct |
3 ms |
604 KB |
Output is correct |
50 |
Correct |
3 ms |
604 KB |
Output is correct |
51 |
Correct |
3 ms |
708 KB |
Output is correct |
52 |
Correct |
2 ms |
604 KB |
Output is correct |
53 |
Correct |
3 ms |
604 KB |
Output is correct |
54 |
Correct |
17 ms |
1880 KB |
Output is correct |
55 |
Correct |
19 ms |
8284 KB |
Output is correct |
56 |
Correct |
90 ms |
8532 KB |
Output is correct |
57 |
Correct |
90 ms |
8280 KB |
Output is correct |
58 |
Correct |
90 ms |
8280 KB |
Output is correct |
59 |
Correct |
89 ms |
8284 KB |
Output is correct |
60 |
Correct |
92 ms |
8284 KB |
Output is correct |
61 |
Correct |
72 ms |
8284 KB |
Output is correct |
62 |
Correct |
19 ms |
8284 KB |
Output is correct |
63 |
Correct |
85 ms |
8372 KB |
Output is correct |
64 |
Correct |
84 ms |
8284 KB |
Output is correct |
65 |
Correct |
71 ms |
8356 KB |
Output is correct |
66 |
Correct |
71 ms |
8344 KB |
Output is correct |
67 |
Correct |
89 ms |
8284 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
856 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
111 ms |
26716 KB |
Output is correct |
14 |
Correct |
153 ms |
31068 KB |
Output is correct |
15 |
Correct |
203 ms |
36184 KB |
Output is correct |
16 |
Correct |
240 ms |
42328 KB |
Output is correct |
17 |
Correct |
296 ms |
49496 KB |
Output is correct |
18 |
Correct |
295 ms |
49756 KB |
Output is correct |
19 |
Correct |
323 ms |
49492 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 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 |
712 KB |
Output is correct |
27 |
Correct |
0 ms |
360 KB |
Output is correct |
28 |
Correct |
0 ms |
452 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
456 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 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 |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 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 |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
604 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
3 ms |
604 KB |
Output is correct |
60 |
Correct |
3 ms |
604 KB |
Output is correct |
61 |
Correct |
3 ms |
604 KB |
Output is correct |
62 |
Correct |
3 ms |
604 KB |
Output is correct |
63 |
Correct |
3 ms |
604 KB |
Output is correct |
64 |
Correct |
2 ms |
604 KB |
Output is correct |
65 |
Correct |
3 ms |
788 KB |
Output is correct |
66 |
Correct |
3 ms |
604 KB |
Output is correct |
67 |
Correct |
3 ms |
604 KB |
Output is correct |
68 |
Correct |
3 ms |
708 KB |
Output is correct |
69 |
Correct |
2 ms |
604 KB |
Output is correct |
70 |
Correct |
3 ms |
604 KB |
Output is correct |
71 |
Correct |
17 ms |
1880 KB |
Output is correct |
72 |
Correct |
19 ms |
8284 KB |
Output is correct |
73 |
Correct |
90 ms |
8532 KB |
Output is correct |
74 |
Correct |
90 ms |
8280 KB |
Output is correct |
75 |
Correct |
90 ms |
8280 KB |
Output is correct |
76 |
Correct |
89 ms |
8284 KB |
Output is correct |
77 |
Correct |
92 ms |
8284 KB |
Output is correct |
78 |
Correct |
72 ms |
8284 KB |
Output is correct |
79 |
Correct |
19 ms |
8284 KB |
Output is correct |
80 |
Correct |
85 ms |
8372 KB |
Output is correct |
81 |
Correct |
84 ms |
8284 KB |
Output is correct |
82 |
Correct |
71 ms |
8356 KB |
Output is correct |
83 |
Correct |
71 ms |
8344 KB |
Output is correct |
84 |
Correct |
89 ms |
8284 KB |
Output is correct |
85 |
Correct |
240 ms |
20304 KB |
Output is correct |
86 |
Correct |
628 ms |
49684 KB |
Output is correct |
87 |
Correct |
611 ms |
49708 KB |
Output is correct |
88 |
Correct |
611 ms |
49744 KB |
Output is correct |
89 |
Correct |
620 ms |
49688 KB |
Output is correct |
90 |
Correct |
611 ms |
49500 KB |
Output is correct |
91 |
Correct |
267 ms |
49540 KB |
Output is correct |
92 |
Correct |
248 ms |
49744 KB |
Output is correct |
93 |
Correct |
586 ms |
49612 KB |
Output is correct |
94 |
Correct |
589 ms |
49748 KB |
Output is correct |
95 |
Correct |
560 ms |
49496 KB |
Output is correct |
96 |
Correct |
586 ms |
49496 KB |
Output is correct |
97 |
Correct |
607 ms |
49688 KB |
Output is correct |