/**
_ _ __ _ _ _ _ _ _
|a ||t ||o d | |o |
| __ _| | _ | __| _ |
| __ |/_ | __ /__\ / _\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 2500;
int N;
string S;
int A, B, C;
vector <int> v[N_MAX + 2];
int Z[N_MAX + 2];
ll minCost[N_MAX + 2][N_MAX + 2];
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> S >> A >> B >> C;
S = " " + S;
for (int l = 1; l <= N; l++) {
for (int r = l; r <= N; r++) {
minCost[l][r] = (ll) A * (r - l + 1);
}
}
for (int l = N; l >= 1; l--) {
Z[l] = 0;
int pos = l;
for (int i = l + 1; i <= N; i++) {
if (i <= pos + Z[pos] - 1) {
Z[i] = min(pos + Z[pos] - i, Z[l + i - pos]);
} else {
Z[i] = 0;
}
while (i + Z[i] <= N && S[i + Z[i]] == S[l + Z[i]]) {
Z[i]++;
}
if (i + Z[i] > pos + Z[pos]) {
pos = i;
}
}
set <int> s;
for (int i = l + 1; i <= N; i++) {
if (Z[i] > 0) {
s.insert(i);
v[Z[i]].push_back(i);
}
}
for (int r = l; r <= N; r++) {
int len = r - l + 1;
if (len > 1) {
for (int i : v[len - 1]) {
s.erase(i);
}
minCost[l][r] = min({minCost[l][r], minCost[l + 1][r] + A, minCost[l][r - 1] + A});
}
ll cost = minCost[l][r] + B;
int i = l;
while (true) {
cost += C;
minCost[l][i + len - 1] = min(minCost[l][i + len - 1], cost);
set <int> :: iterator it = s.lower_bound(i + len);
if (it != s.end()) {
cost += (ll) ((*it - i) - len) * A;
i = *it;
} else {
break;
}
}
}
for (int len = 1; len <= N - l; len++) {
v[len].clear();
}
}
cout << minCost[1][N] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
673 ms |
37644 KB |
Output is correct |
4 |
Correct |
810 ms |
39820 KB |
Output is correct |
5 |
Correct |
967 ms |
44072 KB |
Output is correct |
6 |
Correct |
1145 ms |
46136 KB |
Output is correct |
7 |
Correct |
1359 ms |
48556 KB |
Output is correct |
8 |
Correct |
1400 ms |
48220 KB |
Output is correct |
9 |
Correct |
1372 ms |
48508 KB |
Output is correct |
10 |
Correct |
1 ms |
516 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
352 KB |
Output is correct |
36 |
Correct |
0 ms |
352 KB |
Output is correct |
37 |
Correct |
0 ms |
352 KB |
Output is correct |
38 |
Correct |
1 ms |
2400 KB |
Output is correct |
39 |
Correct |
1 ms |
2400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
352 KB |
Output is correct |
36 |
Correct |
0 ms |
352 KB |
Output is correct |
37 |
Correct |
0 ms |
352 KB |
Output is correct |
38 |
Correct |
1 ms |
2400 KB |
Output is correct |
39 |
Correct |
1 ms |
2400 KB |
Output is correct |
40 |
Correct |
1 ms |
2400 KB |
Output is correct |
41 |
Correct |
4 ms |
4692 KB |
Output is correct |
42 |
Correct |
2 ms |
4452 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4448 KB |
Output is correct |
45 |
Correct |
1 ms |
4448 KB |
Output is correct |
46 |
Correct |
1 ms |
4500 KB |
Output is correct |
47 |
Correct |
1 ms |
4452 KB |
Output is correct |
48 |
Correct |
1 ms |
4448 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4628 KB |
Output is correct |
51 |
Correct |
2 ms |
4444 KB |
Output is correct |
52 |
Correct |
2 ms |
4448 KB |
Output is correct |
53 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
352 KB |
Output is correct |
36 |
Correct |
0 ms |
352 KB |
Output is correct |
37 |
Correct |
0 ms |
352 KB |
Output is correct |
38 |
Correct |
1 ms |
2400 KB |
Output is correct |
39 |
Correct |
1 ms |
2400 KB |
Output is correct |
40 |
Correct |
1 ms |
2400 KB |
Output is correct |
41 |
Correct |
4 ms |
4692 KB |
Output is correct |
42 |
Correct |
2 ms |
4452 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4448 KB |
Output is correct |
45 |
Correct |
1 ms |
4448 KB |
Output is correct |
46 |
Correct |
1 ms |
4500 KB |
Output is correct |
47 |
Correct |
1 ms |
4452 KB |
Output is correct |
48 |
Correct |
1 ms |
4448 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4628 KB |
Output is correct |
51 |
Correct |
2 ms |
4444 KB |
Output is correct |
52 |
Correct |
2 ms |
4448 KB |
Output is correct |
53 |
Correct |
1 ms |
4444 KB |
Output is correct |
54 |
Correct |
10 ms |
10844 KB |
Output is correct |
55 |
Correct |
166 ms |
21200 KB |
Output is correct |
56 |
Correct |
53 ms |
21084 KB |
Output is correct |
57 |
Correct |
37 ms |
21084 KB |
Output is correct |
58 |
Correct |
18 ms |
21156 KB |
Output is correct |
59 |
Correct |
12 ms |
21084 KB |
Output is correct |
60 |
Correct |
10 ms |
21004 KB |
Output is correct |
61 |
Correct |
12 ms |
21000 KB |
Output is correct |
62 |
Correct |
64 ms |
21416 KB |
Output is correct |
63 |
Correct |
12 ms |
21080 KB |
Output is correct |
64 |
Correct |
13 ms |
21084 KB |
Output is correct |
65 |
Correct |
26 ms |
21180 KB |
Output is correct |
66 |
Correct |
24 ms |
21084 KB |
Output is correct |
67 |
Correct |
12 ms |
21080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
673 ms |
37644 KB |
Output is correct |
14 |
Correct |
810 ms |
39820 KB |
Output is correct |
15 |
Correct |
967 ms |
44072 KB |
Output is correct |
16 |
Correct |
1145 ms |
46136 KB |
Output is correct |
17 |
Correct |
1359 ms |
48556 KB |
Output is correct |
18 |
Correct |
1400 ms |
48220 KB |
Output is correct |
19 |
Correct |
1372 ms |
48508 KB |
Output is correct |
20 |
Correct |
1 ms |
516 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2392 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
352 KB |
Output is correct |
53 |
Correct |
0 ms |
352 KB |
Output is correct |
54 |
Correct |
0 ms |
352 KB |
Output is correct |
55 |
Correct |
1 ms |
2400 KB |
Output is correct |
56 |
Correct |
1 ms |
2400 KB |
Output is correct |
57 |
Correct |
1 ms |
2400 KB |
Output is correct |
58 |
Correct |
4 ms |
4692 KB |
Output is correct |
59 |
Correct |
2 ms |
4452 KB |
Output is correct |
60 |
Correct |
2 ms |
4444 KB |
Output is correct |
61 |
Correct |
1 ms |
4448 KB |
Output is correct |
62 |
Correct |
1 ms |
4448 KB |
Output is correct |
63 |
Correct |
1 ms |
4500 KB |
Output is correct |
64 |
Correct |
1 ms |
4452 KB |
Output is correct |
65 |
Correct |
1 ms |
4448 KB |
Output is correct |
66 |
Correct |
1 ms |
4444 KB |
Output is correct |
67 |
Correct |
1 ms |
4628 KB |
Output is correct |
68 |
Correct |
2 ms |
4444 KB |
Output is correct |
69 |
Correct |
2 ms |
4448 KB |
Output is correct |
70 |
Correct |
1 ms |
4444 KB |
Output is correct |
71 |
Correct |
10 ms |
10844 KB |
Output is correct |
72 |
Correct |
166 ms |
21200 KB |
Output is correct |
73 |
Correct |
53 ms |
21084 KB |
Output is correct |
74 |
Correct |
37 ms |
21084 KB |
Output is correct |
75 |
Correct |
18 ms |
21156 KB |
Output is correct |
76 |
Correct |
12 ms |
21084 KB |
Output is correct |
77 |
Correct |
10 ms |
21004 KB |
Output is correct |
78 |
Correct |
12 ms |
21000 KB |
Output is correct |
79 |
Correct |
64 ms |
21416 KB |
Output is correct |
80 |
Correct |
12 ms |
21080 KB |
Output is correct |
81 |
Correct |
13 ms |
21084 KB |
Output is correct |
82 |
Correct |
26 ms |
21180 KB |
Output is correct |
83 |
Correct |
24 ms |
21084 KB |
Output is correct |
84 |
Correct |
12 ms |
21080 KB |
Output is correct |
85 |
Correct |
135 ms |
33372 KB |
Output is correct |
86 |
Correct |
361 ms |
48252 KB |
Output is correct |
87 |
Correct |
218 ms |
48248 KB |
Output is correct |
88 |
Correct |
108 ms |
48240 KB |
Output is correct |
89 |
Correct |
69 ms |
48216 KB |
Output is correct |
90 |
Correct |
55 ms |
48228 KB |
Output is correct |
91 |
Correct |
124 ms |
48252 KB |
Output is correct |
92 |
Correct |
222 ms |
48284 KB |
Output is correct |
93 |
Correct |
67 ms |
48512 KB |
Output is correct |
94 |
Correct |
74 ms |
48216 KB |
Output is correct |
95 |
Correct |
195 ms |
48300 KB |
Output is correct |
96 |
Correct |
198 ms |
48332 KB |
Output is correct |
97 |
Correct |
60 ms |
48220 KB |
Output is correct |