#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back
const ll INF = 9e18;
const ll C = 1e9;
const int P = 29, MOD = 998244353;
const int P2 = 37, MOD2 = 998244341;
int prv[2505][2505], hsh[2505][2505], hsh2[2505][2505];
unordered_map<ll, int> mp[2505];
ll res[2505][2505], dp[2505][2505];
int cal(int l, int i, int r) {
int rt = 1, len = r - i;
while (prv[i][r] >= l)
++rt, i = prv[i][r], r = i + len;
return rt;
}
ll comb(int l, int r) {
return hsh[l][r] * C + hsh2[l][r];
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, a, b, c;
string s;
cin >> n >> s >> a >> b >> c;
for (int r = 0; r < n; ++r) {
for (int i = r; i >= 0; --i) {
hsh[i][r] = ((ll)hsh[i + 1][r] * P + s[i] - 'a') % MOD;
hsh2[i][r] = ((ll)hsh2[i + 1][r] * P2 + s[i] - 'a') % MOD2;
prv[i][r] = -1;
if (2 * i >= r + 1) {
mp[r - i][comb(2 * i - r - 1, i - 1)] = 2 * i - r - 1;
if (mp[r - i].find(comb(i, r)) != mp[r - i].end()) {
prv[i][r] = mp[r - i][comb(i, r)];
}
}
}
}
for (int r = 0; r < n; ++r) {
for (int l = r; l >= 0; --l)
dp[l][r] = INF;
for (int l = r; l >= 0; --l) {
dp[l][r] = min(dp[l][r], (ll)(r - l + 1) * a + b + c);
if (l < r)
dp[l][r] = min(dp[l][r], dp[l + 1][r] + a);
res[l][r] = min(res[l][r - 1] + a, dp[l][r]);
ll t = 1;
for (int x = l, y = r; prv[x][y] >= 0; ) {
++t;
x = prv[x][y], y = x + r - l;
dp[x][r] = min(dp[x][r], res[l][r] + b + t * c + (ll)(r - x + 1 - t * (r - l + 1)) * a);
}
/*cerr << "dp " << l << " " << r << " = " << dp[l][r] << "\n";
cerr << "res " << l << " " << r << " = " << res[l][r] << "\n";*/
}
}
cout << res[0][n - 1] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
547 ms |
82416 KB |
Output is correct |
4 |
Correct |
651 ms |
92240 KB |
Output is correct |
5 |
Correct |
796 ms |
103408 KB |
Output is correct |
6 |
Correct |
864 ms |
115348 KB |
Output is correct |
7 |
Correct |
1118 ms |
128212 KB |
Output is correct |
8 |
Correct |
1035 ms |
128348 KB |
Output is correct |
9 |
Correct |
1055 ms |
128272 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
864 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
552 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
0 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
864 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
552 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
0 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
2260 KB |
Output is correct |
41 |
Correct |
4 ms |
5004 KB |
Output is correct |
42 |
Correct |
5 ms |
5332 KB |
Output is correct |
43 |
Correct |
5 ms |
5332 KB |
Output is correct |
44 |
Correct |
5 ms |
5332 KB |
Output is correct |
45 |
Correct |
6 ms |
5332 KB |
Output is correct |
46 |
Correct |
5 ms |
5332 KB |
Output is correct |
47 |
Correct |
5 ms |
5076 KB |
Output is correct |
48 |
Correct |
4 ms |
5204 KB |
Output is correct |
49 |
Correct |
6 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5204 KB |
Output is correct |
51 |
Correct |
5 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
5 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
864 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
552 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
0 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
2260 KB |
Output is correct |
41 |
Correct |
4 ms |
5004 KB |
Output is correct |
42 |
Correct |
5 ms |
5332 KB |
Output is correct |
43 |
Correct |
5 ms |
5332 KB |
Output is correct |
44 |
Correct |
5 ms |
5332 KB |
Output is correct |
45 |
Correct |
6 ms |
5332 KB |
Output is correct |
46 |
Correct |
5 ms |
5332 KB |
Output is correct |
47 |
Correct |
5 ms |
5076 KB |
Output is correct |
48 |
Correct |
4 ms |
5204 KB |
Output is correct |
49 |
Correct |
6 ms |
5204 KB |
Output is correct |
50 |
Correct |
4 ms |
5204 KB |
Output is correct |
51 |
Correct |
5 ms |
5076 KB |
Output is correct |
52 |
Correct |
4 ms |
5076 KB |
Output is correct |
53 |
Correct |
5 ms |
5460 KB |
Output is correct |
54 |
Correct |
18 ms |
14164 KB |
Output is correct |
55 |
Correct |
105 ms |
34240 KB |
Output is correct |
56 |
Correct |
146 ms |
44468 KB |
Output is correct |
57 |
Correct |
124 ms |
44592 KB |
Output is correct |
58 |
Correct |
131 ms |
44716 KB |
Output is correct |
59 |
Correct |
118 ms |
44840 KB |
Output is correct |
60 |
Correct |
118 ms |
44768 KB |
Output is correct |
61 |
Correct |
93 ms |
38496 KB |
Output is correct |
62 |
Correct |
89 ms |
34344 KB |
Output is correct |
63 |
Correct |
111 ms |
40652 KB |
Output is correct |
64 |
Correct |
141 ms |
40372 KB |
Output is correct |
65 |
Correct |
119 ms |
37928 KB |
Output is correct |
66 |
Correct |
118 ms |
37900 KB |
Output is correct |
67 |
Correct |
127 ms |
44748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
547 ms |
82416 KB |
Output is correct |
14 |
Correct |
651 ms |
92240 KB |
Output is correct |
15 |
Correct |
796 ms |
103408 KB |
Output is correct |
16 |
Correct |
864 ms |
115348 KB |
Output is correct |
17 |
Correct |
1118 ms |
128212 KB |
Output is correct |
18 |
Correct |
1035 ms |
128348 KB |
Output is correct |
19 |
Correct |
1055 ms |
128272 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
0 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
864 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
552 KB |
Output is correct |
35 |
Correct |
0 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
980 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
1 ms |
980 KB |
Output is correct |
42 |
Correct |
1 ms |
980 KB |
Output is correct |
43 |
Correct |
1 ms |
980 KB |
Output is correct |
44 |
Correct |
1 ms |
980 KB |
Output is correct |
45 |
Correct |
1 ms |
980 KB |
Output is correct |
46 |
Correct |
1 ms |
980 KB |
Output is correct |
47 |
Correct |
1 ms |
980 KB |
Output is correct |
48 |
Correct |
1 ms |
980 KB |
Output is correct |
49 |
Correct |
1 ms |
980 KB |
Output is correct |
50 |
Correct |
1 ms |
980 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
0 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
596 KB |
Output is correct |
55 |
Correct |
1 ms |
724 KB |
Output is correct |
56 |
Correct |
1 ms |
980 KB |
Output is correct |
57 |
Correct |
2 ms |
2260 KB |
Output is correct |
58 |
Correct |
4 ms |
5004 KB |
Output is correct |
59 |
Correct |
5 ms |
5332 KB |
Output is correct |
60 |
Correct |
5 ms |
5332 KB |
Output is correct |
61 |
Correct |
5 ms |
5332 KB |
Output is correct |
62 |
Correct |
6 ms |
5332 KB |
Output is correct |
63 |
Correct |
5 ms |
5332 KB |
Output is correct |
64 |
Correct |
5 ms |
5076 KB |
Output is correct |
65 |
Correct |
4 ms |
5204 KB |
Output is correct |
66 |
Correct |
6 ms |
5204 KB |
Output is correct |
67 |
Correct |
4 ms |
5204 KB |
Output is correct |
68 |
Correct |
5 ms |
5076 KB |
Output is correct |
69 |
Correct |
4 ms |
5076 KB |
Output is correct |
70 |
Correct |
5 ms |
5460 KB |
Output is correct |
71 |
Correct |
18 ms |
14164 KB |
Output is correct |
72 |
Correct |
105 ms |
34240 KB |
Output is correct |
73 |
Correct |
146 ms |
44468 KB |
Output is correct |
74 |
Correct |
124 ms |
44592 KB |
Output is correct |
75 |
Correct |
131 ms |
44716 KB |
Output is correct |
76 |
Correct |
118 ms |
44840 KB |
Output is correct |
77 |
Correct |
118 ms |
44768 KB |
Output is correct |
78 |
Correct |
93 ms |
38496 KB |
Output is correct |
79 |
Correct |
89 ms |
34344 KB |
Output is correct |
80 |
Correct |
111 ms |
40652 KB |
Output is correct |
81 |
Correct |
141 ms |
40372 KB |
Output is correct |
82 |
Correct |
119 ms |
37928 KB |
Output is correct |
83 |
Correct |
118 ms |
37900 KB |
Output is correct |
84 |
Correct |
127 ms |
44748 KB |
Output is correct |
85 |
Correct |
442 ms |
93004 KB |
Output is correct |
86 |
Correct |
1549 ms |
193520 KB |
Output is correct |
87 |
Correct |
1380 ms |
193884 KB |
Output is correct |
88 |
Correct |
1347 ms |
194280 KB |
Output is correct |
89 |
Correct |
1313 ms |
194424 KB |
Output is correct |
90 |
Correct |
1238 ms |
194492 KB |
Output is correct |
91 |
Correct |
844 ms |
131820 KB |
Output is correct |
92 |
Correct |
908 ms |
128732 KB |
Output is correct |
93 |
Correct |
1059 ms |
169740 KB |
Output is correct |
94 |
Correct |
1124 ms |
167560 KB |
Output is correct |
95 |
Correct |
1127 ms |
150388 KB |
Output is correct |
96 |
Correct |
1136 ms |
150444 KB |
Output is correct |
97 |
Correct |
1286 ms |
194484 KB |
Output is correct |