#include <iostream>
#include <vector>
#include <array>
#include <map>
#define ll long long
using namespace std;
string S;
const ll M = 1e9 + 7, M2 = 1e9 + 11;
map <array<ll, 2>, ll> mp;
array <ll, 2> X[2510][2510];
ll n, a, b, c, s, s2, base = 37, base2 = 41, p, p2, dp[2510][2500], nx[2510][2510];
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n >> S >> a >> b >> c;
for (ll i=0; i<n; ++i) {
s = s2 = 0, p = p2 = 1;
for (ll j=i; j<n; ++j) {
(s += (S[j]-'a'+1) * p % M) %= M;
(p *= base) %= M;
(s2 += (S[j]-'a'+1) * p2 % M2) %= M2;
(p2 *= base2) %= M2;
X[i][j] = {s, s2};
}
}
for (ll len=0; len<n; ++len) {
mp.clear();
for (ll i=n-len-1; i>=0; --i) {
if (i+2*len+1 < n) {
mp[X[i+len+1][i+2*len+1]] = i+len+1;
}
if (mp.count(X[i][i+len])) nx[i][i+len] = mp[X[i][i+len]];
else nx[i][i+len] = -1;
}
for (ll i=0; i+len<n; ++i) {
if (len) {
dp[i][i+len] = min(dp[i][i+len], min(dp[i][i+len-1], dp[i+1][i+len]));
}
s = 0;
ll j = i;
while (true) {
ll k = nx[j][j+len];
++s;
if (k == -1) break;
dp[i][k+len] = min(dp[i][k+len], dp[i][i+len] + b + (s+1) * c - (s * (len+1) * a));
j = k;
}
}
}
cout << a*n + dp[0][n-1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
106 ms |
131636 KB |
Output is correct |
4 |
Correct |
124 ms |
142676 KB |
Output is correct |
5 |
Correct |
145 ms |
156072 KB |
Output is correct |
6 |
Correct |
196 ms |
170832 KB |
Output is correct |
7 |
Correct |
247 ms |
180304 KB |
Output is correct |
8 |
Correct |
243 ms |
180216 KB |
Output is correct |
9 |
Correct |
236 ms |
180504 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
0 ms |
2432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6616 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
1 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6748 KB |
Output is correct |
29 |
Correct |
1 ms |
6748 KB |
Output is correct |
30 |
Correct |
1 ms |
6748 KB |
Output is correct |
31 |
Correct |
1 ms |
6748 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
1 ms |
6744 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6616 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
1 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6748 KB |
Output is correct |
29 |
Correct |
1 ms |
6748 KB |
Output is correct |
30 |
Correct |
1 ms |
6748 KB |
Output is correct |
31 |
Correct |
1 ms |
6748 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
1 ms |
6744 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
6748 KB |
Output is correct |
40 |
Correct |
1 ms |
9052 KB |
Output is correct |
41 |
Correct |
2 ms |
15708 KB |
Output is correct |
42 |
Correct |
3 ms |
15708 KB |
Output is correct |
43 |
Correct |
4 ms |
15708 KB |
Output is correct |
44 |
Correct |
4 ms |
15708 KB |
Output is correct |
45 |
Correct |
3 ms |
15708 KB |
Output is correct |
46 |
Correct |
4 ms |
15708 KB |
Output is correct |
47 |
Correct |
3 ms |
15708 KB |
Output is correct |
48 |
Correct |
3 ms |
15708 KB |
Output is correct |
49 |
Correct |
3 ms |
15708 KB |
Output is correct |
50 |
Correct |
3 ms |
15708 KB |
Output is correct |
51 |
Correct |
3 ms |
15708 KB |
Output is correct |
52 |
Correct |
3 ms |
15704 KB |
Output is correct |
53 |
Correct |
4 ms |
15708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6616 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
1 ms |
6748 KB |
Output is correct |
24 |
Correct |
1 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
1 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6748 KB |
Output is correct |
29 |
Correct |
1 ms |
6748 KB |
Output is correct |
30 |
Correct |
1 ms |
6748 KB |
Output is correct |
31 |
Correct |
1 ms |
6748 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
1 ms |
6744 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
6748 KB |
Output is correct |
40 |
Correct |
1 ms |
9052 KB |
Output is correct |
41 |
Correct |
2 ms |
15708 KB |
Output is correct |
42 |
Correct |
3 ms |
15708 KB |
Output is correct |
43 |
Correct |
4 ms |
15708 KB |
Output is correct |
44 |
Correct |
4 ms |
15708 KB |
Output is correct |
45 |
Correct |
3 ms |
15708 KB |
Output is correct |
46 |
Correct |
4 ms |
15708 KB |
Output is correct |
47 |
Correct |
3 ms |
15708 KB |
Output is correct |
48 |
Correct |
3 ms |
15708 KB |
Output is correct |
49 |
Correct |
3 ms |
15708 KB |
Output is correct |
50 |
Correct |
3 ms |
15708 KB |
Output is correct |
51 |
Correct |
3 ms |
15708 KB |
Output is correct |
52 |
Correct |
3 ms |
15704 KB |
Output is correct |
53 |
Correct |
4 ms |
15708 KB |
Output is correct |
54 |
Correct |
13 ms |
33884 KB |
Output is correct |
55 |
Correct |
37 ms |
71992 KB |
Output is correct |
56 |
Correct |
56 ms |
72028 KB |
Output is correct |
57 |
Correct |
59 ms |
72076 KB |
Output is correct |
58 |
Correct |
55 ms |
72024 KB |
Output is correct |
59 |
Correct |
56 ms |
72020 KB |
Output is correct |
60 |
Correct |
58 ms |
72024 KB |
Output is correct |
61 |
Correct |
53 ms |
72016 KB |
Output is correct |
62 |
Correct |
33 ms |
72040 KB |
Output is correct |
63 |
Correct |
58 ms |
72020 KB |
Output is correct |
64 |
Correct |
55 ms |
71904 KB |
Output is correct |
65 |
Correct |
55 ms |
72040 KB |
Output is correct |
66 |
Correct |
51 ms |
72040 KB |
Output is correct |
67 |
Correct |
56 ms |
72016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
106 ms |
131636 KB |
Output is correct |
14 |
Correct |
124 ms |
142676 KB |
Output is correct |
15 |
Correct |
145 ms |
156072 KB |
Output is correct |
16 |
Correct |
196 ms |
170832 KB |
Output is correct |
17 |
Correct |
247 ms |
180304 KB |
Output is correct |
18 |
Correct |
243 ms |
180216 KB |
Output is correct |
19 |
Correct |
236 ms |
180504 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
0 ms |
2392 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Correct |
0 ms |
2432 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
6492 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
6616 KB |
Output is correct |
39 |
Correct |
1 ms |
6748 KB |
Output is correct |
40 |
Correct |
1 ms |
6748 KB |
Output is correct |
41 |
Correct |
1 ms |
6748 KB |
Output is correct |
42 |
Correct |
1 ms |
6744 KB |
Output is correct |
43 |
Correct |
1 ms |
6748 KB |
Output is correct |
44 |
Correct |
1 ms |
6748 KB |
Output is correct |
45 |
Correct |
1 ms |
6748 KB |
Output is correct |
46 |
Correct |
1 ms |
6748 KB |
Output is correct |
47 |
Correct |
1 ms |
6748 KB |
Output is correct |
48 |
Correct |
1 ms |
6748 KB |
Output is correct |
49 |
Correct |
1 ms |
6748 KB |
Output is correct |
50 |
Correct |
1 ms |
6744 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
6492 KB |
Output is correct |
56 |
Correct |
1 ms |
6748 KB |
Output is correct |
57 |
Correct |
1 ms |
9052 KB |
Output is correct |
58 |
Correct |
2 ms |
15708 KB |
Output is correct |
59 |
Correct |
3 ms |
15708 KB |
Output is correct |
60 |
Correct |
4 ms |
15708 KB |
Output is correct |
61 |
Correct |
4 ms |
15708 KB |
Output is correct |
62 |
Correct |
3 ms |
15708 KB |
Output is correct |
63 |
Correct |
4 ms |
15708 KB |
Output is correct |
64 |
Correct |
3 ms |
15708 KB |
Output is correct |
65 |
Correct |
3 ms |
15708 KB |
Output is correct |
66 |
Correct |
3 ms |
15708 KB |
Output is correct |
67 |
Correct |
3 ms |
15708 KB |
Output is correct |
68 |
Correct |
3 ms |
15708 KB |
Output is correct |
69 |
Correct |
3 ms |
15704 KB |
Output is correct |
70 |
Correct |
4 ms |
15708 KB |
Output is correct |
71 |
Correct |
13 ms |
33884 KB |
Output is correct |
72 |
Correct |
37 ms |
71992 KB |
Output is correct |
73 |
Correct |
56 ms |
72028 KB |
Output is correct |
74 |
Correct |
59 ms |
72076 KB |
Output is correct |
75 |
Correct |
55 ms |
72024 KB |
Output is correct |
76 |
Correct |
56 ms |
72020 KB |
Output is correct |
77 |
Correct |
58 ms |
72024 KB |
Output is correct |
78 |
Correct |
53 ms |
72016 KB |
Output is correct |
79 |
Correct |
33 ms |
72040 KB |
Output is correct |
80 |
Correct |
58 ms |
72020 KB |
Output is correct |
81 |
Correct |
55 ms |
71904 KB |
Output is correct |
82 |
Correct |
55 ms |
72040 KB |
Output is correct |
83 |
Correct |
51 ms |
72040 KB |
Output is correct |
84 |
Correct |
56 ms |
72016 KB |
Output is correct |
85 |
Correct |
145 ms |
115224 KB |
Output is correct |
86 |
Correct |
392 ms |
181072 KB |
Output is correct |
87 |
Correct |
383 ms |
181076 KB |
Output is correct |
88 |
Correct |
377 ms |
181328 KB |
Output is correct |
89 |
Correct |
391 ms |
181540 KB |
Output is correct |
90 |
Correct |
384 ms |
181076 KB |
Output is correct |
91 |
Correct |
243 ms |
181016 KB |
Output is correct |
92 |
Correct |
229 ms |
181004 KB |
Output is correct |
93 |
Correct |
408 ms |
181072 KB |
Output is correct |
94 |
Correct |
395 ms |
181076 KB |
Output is correct |
95 |
Correct |
393 ms |
181204 KB |
Output is correct |
96 |
Correct |
384 ms |
181076 KB |
Output is correct |
97 |
Correct |
382 ms |
181020 KB |
Output is correct |