#include<bits/stdc++.h>
using namespace std;
using ll = long long;
char s[2505];
int qs[2505][2505];
ll dp[2505][2505];
ll A, B, C;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
bool ch = 1;
for (int i = 1;i <= n;i++) {
cin >> s[i];
ch &= s[i] == 'a';
}
cin >> A >> B >> C;
if (ch) {
vector<ll> dp(n + 2, 0);
for (int i = 1;i <= n;i++) {
dp[i] = dp[i - 1] + A;
for (int j = 1;j < i;j++) {
dp[i] = min(dp[i], (dp[j] + B) + A * (i % j) + C * (i / j));
}
}
cout << dp[n] << '\n';
return 0;
}
for (int i = 1;i <= n;i++) {
for (int j = 1;i + j <= n;j++) {
qs[i][j] = s[i] == s[i + j];
}
for (int j = 1;j <= n;j++) {
qs[i][j] += qs[i - 1][j];
}
}
for (int i = 1;i <= n;i++) {
for (int j = i + 1;j <= n;j++) {
dp[i][j] = (j - i + 1) * A;
}
}
for (int i = 1;i <= n;i++) {
dp[i][i] = A;
int cnt = 1;
for (int j = i + 1;j <= n;j++) {
if (s[i] == s[j]) {
cnt++;
dp[i][j] = min(dp[i][j], A * (j - i + 1 - cnt) + A + B + cnt * C);
}
}
}
for (int k = 1;k < n;k++) {
for (int i = 1;i <= n - k;i++) {
int j = i + k;
dp[i][j] = min(dp[i][j], A + dp[i + 1][j]);
dp[i][j] = min(dp[i][j], dp[i][j - 1] + A);
int cnt = 1, prev = j;
for (int l = j + k + 1;l <= n;l++) {
int x = l - j;
if (qs[j][x] - qs[i - 1][x] == j - i + 1) {
if (l - prev > k) {
cnt++;
prev = l;
}
dp[i][l] = min(dp[i][l], A * (l - i + 1 - cnt * (k + 1)) + (dp[i][j] + B) + cnt * C);
}
}
}
}
cout << dp[1][n] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
2392 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 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 |
344 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 |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
492 KB |
Output is correct |
10 |
Correct |
0 ms |
352 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
2392 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 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 |
4440 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 |
4456 KB |
Output is correct |
32 |
Correct |
1 ms |
4456 KB |
Output is correct |
33 |
Correct |
1 ms |
4456 KB |
Output is correct |
34 |
Correct |
0 ms |
360 KB |
Output is correct |
35 |
Correct |
0 ms |
360 KB |
Output is correct |
36 |
Correct |
0 ms |
360 KB |
Output is correct |
37 |
Correct |
0 ms |
360 KB |
Output is correct |
38 |
Correct |
0 ms |
360 KB |
Output is correct |
39 |
Correct |
0 ms |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
2392 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 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 |
4440 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 |
4456 KB |
Output is correct |
32 |
Correct |
1 ms |
4456 KB |
Output is correct |
33 |
Correct |
1 ms |
4456 KB |
Output is correct |
34 |
Correct |
0 ms |
360 KB |
Output is correct |
35 |
Correct |
0 ms |
360 KB |
Output is correct |
36 |
Correct |
0 ms |
360 KB |
Output is correct |
37 |
Correct |
0 ms |
360 KB |
Output is correct |
38 |
Correct |
0 ms |
360 KB |
Output is correct |
39 |
Correct |
0 ms |
360 KB |
Output is correct |
40 |
Correct |
1 ms |
6504 KB |
Output is correct |
41 |
Correct |
3 ms |
8552 KB |
Output is correct |
42 |
Correct |
2 ms |
8552 KB |
Output is correct |
43 |
Correct |
1 ms |
8676 KB |
Output is correct |
44 |
Correct |
2 ms |
8552 KB |
Output is correct |
45 |
Correct |
2 ms |
8552 KB |
Output is correct |
46 |
Correct |
2 ms |
8552 KB |
Output is correct |
47 |
Correct |
2 ms |
8552 KB |
Output is correct |
48 |
Correct |
2 ms |
8552 KB |
Output is correct |
49 |
Correct |
2 ms |
8552 KB |
Output is correct |
50 |
Correct |
2 ms |
8552 KB |
Output is correct |
51 |
Correct |
2 ms |
8552 KB |
Output is correct |
52 |
Correct |
2 ms |
8540 KB |
Output is correct |
53 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
2392 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 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 |
4440 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 |
4456 KB |
Output is correct |
32 |
Correct |
1 ms |
4456 KB |
Output is correct |
33 |
Correct |
1 ms |
4456 KB |
Output is correct |
34 |
Correct |
0 ms |
360 KB |
Output is correct |
35 |
Correct |
0 ms |
360 KB |
Output is correct |
36 |
Correct |
0 ms |
360 KB |
Output is correct |
37 |
Correct |
0 ms |
360 KB |
Output is correct |
38 |
Correct |
0 ms |
360 KB |
Output is correct |
39 |
Correct |
0 ms |
360 KB |
Output is correct |
40 |
Correct |
1 ms |
6504 KB |
Output is correct |
41 |
Correct |
3 ms |
8552 KB |
Output is correct |
42 |
Correct |
2 ms |
8552 KB |
Output is correct |
43 |
Correct |
1 ms |
8676 KB |
Output is correct |
44 |
Correct |
2 ms |
8552 KB |
Output is correct |
45 |
Correct |
2 ms |
8552 KB |
Output is correct |
46 |
Correct |
2 ms |
8552 KB |
Output is correct |
47 |
Correct |
2 ms |
8552 KB |
Output is correct |
48 |
Correct |
2 ms |
8552 KB |
Output is correct |
49 |
Correct |
2 ms |
8552 KB |
Output is correct |
50 |
Correct |
2 ms |
8552 KB |
Output is correct |
51 |
Correct |
2 ms |
8552 KB |
Output is correct |
52 |
Correct |
2 ms |
8540 KB |
Output is correct |
53 |
Correct |
1 ms |
8540 KB |
Output is correct |
54 |
Correct |
8 ms |
16732 KB |
Output is correct |
55 |
Correct |
180 ms |
33372 KB |
Output is correct |
56 |
Correct |
73 ms |
33368 KB |
Output is correct |
57 |
Correct |
75 ms |
33372 KB |
Output is correct |
58 |
Correct |
70 ms |
33372 KB |
Output is correct |
59 |
Correct |
71 ms |
33372 KB |
Output is correct |
60 |
Correct |
67 ms |
33372 KB |
Output is correct |
61 |
Correct |
72 ms |
33372 KB |
Output is correct |
62 |
Correct |
115 ms |
20568 KB |
Output is correct |
63 |
Correct |
80 ms |
16216 KB |
Output is correct |
64 |
Correct |
80 ms |
16308 KB |
Output is correct |
65 |
Correct |
111 ms |
16220 KB |
Output is correct |
66 |
Correct |
77 ms |
33372 KB |
Output is correct |
67 |
Correct |
72 ms |
33376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 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 |
2392 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 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 |
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 |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
348 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
492 KB |
Output is correct |
20 |
Correct |
0 ms |
352 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 |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
4440 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
0 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 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 |
4440 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 |
4456 KB |
Output is correct |
49 |
Correct |
1 ms |
4456 KB |
Output is correct |
50 |
Correct |
1 ms |
4456 KB |
Output is correct |
51 |
Correct |
0 ms |
360 KB |
Output is correct |
52 |
Correct |
0 ms |
360 KB |
Output is correct |
53 |
Correct |
0 ms |
360 KB |
Output is correct |
54 |
Correct |
0 ms |
360 KB |
Output is correct |
55 |
Correct |
0 ms |
360 KB |
Output is correct |
56 |
Correct |
0 ms |
360 KB |
Output is correct |
57 |
Correct |
1 ms |
6504 KB |
Output is correct |
58 |
Correct |
3 ms |
8552 KB |
Output is correct |
59 |
Correct |
2 ms |
8552 KB |
Output is correct |
60 |
Correct |
1 ms |
8676 KB |
Output is correct |
61 |
Correct |
2 ms |
8552 KB |
Output is correct |
62 |
Correct |
2 ms |
8552 KB |
Output is correct |
63 |
Correct |
2 ms |
8552 KB |
Output is correct |
64 |
Correct |
2 ms |
8552 KB |
Output is correct |
65 |
Correct |
2 ms |
8552 KB |
Output is correct |
66 |
Correct |
2 ms |
8552 KB |
Output is correct |
67 |
Correct |
2 ms |
8552 KB |
Output is correct |
68 |
Correct |
2 ms |
8552 KB |
Output is correct |
69 |
Correct |
2 ms |
8540 KB |
Output is correct |
70 |
Correct |
1 ms |
8540 KB |
Output is correct |
71 |
Correct |
8 ms |
16732 KB |
Output is correct |
72 |
Correct |
180 ms |
33372 KB |
Output is correct |
73 |
Correct |
73 ms |
33368 KB |
Output is correct |
74 |
Correct |
75 ms |
33372 KB |
Output is correct |
75 |
Correct |
70 ms |
33372 KB |
Output is correct |
76 |
Correct |
71 ms |
33372 KB |
Output is correct |
77 |
Correct |
67 ms |
33372 KB |
Output is correct |
78 |
Correct |
72 ms |
33372 KB |
Output is correct |
79 |
Correct |
115 ms |
20568 KB |
Output is correct |
80 |
Correct |
80 ms |
16216 KB |
Output is correct |
81 |
Correct |
80 ms |
16308 KB |
Output is correct |
82 |
Correct |
111 ms |
16220 KB |
Output is correct |
83 |
Correct |
77 ms |
33372 KB |
Output is correct |
84 |
Correct |
72 ms |
33376 KB |
Output is correct |
85 |
Correct |
270 ms |
51880 KB |
Output is correct |
86 |
Correct |
994 ms |
74076 KB |
Output is correct |
87 |
Correct |
961 ms |
74072 KB |
Output is correct |
88 |
Correct |
977 ms |
74072 KB |
Output is correct |
89 |
Correct |
967 ms |
74292 KB |
Output is correct |
90 |
Correct |
959 ms |
74076 KB |
Output is correct |
91 |
Correct |
1053 ms |
74076 KB |
Output is correct |
92 |
Correct |
1227 ms |
74072 KB |
Output is correct |
93 |
Correct |
994 ms |
74076 KB |
Output is correct |
94 |
Correct |
990 ms |
74064 KB |
Output is correct |
95 |
Correct |
1165 ms |
74076 KB |
Output is correct |
96 |
Correct |
1124 ms |
74076 KB |
Output is correct |
97 |
Correct |
968 ms |
74072 KB |
Output is correct |