#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2500;
int z[2 * MAX_N + 2], nextPos[MAX_N + 1][MAX_N + 1];
long long dp[MAX_N + 1][MAX_N + 1];
int main() {
int n, a, b, c;
string s;
cin >> n >> s >> a >> b >> c;
s = "#" + s;
for ( int l = 1; l <= n; l++ ) {
for ( int r = l; r <= n; r++ )
dp[l][r] = (long long)a * (r - l + 1) + b;
}
string t = s;
for ( int l = n; l >= 1; l-- ) {
t = s[l] + t;
int ls = 0, rs = 0;
for ( int i = 1; i < t.size(); i++ )
z[i] = 0;
for ( int i = 1; i < t.size(); i++ ){
if ( i < rs )
z[i] = min( z[i - ls], rs - i );
while( i + z[i] < t.size() && t[z[i]] == t[i + z[i]] )
z[i]++;
if ( i + z[i] > rs ) {
ls = i;
rs = i + z[i];
}
}
for ( int r = l; r <= n; r++ ) {
int i = n - l + r + 2;
while ( i < t.size() && z[i] < r - l + 1 )
i++;
nextPos[l][r] = i - (n - l + 1);
}
}
for ( int len = 1; len <= n; len++ ) {
for ( int l = 1; l <= n - len + 1; l++ ) {
int r = l + len - 1;
dp[l - 1][r] = min( dp[l - 1][r], dp[l][r] + a );
dp[l][r + 1] = min( dp[l][r + 1], dp[l][r] + a );
long long cost = dp[l][r] + b + c;
int k = l;
while ( nextPos[k][k + len - 1] <= n ) {
cost += (long long)a * (nextPos[k][k + len - 1] - (k + len)) + c;
k = nextPos[k][k + len - 1];
dp[l][k + len - 1] = min( dp[l][k + len - 1], cost );
}
}
}
cout << dp[1][n] - b << "\n";
return 0;
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:27:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for ( int i = 1; i < t.size(); i++ )
| ~~^~~~~~~~~~
copypaste3.cpp:29:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for ( int i = 1; i < t.size(); i++ ){
| ~~^~~~~~~~~~
copypaste3.cpp:32:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | while( i + z[i] < t.size() && t[z[i]] == t[i + z[i]] )
| ~~~~~~~~~^~~~~~~~~~
copypaste3.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | while ( i < t.size() && z[i] < r - l + 1 )
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
211 ms |
57940 KB |
Output is correct |
4 |
Correct |
268 ms |
62036 KB |
Output is correct |
5 |
Correct |
327 ms |
66128 KB |
Output is correct |
6 |
Correct |
392 ms |
70228 KB |
Output is correct |
7 |
Correct |
490 ms |
73024 KB |
Output is correct |
8 |
Correct |
498 ms |
73260 KB |
Output is correct |
9 |
Correct |
485 ms |
73044 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4440 KB |
Output is correct |
39 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4440 KB |
Output is correct |
39 |
Correct |
1 ms |
4440 KB |
Output is correct |
40 |
Correct |
1 ms |
6488 KB |
Output is correct |
41 |
Correct |
2 ms |
8540 KB |
Output is correct |
42 |
Correct |
2 ms |
8540 KB |
Output is correct |
43 |
Correct |
2 ms |
8540 KB |
Output is correct |
44 |
Correct |
2 ms |
8540 KB |
Output is correct |
45 |
Correct |
2 ms |
8540 KB |
Output is correct |
46 |
Correct |
2 ms |
8540 KB |
Output is correct |
47 |
Correct |
2 ms |
8540 KB |
Output is correct |
48 |
Correct |
2 ms |
8540 KB |
Output is correct |
49 |
Correct |
2 ms |
8792 KB |
Output is correct |
50 |
Correct |
2 ms |
8540 KB |
Output is correct |
51 |
Correct |
2 ms |
8540 KB |
Output is correct |
52 |
Correct |
2 ms |
8540 KB |
Output is correct |
53 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4440 KB |
Output is correct |
39 |
Correct |
1 ms |
4440 KB |
Output is correct |
40 |
Correct |
1 ms |
6488 KB |
Output is correct |
41 |
Correct |
2 ms |
8540 KB |
Output is correct |
42 |
Correct |
2 ms |
8540 KB |
Output is correct |
43 |
Correct |
2 ms |
8540 KB |
Output is correct |
44 |
Correct |
2 ms |
8540 KB |
Output is correct |
45 |
Correct |
2 ms |
8540 KB |
Output is correct |
46 |
Correct |
2 ms |
8540 KB |
Output is correct |
47 |
Correct |
2 ms |
8540 KB |
Output is correct |
48 |
Correct |
2 ms |
8540 KB |
Output is correct |
49 |
Correct |
2 ms |
8792 KB |
Output is correct |
50 |
Correct |
2 ms |
8540 KB |
Output is correct |
51 |
Correct |
2 ms |
8540 KB |
Output is correct |
52 |
Correct |
2 ms |
8540 KB |
Output is correct |
53 |
Correct |
2 ms |
8540 KB |
Output is correct |
54 |
Correct |
10 ms |
16732 KB |
Output is correct |
55 |
Correct |
45 ms |
33364 KB |
Output is correct |
56 |
Correct |
71 ms |
33360 KB |
Output is correct |
57 |
Correct |
77 ms |
33364 KB |
Output is correct |
58 |
Correct |
70 ms |
33356 KB |
Output is correct |
59 |
Correct |
64 ms |
33360 KB |
Output is correct |
60 |
Correct |
64 ms |
33372 KB |
Output is correct |
61 |
Correct |
41 ms |
33360 KB |
Output is correct |
62 |
Correct |
40 ms |
33368 KB |
Output is correct |
63 |
Correct |
48 ms |
33364 KB |
Output is correct |
64 |
Correct |
49 ms |
33364 KB |
Output is correct |
65 |
Correct |
42 ms |
33360 KB |
Output is correct |
66 |
Correct |
42 ms |
33356 KB |
Output is correct |
67 |
Correct |
65 ms |
33364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
2508 KB |
Output is correct |
13 |
Correct |
211 ms |
57940 KB |
Output is correct |
14 |
Correct |
268 ms |
62036 KB |
Output is correct |
15 |
Correct |
327 ms |
66128 KB |
Output is correct |
16 |
Correct |
392 ms |
70228 KB |
Output is correct |
17 |
Correct |
490 ms |
73024 KB |
Output is correct |
18 |
Correct |
498 ms |
73260 KB |
Output is correct |
19 |
Correct |
485 ms |
73044 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
2392 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
4440 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4440 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
1 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4444 KB |
Output is correct |
45 |
Correct |
1 ms |
4444 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
0 ms |
2396 KB |
Output is correct |
52 |
Correct |
0 ms |
2396 KB |
Output is correct |
53 |
Correct |
0 ms |
2396 KB |
Output is correct |
54 |
Correct |
0 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
4440 KB |
Output is correct |
56 |
Correct |
1 ms |
4440 KB |
Output is correct |
57 |
Correct |
1 ms |
6488 KB |
Output is correct |
58 |
Correct |
2 ms |
8540 KB |
Output is correct |
59 |
Correct |
2 ms |
8540 KB |
Output is correct |
60 |
Correct |
2 ms |
8540 KB |
Output is correct |
61 |
Correct |
2 ms |
8540 KB |
Output is correct |
62 |
Correct |
2 ms |
8540 KB |
Output is correct |
63 |
Correct |
2 ms |
8540 KB |
Output is correct |
64 |
Correct |
2 ms |
8540 KB |
Output is correct |
65 |
Correct |
2 ms |
8540 KB |
Output is correct |
66 |
Correct |
2 ms |
8792 KB |
Output is correct |
67 |
Correct |
2 ms |
8540 KB |
Output is correct |
68 |
Correct |
2 ms |
8540 KB |
Output is correct |
69 |
Correct |
2 ms |
8540 KB |
Output is correct |
70 |
Correct |
2 ms |
8540 KB |
Output is correct |
71 |
Correct |
10 ms |
16732 KB |
Output is correct |
72 |
Correct |
45 ms |
33364 KB |
Output is correct |
73 |
Correct |
71 ms |
33360 KB |
Output is correct |
74 |
Correct |
77 ms |
33364 KB |
Output is correct |
75 |
Correct |
70 ms |
33356 KB |
Output is correct |
76 |
Correct |
64 ms |
33360 KB |
Output is correct |
77 |
Correct |
64 ms |
33372 KB |
Output is correct |
78 |
Correct |
41 ms |
33360 KB |
Output is correct |
79 |
Correct |
40 ms |
33368 KB |
Output is correct |
80 |
Correct |
48 ms |
33364 KB |
Output is correct |
81 |
Correct |
49 ms |
33364 KB |
Output is correct |
82 |
Correct |
42 ms |
33360 KB |
Output is correct |
83 |
Correct |
42 ms |
33356 KB |
Output is correct |
84 |
Correct |
65 ms |
33364 KB |
Output is correct |
85 |
Correct |
267 ms |
51872 KB |
Output is correct |
86 |
Correct |
937 ms |
73292 KB |
Output is correct |
87 |
Correct |
912 ms |
73288 KB |
Output is correct |
88 |
Correct |
926 ms |
73288 KB |
Output is correct |
89 |
Correct |
881 ms |
73300 KB |
Output is correct |
90 |
Correct |
879 ms |
73300 KB |
Output is correct |
91 |
Correct |
374 ms |
73040 KB |
Output is correct |
92 |
Correct |
417 ms |
73300 KB |
Output is correct |
93 |
Correct |
598 ms |
73044 KB |
Output is correct |
94 |
Correct |
638 ms |
73060 KB |
Output is correct |
95 |
Correct |
508 ms |
73540 KB |
Output is correct |
96 |
Correct |
515 ms |
73060 KB |
Output is correct |
97 |
Correct |
876 ms |
73056 KB |
Output is correct |