#include <bits/stdc++.h>
typedef long long ll;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, a, b, c;
std::string s;
std::cin >> n >> s >> a >> b >> c;
if (std::count(s.begin(), s.end(), 'a') == n) {
std::vector<std::vector<ll>> dp(n + 1, std::vector<ll>(n + 1));
// dp[i][j] = the minimum cost to match n - i a's if there are j a's in the
// clipboard
/*
dp[i][j] = min{
dp[i + 1][j] + a,
dp[i + j][j] + c (j != 0),
dp[0][i] + b (i > j)
}
*/
for (ll j = n - 1; j >= 0; --j) {
for (ll i = n - 1; i >= 0; --i) {
dp[i][j] = dp[i + 1][j] + a;
if (j != 0 and i + j <= n) {
dp[i][j] = std::min(dp[i][j], dp[i + j][j] + c);
}
if (i > j) {
dp[i][j] = std::min(dp[i][j], dp[0][i] + b);
}
}
}
std::cout << dp[0][0] << "\n";
return 0;
}
std::vector<std::vector<std::vector<std::vector<ll>>>> dp(
n + 1, std::vector<std::vector<std::vector<ll>>>(
n + 1, std::vector<std::vector<ll>>(
n + 1, std::vector<ll>(n + 1, 1e15))));
// dp[x_beg][x_len][y_beg][y_len] => dp[i][j][k][l] = the minimum cost to
// match if the current x is (i, j) and the current y is (k, l)
for (ll i = 0; i <= n; ++i) {
for (ll j = 0; j <= n; ++j) {
dp[0][n][i][j] = 0;
}
}
auto substring = [&](ll _i, ll len) {
std::string ans;
for (ll i = 0; i < len and i + _i < n; ++i) {
ans.push_back(s[i + _i]);
}
return ans;
};
/*
dp[i][j][k][l] = min{
dp[i][j + 1][k][l] + a,
dp[i][j + l][k][l] + c (iff the string matches),
dp[0][0][i][j] + b (if the pair (i, j) > (k, l))
}
*/
for (ll l = n - 1; l >= 0; --l) {
for (ll k = n - 1; k >= 0; --k) {
for (ll j = n - 1; j >= 0; --j) {
for (ll i = n - 1; i >= 0; --i) {
dp[i][j][k][l] = dp[i][j + 1][k][l] + a;
if (j == 0) {
for (ll m = 0; m < n; ++m) {
dp[i][j][k][l] = std::min(dp[i][j][k][l], dp[m][1][k][l] + a);
}
}
if (l != 0 and j + l <= n - i and
substring(i, j) + substring(k, l) == substring(i, j + l)) {
dp[i][j][k][l] = std::min(dp[i][j][k][l], dp[i][j + l][k][l] + c);
}
if (i > k or (i == k and j > l)) {
dp[i][j][k][l] = std::min(dp[i][j][k][l], dp[0][0][i][j] + b);
}
}
}
}
}
std::cout << dp[0][0][0][0] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
33 ms |
26460 KB |
Output is correct |
4 |
Correct |
34 ms |
31064 KB |
Output is correct |
5 |
Correct |
44 ms |
36188 KB |
Output is correct |
6 |
Correct |
57 ms |
42328 KB |
Output is correct |
7 |
Correct |
68 ms |
49492 KB |
Output is correct |
8 |
Correct |
65 ms |
49496 KB |
Output is correct |
9 |
Correct |
64 ms |
49496 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
656 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
6 ms |
1628 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
8 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
452 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
51 ms |
8796 KB |
Output is correct |
22 |
Correct |
44 ms |
8792 KB |
Output is correct |
23 |
Correct |
42 ms |
8900 KB |
Output is correct |
24 |
Correct |
54 ms |
8796 KB |
Output is correct |
25 |
Correct |
46 ms |
8792 KB |
Output is correct |
26 |
Correct |
44 ms |
8792 KB |
Output is correct |
27 |
Correct |
50 ms |
8796 KB |
Output is correct |
28 |
Correct |
51 ms |
8900 KB |
Output is correct |
29 |
Correct |
48 ms |
8796 KB |
Output is correct |
30 |
Correct |
43 ms |
8796 KB |
Output is correct |
31 |
Correct |
44 ms |
8792 KB |
Output is correct |
32 |
Correct |
48 ms |
8792 KB |
Output is correct |
33 |
Correct |
43 ms |
8796 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
656 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
6 ms |
1628 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
8 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
452 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
51 ms |
8796 KB |
Output is correct |
22 |
Correct |
44 ms |
8792 KB |
Output is correct |
23 |
Correct |
42 ms |
8900 KB |
Output is correct |
24 |
Correct |
54 ms |
8796 KB |
Output is correct |
25 |
Correct |
46 ms |
8792 KB |
Output is correct |
26 |
Correct |
44 ms |
8792 KB |
Output is correct |
27 |
Correct |
50 ms |
8796 KB |
Output is correct |
28 |
Correct |
51 ms |
8900 KB |
Output is correct |
29 |
Correct |
48 ms |
8796 KB |
Output is correct |
30 |
Correct |
43 ms |
8796 KB |
Output is correct |
31 |
Correct |
44 ms |
8792 KB |
Output is correct |
32 |
Correct |
48 ms |
8792 KB |
Output is correct |
33 |
Correct |
43 ms |
8796 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
460 KB |
Output is correct |
40 |
Execution timed out |
3086 ms |
459088 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
656 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
6 ms |
1628 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
8 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
452 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
51 ms |
8796 KB |
Output is correct |
22 |
Correct |
44 ms |
8792 KB |
Output is correct |
23 |
Correct |
42 ms |
8900 KB |
Output is correct |
24 |
Correct |
54 ms |
8796 KB |
Output is correct |
25 |
Correct |
46 ms |
8792 KB |
Output is correct |
26 |
Correct |
44 ms |
8792 KB |
Output is correct |
27 |
Correct |
50 ms |
8796 KB |
Output is correct |
28 |
Correct |
51 ms |
8900 KB |
Output is correct |
29 |
Correct |
48 ms |
8796 KB |
Output is correct |
30 |
Correct |
43 ms |
8796 KB |
Output is correct |
31 |
Correct |
44 ms |
8792 KB |
Output is correct |
32 |
Correct |
48 ms |
8792 KB |
Output is correct |
33 |
Correct |
43 ms |
8796 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
460 KB |
Output is correct |
40 |
Execution timed out |
3086 ms |
459088 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
33 ms |
26460 KB |
Output is correct |
14 |
Correct |
34 ms |
31064 KB |
Output is correct |
15 |
Correct |
44 ms |
36188 KB |
Output is correct |
16 |
Correct |
57 ms |
42328 KB |
Output is correct |
17 |
Correct |
68 ms |
49492 KB |
Output is correct |
18 |
Correct |
65 ms |
49496 KB |
Output is correct |
19 |
Correct |
64 ms |
49496 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
656 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
6 ms |
1628 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
8 ms |
1884 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
0 ms |
452 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
860 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
51 ms |
8796 KB |
Output is correct |
39 |
Correct |
44 ms |
8792 KB |
Output is correct |
40 |
Correct |
42 ms |
8900 KB |
Output is correct |
41 |
Correct |
54 ms |
8796 KB |
Output is correct |
42 |
Correct |
46 ms |
8792 KB |
Output is correct |
43 |
Correct |
44 ms |
8792 KB |
Output is correct |
44 |
Correct |
50 ms |
8796 KB |
Output is correct |
45 |
Correct |
51 ms |
8900 KB |
Output is correct |
46 |
Correct |
48 ms |
8796 KB |
Output is correct |
47 |
Correct |
43 ms |
8796 KB |
Output is correct |
48 |
Correct |
44 ms |
8792 KB |
Output is correct |
49 |
Correct |
48 ms |
8792 KB |
Output is correct |
50 |
Correct |
43 ms |
8796 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
460 KB |
Output is correct |
57 |
Execution timed out |
3086 ms |
459088 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |