// Be name khoda //
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn5 = 2e2 + 5;
const int maxnt = 8e5 + 10;
const ll inf = 1e15;
ll dp[maxn5][maxn5][maxn5], cost[maxn5][maxn5];
bool is_equa[maxn5][maxn5][maxn5];
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n, a, b, c; string s;
cin >> n >> s >> a >> b >> c;
bool alla = true;
for(int i = 0; i < n; i++)
alla &= (s[i] == 'a');
if(alla){
cost[0][0] = a;
for(int len = 2; len <= n; len++){
cost[0][len - 1] = len * a;
for(int k = 1; k <= len; k++){
cost[0][len - 1] = min(cost[0][len - 1], cost[0][k - 1] + b + c * (len / k) + (len % k) * a);
}
}
cout << cost[0][n - 1] << endl;
return 0;
}
for(int len = 2; len <= n; len++) for(int l = 0; l + len - 1 < n; l++) for(int k = 1; k * 2 <= len; k++){
int r = l + len - 1;
if(k == 1)
is_equa[l][r][k] = (s[l] == s[r]);
else
is_equa[l][r][k] = (s[l + k - 1] == s[r]) && is_equa[l][r - 1][k - 1];
}
//cout << is_equa[2][7][2] << ' ' << is_equa[2][7][3] << endl;
for(int len = 1; len <= n; len++) for(int l = 0; l + len - 1 < n; l++){
int r = l + len - 1;
for(int k = 1; k <= len; k++){
if(k == len){
dp[l][r][k] = c;
continue;
}
dp[l][r][k] = dp[l + 1][r][k] + a;
if(k * 2 <= len && is_equa[l][r][k])
dp[l][r][k] = min(dp[l][r][k], dp[l + k][r][k] + c);
}
}
for(int i = 0; i < n; i++)
cost[i][i] = a;
for(int len = 2; len <= n; len++) for(int l = 0; l + len - 1 < n; l++){
int r = l + len - 1;
cost[l][r] = min(a + cost[l + 1][r], a + cost[l][r - 1]);
//cout << "begin " << l << ' ' << r << ' '<< cost[l][r] << ' ' << a << ' ' << len << endl;
for(int k = 1; k * 2 <= len; k++) if(is_equa[l][r][k]){
cost[l][r] = min(cost[l][r], cost[l][l + k - 1] + b + c + dp[l + k][r][k]);
//cout << l << ' '<< r << ' ' << k << ' ' << cost[l][r] << endl;
}
}
cout << cost[0][n - 1] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
340 KB |
Output is correct |
4 |
Correct |
13 ms |
340 KB |
Output is correct |
5 |
Correct |
12 ms |
328 KB |
Output is correct |
6 |
Correct |
15 ms |
356 KB |
Output is correct |
7 |
Correct |
17 ms |
356 KB |
Output is correct |
8 |
Correct |
17 ms |
340 KB |
Output is correct |
9 |
Correct |
16 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
596 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1364 KB |
Output is correct |
32 |
Correct |
1 ms |
1364 KB |
Output is correct |
33 |
Correct |
1 ms |
1364 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
596 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1364 KB |
Output is correct |
32 |
Correct |
1 ms |
1364 KB |
Output is correct |
33 |
Correct |
1 ms |
1364 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
4 ms |
7636 KB |
Output is correct |
41 |
Correct |
26 ms |
38332 KB |
Output is correct |
42 |
Correct |
23 ms |
38448 KB |
Output is correct |
43 |
Correct |
23 ms |
38316 KB |
Output is correct |
44 |
Correct |
27 ms |
38228 KB |
Output is correct |
45 |
Correct |
26 ms |
38328 KB |
Output is correct |
46 |
Correct |
28 ms |
38260 KB |
Output is correct |
47 |
Correct |
22 ms |
38236 KB |
Output is correct |
48 |
Correct |
21 ms |
38328 KB |
Output is correct |
49 |
Correct |
24 ms |
38296 KB |
Output is correct |
50 |
Correct |
21 ms |
38228 KB |
Output is correct |
51 |
Correct |
22 ms |
38268 KB |
Output is correct |
52 |
Correct |
22 ms |
38276 KB |
Output is correct |
53 |
Correct |
21 ms |
38356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
596 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
1364 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1364 KB |
Output is correct |
25 |
Correct |
1 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1364 KB |
Output is correct |
32 |
Correct |
1 ms |
1364 KB |
Output is correct |
33 |
Correct |
1 ms |
1364 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
4 ms |
7636 KB |
Output is correct |
41 |
Correct |
26 ms |
38332 KB |
Output is correct |
42 |
Correct |
23 ms |
38448 KB |
Output is correct |
43 |
Correct |
23 ms |
38316 KB |
Output is correct |
44 |
Correct |
27 ms |
38228 KB |
Output is correct |
45 |
Correct |
26 ms |
38328 KB |
Output is correct |
46 |
Correct |
28 ms |
38260 KB |
Output is correct |
47 |
Correct |
22 ms |
38236 KB |
Output is correct |
48 |
Correct |
21 ms |
38328 KB |
Output is correct |
49 |
Correct |
24 ms |
38296 KB |
Output is correct |
50 |
Correct |
21 ms |
38228 KB |
Output is correct |
51 |
Correct |
22 ms |
38268 KB |
Output is correct |
52 |
Correct |
22 ms |
38276 KB |
Output is correct |
53 |
Correct |
21 ms |
38356 KB |
Output is correct |
54 |
Runtime error |
57 ms |
32404 KB |
Execution killed with signal 11 |
55 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
9 ms |
340 KB |
Output is correct |
14 |
Correct |
13 ms |
340 KB |
Output is correct |
15 |
Correct |
12 ms |
328 KB |
Output is correct |
16 |
Correct |
15 ms |
356 KB |
Output is correct |
17 |
Correct |
17 ms |
356 KB |
Output is correct |
18 |
Correct |
17 ms |
340 KB |
Output is correct |
19 |
Correct |
16 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
596 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
1364 KB |
Output is correct |
39 |
Correct |
1 ms |
1364 KB |
Output is correct |
40 |
Correct |
1 ms |
1364 KB |
Output is correct |
41 |
Correct |
1 ms |
1364 KB |
Output is correct |
42 |
Correct |
1 ms |
1364 KB |
Output is correct |
43 |
Correct |
1 ms |
1364 KB |
Output is correct |
44 |
Correct |
1 ms |
1364 KB |
Output is correct |
45 |
Correct |
1 ms |
1364 KB |
Output is correct |
46 |
Correct |
1 ms |
1364 KB |
Output is correct |
47 |
Correct |
1 ms |
1364 KB |
Output is correct |
48 |
Correct |
1 ms |
1364 KB |
Output is correct |
49 |
Correct |
1 ms |
1364 KB |
Output is correct |
50 |
Correct |
1 ms |
1364 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
0 ms |
212 KB |
Output is correct |
57 |
Correct |
4 ms |
7636 KB |
Output is correct |
58 |
Correct |
26 ms |
38332 KB |
Output is correct |
59 |
Correct |
23 ms |
38448 KB |
Output is correct |
60 |
Correct |
23 ms |
38316 KB |
Output is correct |
61 |
Correct |
27 ms |
38228 KB |
Output is correct |
62 |
Correct |
26 ms |
38328 KB |
Output is correct |
63 |
Correct |
28 ms |
38260 KB |
Output is correct |
64 |
Correct |
22 ms |
38236 KB |
Output is correct |
65 |
Correct |
21 ms |
38328 KB |
Output is correct |
66 |
Correct |
24 ms |
38296 KB |
Output is correct |
67 |
Correct |
21 ms |
38228 KB |
Output is correct |
68 |
Correct |
22 ms |
38268 KB |
Output is correct |
69 |
Correct |
22 ms |
38276 KB |
Output is correct |
70 |
Correct |
21 ms |
38356 KB |
Output is correct |
71 |
Runtime error |
57 ms |
32404 KB |
Execution killed with signal 11 |
72 |
Halted |
0 ms |
0 KB |
- |