#include <iostream>
#include <map>
using namespace std;
long long int c[3005], p[3005], f[3005][3005], g[3005][3005], mod = 1e9 + 7, b = 177013;
long long int cc[3005], pp[3005], bb = 228922;
long long int check(int i, int j) {
return (c[j] - c[i - 1] * p[j - i + 1] % mod + mod) % mod;
}
long long int check2(int i, int j) {
return (cc[j] - cc[i - 1] * pp[j - i + 1] % mod + mod) % mod;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
long long int n, x, y, z;
string s;
cin >> n >> s >> x >> y >> z;
s = " " + s;
p[0] = pp[0] = 1;
for (int i = 1; i <= n; i++) {
c[i] = (c[i - 1] * b + s[i] - 'a' + 1) % mod;
cc[i] = (cc[i - 1] * bb + s[i] - 'a' + 1) % mod;
p[i] = p[i - 1] * b % mod;
pp[i] = pp[i - 1] * bb % mod;
}
for (int d = 1; d <= n; d++) {
map<pair<long long int, long long int>, int> m;
for (int i = n - d + 1; i >= 1; i--) {
int j = i + d - 1;
g[i][j] = 1e18;
long long int h = check(i, j), k = check2(i, j);
if (m.count({h, k})) f[i][j] = m[{h, k}];
else f[i][j] = 0;
if (j + d - 1 <= n) m[{check(j, j + d - 1), check2(j, j + d - 1)}] = j;
}
}
for (int d = 1; d <= n; d++) {
for (int i = 1; i <= n - d + 1; i++) {
int j = i + d - 1;
g[i][j] = min(g[i][j], x * d);
g[i][j] = min(g[i][j], min(g[i + 1][j], g[i][j - 1]) + x);
int h = i, l = 1;
while (1) {
h = f[h][h + d - 1];
if (h == 0) break;
int k = h + d - 1;
l++;
g[i][k] = min(g[i][k], g[i][j] + y + x * (k - i + 1 - l * d) + l * z);
}
}
}
cout << g[1][n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
123 ms |
41392 KB |
Output is correct |
4 |
Correct |
146 ms |
46928 KB |
Output is correct |
5 |
Correct |
170 ms |
53332 KB |
Output is correct |
6 |
Correct |
223 ms |
60800 KB |
Output is correct |
7 |
Correct |
258 ms |
69528 KB |
Output is correct |
8 |
Correct |
260 ms |
69328 KB |
Output is correct |
9 |
Correct |
270 ms |
69448 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
648 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
460 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 |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
0 ms |
720 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 |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
648 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
460 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 |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
0 ms |
720 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 |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
1116 KB |
Output is correct |
41 |
Correct |
2 ms |
2396 KB |
Output is correct |
42 |
Correct |
3 ms |
2396 KB |
Output is correct |
43 |
Correct |
3 ms |
2396 KB |
Output is correct |
44 |
Correct |
3 ms |
2392 KB |
Output is correct |
45 |
Correct |
3 ms |
2396 KB |
Output is correct |
46 |
Correct |
2 ms |
2396 KB |
Output is correct |
47 |
Correct |
2 ms |
2396 KB |
Output is correct |
48 |
Correct |
3 ms |
2396 KB |
Output is correct |
49 |
Correct |
2 ms |
2396 KB |
Output is correct |
50 |
Correct |
3 ms |
2264 KB |
Output is correct |
51 |
Correct |
2 ms |
2396 KB |
Output is correct |
52 |
Correct |
2 ms |
2396 KB |
Output is correct |
53 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
648 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
460 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 |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
0 ms |
720 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 |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
1116 KB |
Output is correct |
41 |
Correct |
2 ms |
2396 KB |
Output is correct |
42 |
Correct |
3 ms |
2396 KB |
Output is correct |
43 |
Correct |
3 ms |
2396 KB |
Output is correct |
44 |
Correct |
3 ms |
2392 KB |
Output is correct |
45 |
Correct |
3 ms |
2396 KB |
Output is correct |
46 |
Correct |
2 ms |
2396 KB |
Output is correct |
47 |
Correct |
2 ms |
2396 KB |
Output is correct |
48 |
Correct |
3 ms |
2396 KB |
Output is correct |
49 |
Correct |
2 ms |
2396 KB |
Output is correct |
50 |
Correct |
3 ms |
2264 KB |
Output is correct |
51 |
Correct |
2 ms |
2396 KB |
Output is correct |
52 |
Correct |
2 ms |
2396 KB |
Output is correct |
53 |
Correct |
2 ms |
2396 KB |
Output is correct |
54 |
Correct |
11 ms |
5468 KB |
Output is correct |
55 |
Correct |
37 ms |
16152 KB |
Output is correct |
56 |
Correct |
55 ms |
16416 KB |
Output is correct |
57 |
Correct |
57 ms |
16244 KB |
Output is correct |
58 |
Correct |
54 ms |
16208 KB |
Output is correct |
59 |
Correct |
56 ms |
16208 KB |
Output is correct |
60 |
Correct |
56 ms |
16420 KB |
Output is correct |
61 |
Correct |
47 ms |
16160 KB |
Output is correct |
62 |
Correct |
30 ms |
16164 KB |
Output is correct |
63 |
Correct |
53 ms |
16208 KB |
Output is correct |
64 |
Correct |
50 ms |
16208 KB |
Output is correct |
65 |
Correct |
49 ms |
16400 KB |
Output is correct |
66 |
Correct |
48 ms |
16212 KB |
Output is correct |
67 |
Correct |
53 ms |
16212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
123 ms |
41392 KB |
Output is correct |
14 |
Correct |
146 ms |
46928 KB |
Output is correct |
15 |
Correct |
170 ms |
53332 KB |
Output is correct |
16 |
Correct |
223 ms |
60800 KB |
Output is correct |
17 |
Correct |
258 ms |
69528 KB |
Output is correct |
18 |
Correct |
260 ms |
69328 KB |
Output is correct |
19 |
Correct |
270 ms |
69448 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
648 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
460 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 |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
0 ms |
604 KB |
Output is correct |
41 |
Correct |
0 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
600 KB |
Output is correct |
44 |
Correct |
0 ms |
604 KB |
Output is correct |
45 |
Correct |
0 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
0 ms |
716 KB |
Output is correct |
48 |
Correct |
1 ms |
604 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
0 ms |
720 KB |
Output is correct |
51 |
Correct |
0 ms |
344 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 |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
1116 KB |
Output is correct |
58 |
Correct |
2 ms |
2396 KB |
Output is correct |
59 |
Correct |
3 ms |
2396 KB |
Output is correct |
60 |
Correct |
3 ms |
2396 KB |
Output is correct |
61 |
Correct |
3 ms |
2392 KB |
Output is correct |
62 |
Correct |
3 ms |
2396 KB |
Output is correct |
63 |
Correct |
2 ms |
2396 KB |
Output is correct |
64 |
Correct |
2 ms |
2396 KB |
Output is correct |
65 |
Correct |
3 ms |
2396 KB |
Output is correct |
66 |
Correct |
2 ms |
2396 KB |
Output is correct |
67 |
Correct |
3 ms |
2264 KB |
Output is correct |
68 |
Correct |
2 ms |
2396 KB |
Output is correct |
69 |
Correct |
2 ms |
2396 KB |
Output is correct |
70 |
Correct |
2 ms |
2396 KB |
Output is correct |
71 |
Correct |
11 ms |
5468 KB |
Output is correct |
72 |
Correct |
37 ms |
16152 KB |
Output is correct |
73 |
Correct |
55 ms |
16416 KB |
Output is correct |
74 |
Correct |
57 ms |
16244 KB |
Output is correct |
75 |
Correct |
54 ms |
16208 KB |
Output is correct |
76 |
Correct |
56 ms |
16208 KB |
Output is correct |
77 |
Correct |
56 ms |
16420 KB |
Output is correct |
78 |
Correct |
47 ms |
16160 KB |
Output is correct |
79 |
Correct |
30 ms |
16164 KB |
Output is correct |
80 |
Correct |
53 ms |
16208 KB |
Output is correct |
81 |
Correct |
50 ms |
16208 KB |
Output is correct |
82 |
Correct |
49 ms |
16400 KB |
Output is correct |
83 |
Correct |
48 ms |
16212 KB |
Output is correct |
84 |
Correct |
53 ms |
16212 KB |
Output is correct |
85 |
Correct |
156 ms |
32848 KB |
Output is correct |
86 |
Correct |
406 ms |
69712 KB |
Output is correct |
87 |
Correct |
411 ms |
69712 KB |
Output is correct |
88 |
Correct |
396 ms |
69716 KB |
Output is correct |
89 |
Correct |
398 ms |
69684 KB |
Output is correct |
90 |
Correct |
400 ms |
69716 KB |
Output is correct |
91 |
Correct |
228 ms |
69460 KB |
Output is correct |
92 |
Correct |
218 ms |
69456 KB |
Output is correct |
93 |
Correct |
395 ms |
69460 KB |
Output is correct |
94 |
Correct |
383 ms |
69460 KB |
Output is correct |
95 |
Correct |
440 ms |
69572 KB |
Output is correct |
96 |
Correct |
453 ms |
69456 KB |
Output is correct |
97 |
Correct |
406 ms |
69712 KB |
Output is correct |