#include <bits/stdc++.h>
using namespace std;
#define dbg(x) "[" #x " = " << (x) << "]"
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true;
return false;
}
vector<int> z_function(const string& s){
int n = (int)s.size();
vector<int> z(n);
for(int i = 1, l = 0, r = 0; i < n; ++i){
z[i] = max(0, min(r - i, z[i - l]));
while(i + z[i] < n && s[i + z[i]] == s[z[i]]) ++z[i];
if(i + z[i] > r){
l = i; r = i + z[i];
}
}
return z;
}
const long long inf = 1e18;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif // LOCAL
int N; long long A, B, C;
string S;
cin >> N >> S >> A >> B >> C;
vector<vector<int>> next(N + 1, vector<int>(N + 1, N));
for(int i = N - 1; i >= 0; --i){
vector<int> z = z_function(S.substr(i, N - i));
// for(int j : z) cout << j << ' '; cout << '\n';
int mx = 0;
for(int j = i + 1; j < N; ++j){
int extend = min(j - i, z[j - i]);
while(mx < extend){
++mx;
next[i][i + mx] = j;
}
}
}
// for(int i = 0; i < N; ++i){
// for(int j = i + 1; j <= N; ++j){
// if(next[i][j] != N){
// cout << dbg(i) << dbg(j) << dbg(next[i][j]) << '\n';
// }
// }
// }
vector<vector<long long>> dp(N + 1, vector<long long>(N + 1, inf));
for(int i = 0; i < N; ++i) dp[i][i + 1] = A + B;
for(int len = 1; len <= N; ++len){
for(int i = 0, j = len; j <= N; ++i, ++j){
minimize(dp[i][j], dp[i][j - 1] + A);
minimize(dp[i][j], dp[i + 1][j] + A);
int len = j - i;
int pos = next[i][j];
int cnt_paste = 1;
while(pos < N){
// assert(pos - i - cnt_paste * len >= 0);
minimize(dp[i][pos + len], dp[i][j] + (1 + cnt_paste) * C + (pos - i - cnt_paste * len) * A + B);
pos = next[pos][pos + len];
++cnt_paste;
}
}
}
cout << dp[0][N] - B << '\n';
return 0;
}
/*
next[l][r] = minimum k that s[l...r) = s[k...k + (r - l))
dp[l][r] = minimum cost to make X = "", Y = s[l...r)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
138 ms |
39844 KB |
Output is correct |
4 |
Correct |
165 ms |
46412 KB |
Output is correct |
5 |
Correct |
204 ms |
54360 KB |
Output is correct |
6 |
Correct |
208 ms |
63300 KB |
Output is correct |
7 |
Correct |
268 ms |
74108 KB |
Output is correct |
8 |
Correct |
281 ms |
73964 KB |
Output is correct |
9 |
Correct |
287 ms |
74068 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
508 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
504 KB |
Output is correct |
23 |
Correct |
1 ms |
548 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
580 KB |
Output is correct |
39 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
504 KB |
Output is correct |
23 |
Correct |
1 ms |
548 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
580 KB |
Output is correct |
39 |
Correct |
1 ms |
352 KB |
Output is correct |
40 |
Correct |
1 ms |
352 KB |
Output is correct |
41 |
Correct |
2 ms |
864 KB |
Output is correct |
42 |
Correct |
2 ms |
864 KB |
Output is correct |
43 |
Correct |
2 ms |
864 KB |
Output is correct |
44 |
Correct |
2 ms |
864 KB |
Output is correct |
45 |
Correct |
2 ms |
864 KB |
Output is correct |
46 |
Correct |
1 ms |
1020 KB |
Output is correct |
47 |
Correct |
2 ms |
864 KB |
Output is correct |
48 |
Correct |
2 ms |
992 KB |
Output is correct |
49 |
Correct |
2 ms |
864 KB |
Output is correct |
50 |
Correct |
2 ms |
864 KB |
Output is correct |
51 |
Correct |
2 ms |
728 KB |
Output is correct |
52 |
Correct |
2 ms |
864 KB |
Output is correct |
53 |
Correct |
2 ms |
864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
504 KB |
Output is correct |
23 |
Correct |
1 ms |
548 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
580 KB |
Output is correct |
39 |
Correct |
1 ms |
352 KB |
Output is correct |
40 |
Correct |
1 ms |
352 KB |
Output is correct |
41 |
Correct |
2 ms |
864 KB |
Output is correct |
42 |
Correct |
2 ms |
864 KB |
Output is correct |
43 |
Correct |
2 ms |
864 KB |
Output is correct |
44 |
Correct |
2 ms |
864 KB |
Output is correct |
45 |
Correct |
2 ms |
864 KB |
Output is correct |
46 |
Correct |
1 ms |
1020 KB |
Output is correct |
47 |
Correct |
2 ms |
864 KB |
Output is correct |
48 |
Correct |
2 ms |
992 KB |
Output is correct |
49 |
Correct |
2 ms |
864 KB |
Output is correct |
50 |
Correct |
2 ms |
864 KB |
Output is correct |
51 |
Correct |
2 ms |
728 KB |
Output is correct |
52 |
Correct |
2 ms |
864 KB |
Output is correct |
53 |
Correct |
2 ms |
864 KB |
Output is correct |
54 |
Correct |
5 ms |
2812 KB |
Output is correct |
55 |
Correct |
35 ms |
12128 KB |
Output is correct |
56 |
Correct |
25 ms |
12476 KB |
Output is correct |
57 |
Correct |
19 ms |
12288 KB |
Output is correct |
58 |
Correct |
17 ms |
12128 KB |
Output is correct |
59 |
Correct |
17 ms |
12336 KB |
Output is correct |
60 |
Correct |
15 ms |
12348 KB |
Output is correct |
61 |
Correct |
19 ms |
12384 KB |
Output is correct |
62 |
Correct |
30 ms |
12340 KB |
Output is correct |
63 |
Correct |
15 ms |
12116 KB |
Output is correct |
64 |
Correct |
16 ms |
12116 KB |
Output is correct |
65 |
Correct |
27 ms |
12116 KB |
Output is correct |
66 |
Correct |
27 ms |
12116 KB |
Output is correct |
67 |
Correct |
15 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
138 ms |
39844 KB |
Output is correct |
14 |
Correct |
165 ms |
46412 KB |
Output is correct |
15 |
Correct |
204 ms |
54360 KB |
Output is correct |
16 |
Correct |
208 ms |
63300 KB |
Output is correct |
17 |
Correct |
268 ms |
74108 KB |
Output is correct |
18 |
Correct |
281 ms |
73964 KB |
Output is correct |
19 |
Correct |
287 ms |
74068 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
508 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
504 KB |
Output is correct |
40 |
Correct |
1 ms |
548 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
352 KB |
Output is correct |
45 |
Correct |
1 ms |
600 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
580 KB |
Output is correct |
56 |
Correct |
1 ms |
352 KB |
Output is correct |
57 |
Correct |
1 ms |
352 KB |
Output is correct |
58 |
Correct |
2 ms |
864 KB |
Output is correct |
59 |
Correct |
2 ms |
864 KB |
Output is correct |
60 |
Correct |
2 ms |
864 KB |
Output is correct |
61 |
Correct |
2 ms |
864 KB |
Output is correct |
62 |
Correct |
2 ms |
864 KB |
Output is correct |
63 |
Correct |
1 ms |
1020 KB |
Output is correct |
64 |
Correct |
2 ms |
864 KB |
Output is correct |
65 |
Correct |
2 ms |
992 KB |
Output is correct |
66 |
Correct |
2 ms |
864 KB |
Output is correct |
67 |
Correct |
2 ms |
864 KB |
Output is correct |
68 |
Correct |
2 ms |
728 KB |
Output is correct |
69 |
Correct |
2 ms |
864 KB |
Output is correct |
70 |
Correct |
2 ms |
864 KB |
Output is correct |
71 |
Correct |
5 ms |
2812 KB |
Output is correct |
72 |
Correct |
35 ms |
12128 KB |
Output is correct |
73 |
Correct |
25 ms |
12476 KB |
Output is correct |
74 |
Correct |
19 ms |
12288 KB |
Output is correct |
75 |
Correct |
17 ms |
12128 KB |
Output is correct |
76 |
Correct |
17 ms |
12336 KB |
Output is correct |
77 |
Correct |
15 ms |
12348 KB |
Output is correct |
78 |
Correct |
19 ms |
12384 KB |
Output is correct |
79 |
Correct |
30 ms |
12340 KB |
Output is correct |
80 |
Correct |
15 ms |
12116 KB |
Output is correct |
81 |
Correct |
16 ms |
12116 KB |
Output is correct |
82 |
Correct |
27 ms |
12116 KB |
Output is correct |
83 |
Correct |
27 ms |
12116 KB |
Output is correct |
84 |
Correct |
15 ms |
12116 KB |
Output is correct |
85 |
Correct |
53 ms |
29780 KB |
Output is correct |
86 |
Correct |
152 ms |
73956 KB |
Output is correct |
87 |
Correct |
129 ms |
73968 KB |
Output is correct |
88 |
Correct |
121 ms |
74004 KB |
Output is correct |
89 |
Correct |
123 ms |
73960 KB |
Output is correct |
90 |
Correct |
109 ms |
74012 KB |
Output is correct |
91 |
Correct |
211 ms |
73920 KB |
Output is correct |
92 |
Correct |
257 ms |
73972 KB |
Output is correct |
93 |
Correct |
131 ms |
74060 KB |
Output is correct |
94 |
Correct |
144 ms |
74056 KB |
Output is correct |
95 |
Correct |
274 ms |
73968 KB |
Output is correct |
96 |
Correct |
267 ms |
74068 KB |
Output is correct |
97 |
Correct |
95 ms |
74048 KB |
Output is correct |