#include<bits/stdc++.h>
#define N 2510
#define INF 1000000000000000
using namespace std;
using ll = long long;
int n;
ll A, B, C;
string s;
ll dp[N][N];
int nxt[N][N];
vector<int> Z(string s) {
int n = (int)s.size();
vector<int> z(n);
for(int i = 1, l = 0, r = 1; i < n; i++) {
if(i < r) z[i] = min(z[i - l], r - i);
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;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
cin >> s;
for(int i = n - 1; i >= 0; i--) {
vector<int> z = Z(s.substr(i, n - i));
int lb = i;
for(int j = 0; j < n - i; j++) {
while(lb < n && (lb - i <= j || z[lb - i] <= j)) lb++;
nxt[i][i + j] = lb;
}
}
cin >> A >> B >> C;
for(int i = 0; i < n; i++) {
dp[i][i] = A;
for(int j = i + 1; j < n; j++)
dp[i][j] = INF;
}
for(int l = n - 1; l >= 0; l--) {
for(int r = l; r < n; r++) {
dp[l][r + 1] = min(dp[l][r + 1], dp[l][r] + A);
if(l) dp[l - 1][r] = min(dp[l - 1][r], dp[l][r] + A);
int len = r - l, h = l, cnt = 1;
while(nxt[h][h + len] != n) {
h = nxt[h][h + len];
cnt++;
dp[l][h + len] = min(dp[l][h + len], dp[l][r] + B + C * cnt + (h + len - l + 1 - (len + 1) * cnt) * A);
}
}
}
cout << dp[0][n - 1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
328 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 |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
137 ms |
34444 KB |
Output is correct |
4 |
Correct |
198 ms |
38704 KB |
Output is correct |
5 |
Correct |
241 ms |
43488 KB |
Output is correct |
6 |
Correct |
287 ms |
48576 KB |
Output is correct |
7 |
Correct |
332 ms |
54092 KB |
Output is correct |
8 |
Correct |
344 ms |
54280 KB |
Output is correct |
9 |
Correct |
336 ms |
54144 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 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 |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
328 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 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 |
456 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 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 |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
328 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 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 |
456 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 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 |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
2 ms |
2132 KB |
Output is correct |
42 |
Correct |
2 ms |
2188 KB |
Output is correct |
43 |
Correct |
1 ms |
2128 KB |
Output is correct |
44 |
Correct |
2 ms |
2132 KB |
Output is correct |
45 |
Correct |
1 ms |
2132 KB |
Output is correct |
46 |
Correct |
1 ms |
2132 KB |
Output is correct |
47 |
Correct |
2 ms |
2132 KB |
Output is correct |
48 |
Correct |
1 ms |
2132 KB |
Output is correct |
49 |
Correct |
2 ms |
2132 KB |
Output is correct |
50 |
Correct |
1 ms |
2132 KB |
Output is correct |
51 |
Correct |
2 ms |
2132 KB |
Output is correct |
52 |
Correct |
1 ms |
2132 KB |
Output is correct |
53 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
328 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 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 |
456 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 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 |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
2 ms |
2132 KB |
Output is correct |
42 |
Correct |
2 ms |
2188 KB |
Output is correct |
43 |
Correct |
1 ms |
2128 KB |
Output is correct |
44 |
Correct |
2 ms |
2132 KB |
Output is correct |
45 |
Correct |
1 ms |
2132 KB |
Output is correct |
46 |
Correct |
1 ms |
2132 KB |
Output is correct |
47 |
Correct |
2 ms |
2132 KB |
Output is correct |
48 |
Correct |
1 ms |
2132 KB |
Output is correct |
49 |
Correct |
2 ms |
2132 KB |
Output is correct |
50 |
Correct |
1 ms |
2132 KB |
Output is correct |
51 |
Correct |
2 ms |
2132 KB |
Output is correct |
52 |
Correct |
1 ms |
2132 KB |
Output is correct |
53 |
Correct |
1 ms |
2132 KB |
Output is correct |
54 |
Correct |
4 ms |
5064 KB |
Output is correct |
55 |
Correct |
30 ms |
14272 KB |
Output is correct |
56 |
Correct |
15 ms |
14284 KB |
Output is correct |
57 |
Correct |
13 ms |
14176 KB |
Output is correct |
58 |
Correct |
12 ms |
14284 KB |
Output is correct |
59 |
Correct |
11 ms |
14292 KB |
Output is correct |
60 |
Correct |
10 ms |
14228 KB |
Output is correct |
61 |
Correct |
10 ms |
14292 KB |
Output is correct |
62 |
Correct |
22 ms |
14288 KB |
Output is correct |
63 |
Correct |
10 ms |
14284 KB |
Output is correct |
64 |
Correct |
11 ms |
14292 KB |
Output is correct |
65 |
Correct |
16 ms |
14300 KB |
Output is correct |
66 |
Correct |
18 ms |
14248 KB |
Output is correct |
67 |
Correct |
10 ms |
14288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
328 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
137 ms |
34444 KB |
Output is correct |
14 |
Correct |
198 ms |
38704 KB |
Output is correct |
15 |
Correct |
241 ms |
43488 KB |
Output is correct |
16 |
Correct |
287 ms |
48576 KB |
Output is correct |
17 |
Correct |
332 ms |
54092 KB |
Output is correct |
18 |
Correct |
344 ms |
54280 KB |
Output is correct |
19 |
Correct |
336 ms |
54144 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 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 |
456 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
0 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
0 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
980 KB |
Output is correct |
58 |
Correct |
2 ms |
2132 KB |
Output is correct |
59 |
Correct |
2 ms |
2188 KB |
Output is correct |
60 |
Correct |
1 ms |
2128 KB |
Output is correct |
61 |
Correct |
2 ms |
2132 KB |
Output is correct |
62 |
Correct |
1 ms |
2132 KB |
Output is correct |
63 |
Correct |
1 ms |
2132 KB |
Output is correct |
64 |
Correct |
2 ms |
2132 KB |
Output is correct |
65 |
Correct |
1 ms |
2132 KB |
Output is correct |
66 |
Correct |
2 ms |
2132 KB |
Output is correct |
67 |
Correct |
1 ms |
2132 KB |
Output is correct |
68 |
Correct |
2 ms |
2132 KB |
Output is correct |
69 |
Correct |
1 ms |
2132 KB |
Output is correct |
70 |
Correct |
1 ms |
2132 KB |
Output is correct |
71 |
Correct |
4 ms |
5064 KB |
Output is correct |
72 |
Correct |
30 ms |
14272 KB |
Output is correct |
73 |
Correct |
15 ms |
14284 KB |
Output is correct |
74 |
Correct |
13 ms |
14176 KB |
Output is correct |
75 |
Correct |
12 ms |
14284 KB |
Output is correct |
76 |
Correct |
11 ms |
14292 KB |
Output is correct |
77 |
Correct |
10 ms |
14228 KB |
Output is correct |
78 |
Correct |
10 ms |
14292 KB |
Output is correct |
79 |
Correct |
22 ms |
14288 KB |
Output is correct |
80 |
Correct |
10 ms |
14284 KB |
Output is correct |
81 |
Correct |
11 ms |
14292 KB |
Output is correct |
82 |
Correct |
16 ms |
14300 KB |
Output is correct |
83 |
Correct |
18 ms |
14248 KB |
Output is correct |
84 |
Correct |
10 ms |
14288 KB |
Output is correct |
85 |
Correct |
38 ms |
27700 KB |
Output is correct |
86 |
Correct |
87 ms |
54136 KB |
Output is correct |
87 |
Correct |
73 ms |
54128 KB |
Output is correct |
88 |
Correct |
58 ms |
54024 KB |
Output is correct |
89 |
Correct |
51 ms |
54080 KB |
Output is correct |
90 |
Correct |
49 ms |
54120 KB |
Output is correct |
91 |
Correct |
82 ms |
54144 KB |
Output is correct |
92 |
Correct |
127 ms |
54092 KB |
Output is correct |
93 |
Correct |
62 ms |
54104 KB |
Output is correct |
94 |
Correct |
56 ms |
54036 KB |
Output is correct |
95 |
Correct |
96 ms |
54068 KB |
Output is correct |
96 |
Correct |
101 ms |
54264 KB |
Output is correct |
97 |
Correct |
48 ms |
54100 KB |
Output is correct |