// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define sz(x) int(x.size())
using pi = pair<int, int>;
using ll = long long;
template<class T> using V = vector<T>;
using vi = V<int>;
using vl = V<ll>;
using vpi = V<pi>;
using str = string;
const int MOD = 1e9 + 9;
const ll b1 = 2003;
const ll b2 = 1009;
const ll INFL = ll(4e18);
const int LG = 18;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin >> N;
str S; cin >> S;
ll a, b, c; cin >> a >> b >> c;
V<vl> dp(N, vl(N, INFL));
V<vi> nxt(N, vi(N, -1));
for(int l = 0; l < N; l++) for(int r = l; r < N; r++) dp[l][r] = (r - l + 1) * a;
unordered_map<ll, int> C;
V<V<vi>> H(N);
for(int l = 0; l < N; l++) {
ll h1 = 0, h2 = 0;
for(int w = 0; l + w < N; w++) {
h1 = (b1 * h1 + S[l + w]) % MOD;
h2 = (b2 * h2 + S[l + w]) % MOD;
// cout << l << " " << w << " " << h << endl;
ll h = MOD * h1 + h2;
if (C.find(h) == C.end()) C[h] = sz(H[w]), H[w].pb({});
H[w][C[h]].pb(l);
}
}
for(int w = 0; w < N; w++) {
// cout << w << endl;
for(auto& v : H[w]) {
// for(auto& x : v) cout << x << " ";
// cout << endl;
v.pb(-1);
int j = sz(v) - 1;
for(int i = sz(v) - 2; i >= 0; i--) {
while(j - 1 >= 0 && (v[i] + w) < v[j - 1]) j--;
nxt[v[i]][v[i] + w] = v[j];
}
}
// cout << endl;
}
// for(int l = 0; l < N; l++) for(int r = l; r < N; r++) {
// if (nxt[l][r] >= MOD) nxt[l][r] = -1;
// // cout << l << " " << r << " " << nxt[l][r] << endl;
// }
for(int w = 0; w < N; w++) {
for(int l = 0; l + w < N; l++) {
int r = l + w;
if (r-1 >= 0) dp[l][r] = min(dp[l][r], dp[l][r-1] + a);
if (l+1 < N) dp[l][r] = min(dp[l][r], dp[l+1][r] + a);
// cout << l << " " << r << " " << dp[l][r] << endl;
int L = l; ll amt = 1;
while(nxt[L][L + w] != -1) {
L = nxt[L][L + w], amt++;
ll left = (L + w - l + 1) - (l + w - l + 1) * amt;
dp[l][L + w] = min(dp[l][L + w], dp[l][r] + b + c * amt + left * a);
// cout << l << " " << l + w << " " << L << " " << L + w << " => " << dp[l][L + w] << endl;
}
}
}
cout << dp[0][N-1] << nl;
exit(0-0);
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
236 ms |
49236 KB |
Output is correct |
4 |
Correct |
306 ms |
57196 KB |
Output is correct |
5 |
Correct |
385 ms |
66892 KB |
Output is correct |
6 |
Correct |
488 ms |
78520 KB |
Output is correct |
7 |
Correct |
563 ms |
91732 KB |
Output is correct |
8 |
Correct |
569 ms |
91932 KB |
Output is correct |
9 |
Correct |
560 ms |
91984 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
408 KB |
Output is correct |
23 |
Correct |
0 ms |
344 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 |
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 |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
408 KB |
Output is correct |
23 |
Correct |
0 ms |
344 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 |
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 |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
5 ms |
2904 KB |
Output is correct |
43 |
Correct |
5 ms |
2904 KB |
Output is correct |
44 |
Correct |
4 ms |
2904 KB |
Output is correct |
45 |
Correct |
4 ms |
2904 KB |
Output is correct |
46 |
Correct |
5 ms |
2908 KB |
Output is correct |
47 |
Correct |
3 ms |
1372 KB |
Output is correct |
48 |
Correct |
4 ms |
2136 KB |
Output is correct |
49 |
Correct |
4 ms |
2396 KB |
Output is correct |
50 |
Correct |
4 ms |
2536 KB |
Output is correct |
51 |
Correct |
4 ms |
2140 KB |
Output is correct |
52 |
Correct |
4 ms |
2140 KB |
Output is correct |
53 |
Correct |
4 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
408 KB |
Output is correct |
23 |
Correct |
0 ms |
344 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 |
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 |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
5 ms |
2904 KB |
Output is correct |
43 |
Correct |
5 ms |
2904 KB |
Output is correct |
44 |
Correct |
4 ms |
2904 KB |
Output is correct |
45 |
Correct |
4 ms |
2904 KB |
Output is correct |
46 |
Correct |
5 ms |
2908 KB |
Output is correct |
47 |
Correct |
3 ms |
1372 KB |
Output is correct |
48 |
Correct |
4 ms |
2136 KB |
Output is correct |
49 |
Correct |
4 ms |
2396 KB |
Output is correct |
50 |
Correct |
4 ms |
2536 KB |
Output is correct |
51 |
Correct |
4 ms |
2140 KB |
Output is correct |
52 |
Correct |
4 ms |
2140 KB |
Output is correct |
53 |
Correct |
4 ms |
2908 KB |
Output is correct |
54 |
Correct |
24 ms |
13284 KB |
Output is correct |
55 |
Correct |
52 ms |
15092 KB |
Output is correct |
56 |
Correct |
210 ms |
63428 KB |
Output is correct |
57 |
Correct |
202 ms |
63608 KB |
Output is correct |
58 |
Correct |
221 ms |
63960 KB |
Output is correct |
59 |
Correct |
207 ms |
63948 KB |
Output is correct |
60 |
Correct |
198 ms |
63932 KB |
Output is correct |
61 |
Correct |
116 ms |
35512 KB |
Output is correct |
62 |
Correct |
51 ms |
15452 KB |
Output is correct |
63 |
Correct |
188 ms |
54336 KB |
Output is correct |
64 |
Correct |
184 ms |
53684 KB |
Output is correct |
65 |
Correct |
124 ms |
39648 KB |
Output is correct |
66 |
Correct |
130 ms |
39788 KB |
Output is correct |
67 |
Correct |
193 ms |
63936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
236 ms |
49236 KB |
Output is correct |
14 |
Correct |
306 ms |
57196 KB |
Output is correct |
15 |
Correct |
385 ms |
66892 KB |
Output is correct |
16 |
Correct |
488 ms |
78520 KB |
Output is correct |
17 |
Correct |
563 ms |
91732 KB |
Output is correct |
18 |
Correct |
569 ms |
91932 KB |
Output is correct |
19 |
Correct |
560 ms |
91984 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 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 |
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 |
0 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 |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
600 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
408 KB |
Output is correct |
40 |
Correct |
0 ms |
344 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 |
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 |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
600 KB |
Output is correct |
50 |
Correct |
0 ms |
344 KB |
Output is correct |
51 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
55 |
Correct |
0 ms |
344 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
860 KB |
Output is correct |
58 |
Correct |
2 ms |
860 KB |
Output is correct |
59 |
Correct |
5 ms |
2904 KB |
Output is correct |
60 |
Correct |
5 ms |
2904 KB |
Output is correct |
61 |
Correct |
4 ms |
2904 KB |
Output is correct |
62 |
Correct |
4 ms |
2904 KB |
Output is correct |
63 |
Correct |
5 ms |
2908 KB |
Output is correct |
64 |
Correct |
3 ms |
1372 KB |
Output is correct |
65 |
Correct |
4 ms |
2136 KB |
Output is correct |
66 |
Correct |
4 ms |
2396 KB |
Output is correct |
67 |
Correct |
4 ms |
2536 KB |
Output is correct |
68 |
Correct |
4 ms |
2140 KB |
Output is correct |
69 |
Correct |
4 ms |
2140 KB |
Output is correct |
70 |
Correct |
4 ms |
2908 KB |
Output is correct |
71 |
Correct |
24 ms |
13284 KB |
Output is correct |
72 |
Correct |
52 ms |
15092 KB |
Output is correct |
73 |
Correct |
210 ms |
63428 KB |
Output is correct |
74 |
Correct |
202 ms |
63608 KB |
Output is correct |
75 |
Correct |
221 ms |
63960 KB |
Output is correct |
76 |
Correct |
207 ms |
63948 KB |
Output is correct |
77 |
Correct |
198 ms |
63932 KB |
Output is correct |
78 |
Correct |
116 ms |
35512 KB |
Output is correct |
79 |
Correct |
51 ms |
15452 KB |
Output is correct |
80 |
Correct |
188 ms |
54336 KB |
Output is correct |
81 |
Correct |
184 ms |
53684 KB |
Output is correct |
82 |
Correct |
124 ms |
39648 KB |
Output is correct |
83 |
Correct |
130 ms |
39788 KB |
Output is correct |
84 |
Correct |
193 ms |
63936 KB |
Output is correct |
85 |
Correct |
667 ms |
156680 KB |
Output is correct |
86 |
Correct |
2145 ms |
409216 KB |
Output is correct |
87 |
Correct |
2116 ms |
409340 KB |
Output is correct |
88 |
Correct |
2132 ms |
410764 KB |
Output is correct |
89 |
Correct |
2107 ms |
410264 KB |
Output is correct |
90 |
Correct |
2100 ms |
409888 KB |
Output is correct |
91 |
Correct |
579 ms |
109756 KB |
Output is correct |
92 |
Correct |
518 ms |
96176 KB |
Output is correct |
93 |
Correct |
1654 ms |
308400 KB |
Output is correct |
94 |
Correct |
1697 ms |
315500 KB |
Output is correct |
95 |
Correct |
1603 ms |
244352 KB |
Output is correct |
96 |
Correct |
1618 ms |
244508 KB |
Output is correct |
97 |
Correct |
2103 ms |
410848 KB |
Output is correct |