#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 2500 + 10;
const int INF = 1e9;
int n;
char s[MAXN];
llong a, b, c;
llong dp[MAXN][MAXN];
bool bl[MAXN][MAXN];
llong f(int l, int r)
{
// std::cout << "call: " << l << ' ' << r << '\n' << std::flush;
if (l == r)
{
return a;
}
if (bl[l][r])
{
return dp[l][r];
}
bl[l][r] = true;
dp[l][r] = std::min(f(l + 1, r) + a, f(l, r - 1) + a);
int kmpF[MAXN];
kmpF[l] = 0;
for (int prefix = l ; prefix < r ; ++prefix)
{
if (prefix != l)
{
kmpF[prefix] = kmpF[prefix - 1];
while (kmpF[prefix] > 0 && s[l + kmpF[prefix]] != s[prefix])
{
// std::cout << "here22: " << prefix << ' ' << kmpF[prefix] << '\n';
kmpF[prefix] = kmpF[l + kmpF[prefix] - 1];
}
if (s[l + kmpF[prefix]] == s[prefix])
{
kmpF[prefix]++;
// std::cout << "incr: " << prefix << ' ' << kmpF[prefix] << '\n';
}
}
int ptr = 0;
llong cnt = 1;
llong cost = f(l, prefix) + b;
for (int j = prefix + 1 ; j <= r ; ++j)
{
if (s[j] == s[l + ptr])
{
ptr++;
if (ptr == prefix - l + 1)
{
cnt++;
ptr = 0;
}
} else
{
while (ptr > 0 && s[l + ptr] != s[j])
{
ptr = kmpF[l + ptr - 1];
}
if (s[j] == s[l + ptr])
{
ptr++;
}
}
}
dp[l][r] = std::min(dp[l][r], cost + cnt * c + (r - l + 1) * a - (prefix - l + 1) * cnt * a);
}
return dp[l][r];
}
void solve()
{
std::cout << f(1, n) << '\n';
}
void input()
{
std::cin >> n;
std::cin >> s + 1;
std::cin >> a >> b >> c;
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
Compilation message
copypaste3.cpp: In function 'void input()':
copypaste3.cpp:94:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | std::cin >> s + 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Execution timed out |
3076 ms |
32320 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4752 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4696 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4952 KB |
Output is correct |
28 |
Correct |
1 ms |
4696 KB |
Output is correct |
29 |
Correct |
1 ms |
4952 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
1 ms |
4700 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4700 KB |
Output is correct |
39 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4752 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4696 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4952 KB |
Output is correct |
28 |
Correct |
1 ms |
4696 KB |
Output is correct |
29 |
Correct |
1 ms |
4952 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
1 ms |
4700 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4700 KB |
Output is correct |
39 |
Correct |
1 ms |
4700 KB |
Output is correct |
40 |
Correct |
9 ms |
5468 KB |
Output is correct |
41 |
Correct |
82 ms |
9036 KB |
Output is correct |
42 |
Correct |
193 ms |
9032 KB |
Output is correct |
43 |
Correct |
182 ms |
8916 KB |
Output is correct |
44 |
Correct |
149 ms |
8952 KB |
Output is correct |
45 |
Correct |
119 ms |
8948 KB |
Output is correct |
46 |
Correct |
107 ms |
9052 KB |
Output is correct |
47 |
Correct |
119 ms |
9296 KB |
Output is correct |
48 |
Correct |
114 ms |
8940 KB |
Output is correct |
49 |
Correct |
122 ms |
9200 KB |
Output is correct |
50 |
Correct |
132 ms |
8972 KB |
Output is correct |
51 |
Correct |
139 ms |
9040 KB |
Output is correct |
52 |
Correct |
143 ms |
9040 KB |
Output is correct |
53 |
Correct |
111 ms |
8972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4752 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4696 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4952 KB |
Output is correct |
28 |
Correct |
1 ms |
4696 KB |
Output is correct |
29 |
Correct |
1 ms |
4952 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
1 ms |
4700 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4700 KB |
Output is correct |
39 |
Correct |
1 ms |
4700 KB |
Output is correct |
40 |
Correct |
9 ms |
5468 KB |
Output is correct |
41 |
Correct |
82 ms |
9036 KB |
Output is correct |
42 |
Correct |
193 ms |
9032 KB |
Output is correct |
43 |
Correct |
182 ms |
8916 KB |
Output is correct |
44 |
Correct |
149 ms |
8952 KB |
Output is correct |
45 |
Correct |
119 ms |
8948 KB |
Output is correct |
46 |
Correct |
107 ms |
9052 KB |
Output is correct |
47 |
Correct |
119 ms |
9296 KB |
Output is correct |
48 |
Correct |
114 ms |
8940 KB |
Output is correct |
49 |
Correct |
122 ms |
9200 KB |
Output is correct |
50 |
Correct |
132 ms |
8972 KB |
Output is correct |
51 |
Correct |
139 ms |
9040 KB |
Output is correct |
52 |
Correct |
143 ms |
9040 KB |
Output is correct |
53 |
Correct |
111 ms |
8972 KB |
Output is correct |
54 |
Execution timed out |
3100 ms |
16460 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Execution timed out |
3076 ms |
32320 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |