#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[31][31][31][31];
bool pre[31][31][31][31];
string s; ll a, b, c, n;
const ll inf = 1e18;
ll ans (int l1, int r1, int l2, int r2) {
ll &ret = dp[l1][r1][l2][r2];
if (ret != -1) return ret;
if (l1 == 1 && r1 == n) return ret = 0;
ret = inf;
if (l1 && r1 != n) ret = min(ret, a + ans(l1, r1 + 1, l2, r2));
if (!l1) {
for (int i = 1; i <= n; i++) ret = min(ret, a + ans(i, i, l2, r2));
}
if (l1) ret = min(ret, b + ans(0, 0, l1, r1));
if (l1 && l2 && r1 + (r2 - l2 + 1) <= n && pre[r1 + 1][r1 + r2 - l2 + 1][l2][r2]) {
ret = min(ret, c + ans(l1, r1 + r2 - l2 + 1, l2, r2));
}
if (!l1 && l2) {
for (int i = 1; i + (r2 - l2) <= n; i++) {
if (pre[i][i + r2 - l2][l2][r2]) ret = min(ret, c + ans(i, i + r2 - l2, l2, r2));
}
}
return ret;
}
ll dist[2501][2501];
using matr = array <ll, 3>;
priority_queue <matr, vector <matr>, greater <matr>> pq;
void addedge (ll a, ll b, ll c) {
if (a < dist[b][c]) {
dist[b][c] = a;
pq.push({a, b, c});
}
}
int main () {
memset(dp, -1, sizeof(dp));
cin >> n >> s >> a >> b >> c;
bool flag = 1;
s.insert(s.begin(), '0');
for (int i = 1; i <= n; i++) flag &= s[i] == 'a';
if (flag) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
dist[i][j] = inf;
}
}
dist[0][0] = 0;
pq.push({0, 0});
while (!pq.empty()) {
auto k = pq.top();
pq.pop();
if (k[0] > dist[k[1]][k[2]]) continue;
if (k[1] != n) addedge(k[0] + a, k[1] + 1, k[2]);
addedge(k[0] + b, 0, k[1]);
if (k[1] + k[2] <= n) addedge(k[0] + c, k[1] + k[2], k[2]);
}
ll mn = inf;
for (int i = 0; i <= n; i++) mn = min(mn, dist[n][i]);
cout << (mn == inf ? -1 : mn) << '\n';
return 0;
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
for (int l = 1; l <= n; l++) {
for (int m = l; m <= n; m++) {
if (m - l != j - i) continue;
pre[i][j][l][m] = 1;
for (int t = i; t <= j; t++) {
pre[i][j][l][m] &= s[t] == s[l + t - i];
}
}
}
}
}
cout << ans(0, 0, 0, 0) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10072 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
2 ms |
10076 KB |
Output is correct |
4 |
Correct |
2 ms |
10076 KB |
Output is correct |
5 |
Correct |
2 ms |
10072 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12124 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
614 ms |
59832 KB |
Output is correct |
4 |
Correct |
708 ms |
56512 KB |
Output is correct |
5 |
Correct |
936 ms |
104152 KB |
Output is correct |
6 |
Correct |
744 ms |
55896 KB |
Output is correct |
7 |
Correct |
1330 ms |
156072 KB |
Output is correct |
8 |
Correct |
893 ms |
57960 KB |
Output is correct |
9 |
Correct |
725 ms |
58196 KB |
Output is correct |
10 |
Correct |
2 ms |
10072 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
2 ms |
10156 KB |
Output is correct |
13 |
Correct |
2 ms |
10072 KB |
Output is correct |
14 |
Correct |
2 ms |
10072 KB |
Output is correct |
15 |
Correct |
2 ms |
12124 KB |
Output is correct |
16 |
Correct |
2 ms |
12124 KB |
Output is correct |
17 |
Correct |
2 ms |
10072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10072 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
2 ms |
10076 KB |
Output is correct |
4 |
Correct |
2 ms |
10076 KB |
Output is correct |
5 |
Correct |
2 ms |
10072 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10072 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
2 ms |
12124 KB |
Output is correct |
13 |
Correct |
3 ms |
10076 KB |
Output is correct |
14 |
Correct |
2 ms |
10076 KB |
Output is correct |
15 |
Correct |
2 ms |
10076 KB |
Output is correct |
16 |
Correct |
2 ms |
10076 KB |
Output is correct |
17 |
Correct |
2 ms |
10076 KB |
Output is correct |
18 |
Correct |
2 ms |
10076 KB |
Output is correct |
19 |
Correct |
2 ms |
10076 KB |
Output is correct |
20 |
Correct |
2 ms |
10076 KB |
Output is correct |
21 |
Correct |
5 ms |
10588 KB |
Output is correct |
22 |
Correct |
5 ms |
10588 KB |
Output is correct |
23 |
Correct |
5 ms |
10668 KB |
Output is correct |
24 |
Correct |
5 ms |
10588 KB |
Output is correct |
25 |
Correct |
6 ms |
10588 KB |
Output is correct |
26 |
Correct |
5 ms |
10588 KB |
Output is correct |
27 |
Correct |
5 ms |
10508 KB |
Output is correct |
28 |
Correct |
5 ms |
10424 KB |
Output is correct |
29 |
Correct |
6 ms |
10588 KB |
Output is correct |
30 |
Correct |
5 ms |
10588 KB |
Output is correct |
31 |
Correct |
5 ms |
10588 KB |
Output is correct |
32 |
Correct |
5 ms |
10588 KB |
Output is correct |
33 |
Correct |
5 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10072 KB |
Output is correct |
35 |
Correct |
2 ms |
10076 KB |
Output is correct |
36 |
Correct |
2 ms |
10076 KB |
Output is correct |
37 |
Correct |
2 ms |
10076 KB |
Output is correct |
38 |
Correct |
2 ms |
12124 KB |
Output is correct |
39 |
Correct |
2 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10072 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
2 ms |
10076 KB |
Output is correct |
4 |
Correct |
2 ms |
10076 KB |
Output is correct |
5 |
Correct |
2 ms |
10072 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10072 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
2 ms |
12124 KB |
Output is correct |
13 |
Correct |
3 ms |
10076 KB |
Output is correct |
14 |
Correct |
2 ms |
10076 KB |
Output is correct |
15 |
Correct |
2 ms |
10076 KB |
Output is correct |
16 |
Correct |
2 ms |
10076 KB |
Output is correct |
17 |
Correct |
2 ms |
10076 KB |
Output is correct |
18 |
Correct |
2 ms |
10076 KB |
Output is correct |
19 |
Correct |
2 ms |
10076 KB |
Output is correct |
20 |
Correct |
2 ms |
10076 KB |
Output is correct |
21 |
Correct |
5 ms |
10588 KB |
Output is correct |
22 |
Correct |
5 ms |
10588 KB |
Output is correct |
23 |
Correct |
5 ms |
10668 KB |
Output is correct |
24 |
Correct |
5 ms |
10588 KB |
Output is correct |
25 |
Correct |
6 ms |
10588 KB |
Output is correct |
26 |
Correct |
5 ms |
10588 KB |
Output is correct |
27 |
Correct |
5 ms |
10508 KB |
Output is correct |
28 |
Correct |
5 ms |
10424 KB |
Output is correct |
29 |
Correct |
6 ms |
10588 KB |
Output is correct |
30 |
Correct |
5 ms |
10588 KB |
Output is correct |
31 |
Correct |
5 ms |
10588 KB |
Output is correct |
32 |
Correct |
5 ms |
10588 KB |
Output is correct |
33 |
Correct |
5 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10072 KB |
Output is correct |
35 |
Correct |
2 ms |
10076 KB |
Output is correct |
36 |
Correct |
2 ms |
10076 KB |
Output is correct |
37 |
Correct |
2 ms |
10076 KB |
Output is correct |
38 |
Correct |
2 ms |
12124 KB |
Output is correct |
39 |
Correct |
2 ms |
12124 KB |
Output is correct |
40 |
Incorrect |
13 ms |
10156 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10072 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
2 ms |
10076 KB |
Output is correct |
4 |
Correct |
2 ms |
10076 KB |
Output is correct |
5 |
Correct |
2 ms |
10072 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10072 KB |
Output is correct |
11 |
Correct |
2 ms |
10076 KB |
Output is correct |
12 |
Correct |
2 ms |
12124 KB |
Output is correct |
13 |
Correct |
3 ms |
10076 KB |
Output is correct |
14 |
Correct |
2 ms |
10076 KB |
Output is correct |
15 |
Correct |
2 ms |
10076 KB |
Output is correct |
16 |
Correct |
2 ms |
10076 KB |
Output is correct |
17 |
Correct |
2 ms |
10076 KB |
Output is correct |
18 |
Correct |
2 ms |
10076 KB |
Output is correct |
19 |
Correct |
2 ms |
10076 KB |
Output is correct |
20 |
Correct |
2 ms |
10076 KB |
Output is correct |
21 |
Correct |
5 ms |
10588 KB |
Output is correct |
22 |
Correct |
5 ms |
10588 KB |
Output is correct |
23 |
Correct |
5 ms |
10668 KB |
Output is correct |
24 |
Correct |
5 ms |
10588 KB |
Output is correct |
25 |
Correct |
6 ms |
10588 KB |
Output is correct |
26 |
Correct |
5 ms |
10588 KB |
Output is correct |
27 |
Correct |
5 ms |
10508 KB |
Output is correct |
28 |
Correct |
5 ms |
10424 KB |
Output is correct |
29 |
Correct |
6 ms |
10588 KB |
Output is correct |
30 |
Correct |
5 ms |
10588 KB |
Output is correct |
31 |
Correct |
5 ms |
10588 KB |
Output is correct |
32 |
Correct |
5 ms |
10588 KB |
Output is correct |
33 |
Correct |
5 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10072 KB |
Output is correct |
35 |
Correct |
2 ms |
10076 KB |
Output is correct |
36 |
Correct |
2 ms |
10076 KB |
Output is correct |
37 |
Correct |
2 ms |
10076 KB |
Output is correct |
38 |
Correct |
2 ms |
12124 KB |
Output is correct |
39 |
Correct |
2 ms |
12124 KB |
Output is correct |
40 |
Incorrect |
13 ms |
10156 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10072 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
2 ms |
10076 KB |
Output is correct |
4 |
Correct |
2 ms |
10076 KB |
Output is correct |
5 |
Correct |
2 ms |
10072 KB |
Output is correct |
6 |
Correct |
2 ms |
10076 KB |
Output is correct |
7 |
Correct |
2 ms |
10076 KB |
Output is correct |
8 |
Correct |
2 ms |
10076 KB |
Output is correct |
9 |
Correct |
2 ms |
10076 KB |
Output is correct |
10 |
Correct |
2 ms |
10072 KB |
Output is correct |
11 |
Correct |
2 ms |
12124 KB |
Output is correct |
12 |
Correct |
2 ms |
10076 KB |
Output is correct |
13 |
Correct |
614 ms |
59832 KB |
Output is correct |
14 |
Correct |
708 ms |
56512 KB |
Output is correct |
15 |
Correct |
936 ms |
104152 KB |
Output is correct |
16 |
Correct |
744 ms |
55896 KB |
Output is correct |
17 |
Correct |
1330 ms |
156072 KB |
Output is correct |
18 |
Correct |
893 ms |
57960 KB |
Output is correct |
19 |
Correct |
725 ms |
58196 KB |
Output is correct |
20 |
Correct |
2 ms |
10072 KB |
Output is correct |
21 |
Correct |
2 ms |
10076 KB |
Output is correct |
22 |
Correct |
2 ms |
10156 KB |
Output is correct |
23 |
Correct |
2 ms |
10072 KB |
Output is correct |
24 |
Correct |
2 ms |
10072 KB |
Output is correct |
25 |
Correct |
2 ms |
12124 KB |
Output is correct |
26 |
Correct |
2 ms |
12124 KB |
Output is correct |
27 |
Correct |
2 ms |
10072 KB |
Output is correct |
28 |
Correct |
2 ms |
10076 KB |
Output is correct |
29 |
Correct |
2 ms |
12124 KB |
Output is correct |
30 |
Correct |
3 ms |
10076 KB |
Output is correct |
31 |
Correct |
2 ms |
10076 KB |
Output is correct |
32 |
Correct |
2 ms |
10076 KB |
Output is correct |
33 |
Correct |
2 ms |
10076 KB |
Output is correct |
34 |
Correct |
2 ms |
10076 KB |
Output is correct |
35 |
Correct |
2 ms |
10076 KB |
Output is correct |
36 |
Correct |
2 ms |
10076 KB |
Output is correct |
37 |
Correct |
2 ms |
10076 KB |
Output is correct |
38 |
Correct |
5 ms |
10588 KB |
Output is correct |
39 |
Correct |
5 ms |
10588 KB |
Output is correct |
40 |
Correct |
5 ms |
10668 KB |
Output is correct |
41 |
Correct |
5 ms |
10588 KB |
Output is correct |
42 |
Correct |
6 ms |
10588 KB |
Output is correct |
43 |
Correct |
5 ms |
10588 KB |
Output is correct |
44 |
Correct |
5 ms |
10508 KB |
Output is correct |
45 |
Correct |
5 ms |
10424 KB |
Output is correct |
46 |
Correct |
6 ms |
10588 KB |
Output is correct |
47 |
Correct |
5 ms |
10588 KB |
Output is correct |
48 |
Correct |
5 ms |
10588 KB |
Output is correct |
49 |
Correct |
5 ms |
10588 KB |
Output is correct |
50 |
Correct |
5 ms |
10588 KB |
Output is correct |
51 |
Correct |
2 ms |
10072 KB |
Output is correct |
52 |
Correct |
2 ms |
10076 KB |
Output is correct |
53 |
Correct |
2 ms |
10076 KB |
Output is correct |
54 |
Correct |
2 ms |
10076 KB |
Output is correct |
55 |
Correct |
2 ms |
12124 KB |
Output is correct |
56 |
Correct |
2 ms |
12124 KB |
Output is correct |
57 |
Incorrect |
13 ms |
10156 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |