#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define endl "\n"
const int N = 25e2 + 5, mod = 1e9 + 7;
const ll inf = 1e14; //!
int t, h[N], nxt[N];
vector<int> v[N];
ll dp[N][N];
main() {
ios_base::sync_with_stdio(false),
cin.tie(0),cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
s = '#' + s;
int A,B,C;
cin >> A >> B >> C;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) dp[i][j] = inf;
for(int j = i; j <= n; j++) dp[i][j] = (ll)(j - i + 1) * A;
}
int p = 37;
for(int len = 1; len <= n; len++) {
vector<pii> x;
for(int i = 1; i <= n - len + 1; i++) {
h[i] = ((ll)h[i] * p + (s[i + len - 1] - 'a' + 1) ) % mod;
dp[i][i + len - 1] = min(dp[i][i + len - 1], min(dp[i + 1][i + len - 1] + A , dp[i][i + len - 2] + A));
x.push_back({h[i], i});
}
sort(x.begin(), x.end());
int cur = 0;
for(int i = 0; i < x.size(); i++) {
if(!i || x[i].f != x[i - 1].f) cur++;
v[cur].push_back(x[i].s);
}
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();// cnt++;
int x = r; r--;
while(l <= r) {
int mid = (l + r) / 2;
if(v[i][mid] >= v[i][j] + len) x = mid, r = mid - 1;
else l = mid + 1;
}
nxt[j] = x;
}
for(int j = 0; j < v[i].size(); j++) {
int cur = j, cnt = 0;
while(cur < v[i].size()) {
int l = v[i][j], r = v[i][cur] + len - 1;
// cout << dp[l][r] << endl;
dp[l][r] = 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 == v[i].size()) break;
cur = x;
}
}
v[i].clear();
}
}
cout << dp[1][n];
}
Compilation message
copypaste3.cpp:15:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
15 | main() {
| ^~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:40:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
copypaste3.cpp:56:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int j = 0; j < v[i].size(); j++) {
| ~~^~~~~~~~~~~~~
copypaste3.cpp:58:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while(cur < v[i].size()) {
| ~~~~^~~~~~~~~~~~~
copypaste3.cpp:66:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if(x == v[i].size()) break;
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
250 ms |
33920 KB |
Output is correct |
4 |
Correct |
283 ms |
38940 KB |
Output is correct |
5 |
Correct |
404 ms |
42384 KB |
Output is correct |
6 |
Correct |
411 ms |
45780 KB |
Output is correct |
7 |
Correct |
451 ms |
49484 KB |
Output is correct |
8 |
Correct |
482 ms |
49488 KB |
Output is correct |
9 |
Correct |
478 ms |
49484 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
392 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
388 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
516 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
524 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
388 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
388 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
516 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
524 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
388 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
800 KB |
Output is correct |
41 |
Correct |
3 ms |
1492 KB |
Output is correct |
42 |
Correct |
3 ms |
1492 KB |
Output is correct |
43 |
Correct |
4 ms |
1420 KB |
Output is correct |
44 |
Correct |
3 ms |
1492 KB |
Output is correct |
45 |
Correct |
3 ms |
1492 KB |
Output is correct |
46 |
Correct |
3 ms |
1528 KB |
Output is correct |
47 |
Correct |
3 ms |
1492 KB |
Output is correct |
48 |
Correct |
3 ms |
1492 KB |
Output is correct |
49 |
Correct |
3 ms |
1492 KB |
Output is correct |
50 |
Correct |
3 ms |
1492 KB |
Output is correct |
51 |
Correct |
4 ms |
1544 KB |
Output is correct |
52 |
Correct |
3 ms |
1492 KB |
Output is correct |
53 |
Correct |
3 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
388 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
516 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
524 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
388 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
800 KB |
Output is correct |
41 |
Correct |
3 ms |
1492 KB |
Output is correct |
42 |
Correct |
3 ms |
1492 KB |
Output is correct |
43 |
Correct |
4 ms |
1420 KB |
Output is correct |
44 |
Correct |
3 ms |
1492 KB |
Output is correct |
45 |
Correct |
3 ms |
1492 KB |
Output is correct |
46 |
Correct |
3 ms |
1528 KB |
Output is correct |
47 |
Correct |
3 ms |
1492 KB |
Output is correct |
48 |
Correct |
3 ms |
1492 KB |
Output is correct |
49 |
Correct |
3 ms |
1492 KB |
Output is correct |
50 |
Correct |
3 ms |
1492 KB |
Output is correct |
51 |
Correct |
4 ms |
1544 KB |
Output is correct |
52 |
Correct |
3 ms |
1492 KB |
Output is correct |
53 |
Correct |
3 ms |
1492 KB |
Output is correct |
54 |
Correct |
12 ms |
3796 KB |
Output is correct |
55 |
Correct |
73 ms |
12244 KB |
Output is correct |
56 |
Correct |
58 ms |
12292 KB |
Output is correct |
57 |
Correct |
55 ms |
12296 KB |
Output is correct |
58 |
Correct |
56 ms |
12284 KB |
Output is correct |
59 |
Correct |
56 ms |
12300 KB |
Output is correct |
60 |
Correct |
55 ms |
12276 KB |
Output is correct |
61 |
Correct |
59 ms |
12264 KB |
Output is correct |
62 |
Correct |
68 ms |
12244 KB |
Output is correct |
63 |
Correct |
57 ms |
12244 KB |
Output is correct |
64 |
Correct |
57 ms |
12244 KB |
Output is correct |
65 |
Correct |
65 ms |
12284 KB |
Output is correct |
66 |
Correct |
69 ms |
12280 KB |
Output is correct |
67 |
Correct |
57 ms |
12244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
250 ms |
33920 KB |
Output is correct |
14 |
Correct |
283 ms |
38940 KB |
Output is correct |
15 |
Correct |
404 ms |
42384 KB |
Output is correct |
16 |
Correct |
411 ms |
45780 KB |
Output is correct |
17 |
Correct |
451 ms |
49484 KB |
Output is correct |
18 |
Correct |
482 ms |
49488 KB |
Output is correct |
19 |
Correct |
478 ms |
49484 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
392 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
388 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
516 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
524 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
388 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
800 KB |
Output is correct |
58 |
Correct |
3 ms |
1492 KB |
Output is correct |
59 |
Correct |
3 ms |
1492 KB |
Output is correct |
60 |
Correct |
4 ms |
1420 KB |
Output is correct |
61 |
Correct |
3 ms |
1492 KB |
Output is correct |
62 |
Correct |
3 ms |
1492 KB |
Output is correct |
63 |
Correct |
3 ms |
1528 KB |
Output is correct |
64 |
Correct |
3 ms |
1492 KB |
Output is correct |
65 |
Correct |
3 ms |
1492 KB |
Output is correct |
66 |
Correct |
3 ms |
1492 KB |
Output is correct |
67 |
Correct |
3 ms |
1492 KB |
Output is correct |
68 |
Correct |
4 ms |
1544 KB |
Output is correct |
69 |
Correct |
3 ms |
1492 KB |
Output is correct |
70 |
Correct |
3 ms |
1492 KB |
Output is correct |
71 |
Correct |
12 ms |
3796 KB |
Output is correct |
72 |
Correct |
73 ms |
12244 KB |
Output is correct |
73 |
Correct |
58 ms |
12292 KB |
Output is correct |
74 |
Correct |
55 ms |
12296 KB |
Output is correct |
75 |
Correct |
56 ms |
12284 KB |
Output is correct |
76 |
Correct |
56 ms |
12300 KB |
Output is correct |
77 |
Correct |
55 ms |
12276 KB |
Output is correct |
78 |
Correct |
59 ms |
12264 KB |
Output is correct |
79 |
Correct |
68 ms |
12244 KB |
Output is correct |
80 |
Correct |
57 ms |
12244 KB |
Output is correct |
81 |
Correct |
57 ms |
12244 KB |
Output is correct |
82 |
Correct |
65 ms |
12284 KB |
Output is correct |
83 |
Correct |
69 ms |
12280 KB |
Output is correct |
84 |
Correct |
57 ms |
12244 KB |
Output is correct |
85 |
Correct |
159 ms |
26420 KB |
Output is correct |
86 |
Correct |
370 ms |
49616 KB |
Output is correct |
87 |
Correct |
353 ms |
49600 KB |
Output is correct |
88 |
Correct |
372 ms |
49580 KB |
Output is correct |
89 |
Correct |
338 ms |
49564 KB |
Output is correct |
90 |
Correct |
339 ms |
49620 KB |
Output is correct |
91 |
Correct |
520 ms |
49484 KB |
Output is correct |
92 |
Correct |
588 ms |
49488 KB |
Output is correct |
93 |
Correct |
431 ms |
49528 KB |
Output is correct |
94 |
Correct |
387 ms |
49580 KB |
Output is correct |
95 |
Correct |
516 ms |
49668 KB |
Output is correct |
96 |
Correct |
549 ms |
49564 KB |
Output is correct |
97 |
Correct |
336 ms |
49616 KB |
Output is correct |