#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#define int long long
#warning That's the baby, that's not my baby
typedef long long ll;
const int NMAX = 2500;
const int mod = 1e9 + 321;
const int P = 31;
int hsh[NMAX + 1], nxt[NMAX + 1];
std::vector<int> v[NMAX + 1];
ll dp[NMAX + 1][NMAX + 1];
signed main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n;
std::cin >> n;
std::string s;
std::cin >> s;
s = '#' + s;
int A, B, C;
std::cin >> A >> B >> C;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
dp[i][j] = 1e18;
}
for(int j = i; j <= n; j++) {
dp[i][j] = (ll) (j - i + 1) * A;
}
}
for(int len = 1; len <= n; len++) {
std::vector<std::pair<int, int>> x;
for(int i = 1; i <= n - len + 1; i++) {
hsh[i] = ((ll) hsh[i] * 31 + (s[i + len - 1] - 'a' + 1)) % mod;
dp[i][i + len - 1] = std::min({dp[i][i + len - 1], dp[i + 1][i + len - 1] + A, dp[i][i + len - 2] + A});
x.push_back({hsh[i], i});
}
std::sort(x.begin(), x.end());
int cur = 0;
for(int i = 0; i < x.size(); i++) {
if(!i || x[i].first != x[i - 1].first) {
cur++;
}
v[cur].push_back(x[i].second);
}
for(int i = 1; i <= cur; i++) {
for(int j = (int)v[i].size() - 1; j >= 0; j--) {
int l = j + 1, r = v[i].size();
while(l < r) {
int mid = (l + r) / 2;
if(v[i][mid] >= v[i][j] + len) {
r = mid;
} else {
l = mid + 1;
}
}
nxt[j] = r;
}
for(int j = 0; j < (int) v[i].size(); j++) {
int cur = j, cnt = 0;
while(cur < (int) v[i].size()) {
int l = v[i][j], r = v[i][cur] + len - 1;
dp[l][r] = std::min(dp[l][r], (ll)(cnt + 1) * C + (ll)(r - l + 1 - (cnt + 1) * len) * A + B + dp[l][l + len - 1]);
cnt++;
int x = nxt[cur];
if(x == (int) v[i].size()) {
break;
}
cur = x;
}
}
v[i].clear();
}
}
std::cout << dp[1][n];
return 0;
}
Compilation message
copypaste3.cpp:7:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
7 | #warning That's the baby, that's not my baby
| ^~~~~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:50:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
148 ms |
37704 KB |
Output is correct |
4 |
Correct |
220 ms |
39772 KB |
Output is correct |
5 |
Correct |
229 ms |
43944 KB |
Output is correct |
6 |
Correct |
292 ms |
45980 KB |
Output is correct |
7 |
Correct |
346 ms |
49500 KB |
Output is correct |
8 |
Correct |
352 ms |
49748 KB |
Output is correct |
9 |
Correct |
337 ms |
49500 KB |
Output is correct |
10 |
Correct |
0 ms |
348 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 |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
344 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 |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
344 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 |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
2 ms |
4700 KB |
Output is correct |
42 |
Correct |
2 ms |
4700 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
2 ms |
4444 KB |
Output is correct |
45 |
Correct |
2 ms |
4444 KB |
Output is correct |
46 |
Correct |
2 ms |
4444 KB |
Output is correct |
47 |
Correct |
2 ms |
4444 KB |
Output is correct |
48 |
Correct |
2 ms |
4444 KB |
Output is correct |
49 |
Correct |
2 ms |
4440 KB |
Output is correct |
50 |
Correct |
2 ms |
4444 KB |
Output is correct |
51 |
Correct |
2 ms |
4444 KB |
Output is correct |
52 |
Correct |
2 ms |
4440 KB |
Output is correct |
53 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
344 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 |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
2 ms |
4700 KB |
Output is correct |
42 |
Correct |
2 ms |
4700 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
2 ms |
4444 KB |
Output is correct |
45 |
Correct |
2 ms |
4444 KB |
Output is correct |
46 |
Correct |
2 ms |
4444 KB |
Output is correct |
47 |
Correct |
2 ms |
4444 KB |
Output is correct |
48 |
Correct |
2 ms |
4444 KB |
Output is correct |
49 |
Correct |
2 ms |
4440 KB |
Output is correct |
50 |
Correct |
2 ms |
4444 KB |
Output is correct |
51 |
Correct |
2 ms |
4444 KB |
Output is correct |
52 |
Correct |
2 ms |
4440 KB |
Output is correct |
53 |
Correct |
2 ms |
4444 KB |
Output is correct |
54 |
Correct |
7 ms |
10844 KB |
Output is correct |
55 |
Correct |
33 ms |
21084 KB |
Output is correct |
56 |
Correct |
32 ms |
21084 KB |
Output is correct |
57 |
Correct |
32 ms |
21080 KB |
Output is correct |
58 |
Correct |
32 ms |
21080 KB |
Output is correct |
59 |
Correct |
32 ms |
21236 KB |
Output is correct |
60 |
Incorrect |
33 ms |
21084 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
148 ms |
37704 KB |
Output is correct |
14 |
Correct |
220 ms |
39772 KB |
Output is correct |
15 |
Correct |
229 ms |
43944 KB |
Output is correct |
16 |
Correct |
292 ms |
45980 KB |
Output is correct |
17 |
Correct |
346 ms |
49500 KB |
Output is correct |
18 |
Correct |
352 ms |
49748 KB |
Output is correct |
19 |
Correct |
337 ms |
49500 KB |
Output is correct |
20 |
Correct |
0 ms |
348 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 |
600 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 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 |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2648 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2392 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
344 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 |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
2 ms |
4700 KB |
Output is correct |
59 |
Correct |
2 ms |
4700 KB |
Output is correct |
60 |
Correct |
2 ms |
4444 KB |
Output is correct |
61 |
Correct |
2 ms |
4444 KB |
Output is correct |
62 |
Correct |
2 ms |
4444 KB |
Output is correct |
63 |
Correct |
2 ms |
4444 KB |
Output is correct |
64 |
Correct |
2 ms |
4444 KB |
Output is correct |
65 |
Correct |
2 ms |
4444 KB |
Output is correct |
66 |
Correct |
2 ms |
4440 KB |
Output is correct |
67 |
Correct |
2 ms |
4444 KB |
Output is correct |
68 |
Correct |
2 ms |
4444 KB |
Output is correct |
69 |
Correct |
2 ms |
4440 KB |
Output is correct |
70 |
Correct |
2 ms |
4444 KB |
Output is correct |
71 |
Correct |
7 ms |
10844 KB |
Output is correct |
72 |
Correct |
33 ms |
21084 KB |
Output is correct |
73 |
Correct |
32 ms |
21084 KB |
Output is correct |
74 |
Correct |
32 ms |
21080 KB |
Output is correct |
75 |
Correct |
32 ms |
21080 KB |
Output is correct |
76 |
Correct |
32 ms |
21236 KB |
Output is correct |
77 |
Incorrect |
33 ms |
21084 KB |
Output isn't correct |
78 |
Halted |
0 ms |
0 KB |
- |