#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll ans[2501][2501];
int n;
string s;
ll a, b, c;
map<string, int> ma;
vector<vector<int>> apps;
vector<vector<int>> ne;
vector<int> siz;
ll f(int x, int y) {
if(ans[x][y] != -1) return ans[x][y];
if(y - x == 1) {
return ans[x][y] = a;
}
ll best = f(x, y-1) + a;
for(int i = x+1; i < y; i++) {
if(a * (y - i) < c) break;
int str_idx = ma[s.substr(i, y-i)];
int l = 0, r = apps[str_idx].size();
while(l != r - 1) {
int mid = (l + r) / 2;
if(apps[str_idx][mid] <= i) l = mid;
else r = mid;
}
assert(apps[str_idx][l] == i);
int ctApps = 0;
// Optimize with binary lifting if this is right
while(apps[str_idx][l] >= x) {
ctApps++;
l = ne[str_idx][l];
if(l == -1) break;
}
int indivChars = y - x - ctApps * (y - i);
best = min(best, f(i, y) + b + a * indivChars + c * ctApps);
}
return ans[x][y] = best;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> s >> a >> b >> c;
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) {
ans[i][j] = -1;
}
}
for(int i = 0; i < n; i++) {
for(int j = i+1; j <= n; j++) {
string s2 = s.substr(i, j-i);
if(!ma.count(s2)) {
ma[s2] = ma.size();
apps.push_back(vector<int>());
siz.push_back(j-i);
}
apps[ma[s2]].push_back(i);
}
}
ne = apps;
for(int j = 0; j < ne.size(); j++) {
vector<int> v = ne[j];
queue<int> q;
for(int i = v.size() - 1; i >= 0; i--) {
q.push(i);
int g = q.front();
while(v[g] - v[i] >= siz[j]) {
q.pop();
ne[j][g] = i;
g = q.front();
}
}
while(q.size()) {
int g = q.front();
q.pop();
ne[j][g] = -1;
}
}
cout << (ll)f(0, n) << '\n';
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int j = 0; j < ne.size(); j++) {
| ~~^~~~~~~~~~~
# |
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 |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
332 KB |
Output is correct |
3 |
Execution timed out |
3053 ms |
52016 KB |
Time limit exceeded |
4 |
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 |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
212 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
2 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 |
2 ms |
584 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 |
452 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
260 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
2 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 |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
212 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
2 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 |
2 ms |
584 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 |
452 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
260 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
468 KB |
Output is correct |
40 |
Correct |
25 ms |
1492 KB |
Output is correct |
41 |
Correct |
219 ms |
1748 KB |
Output is correct |
42 |
Correct |
402 ms |
6668 KB |
Output is correct |
43 |
Correct |
22 ms |
6744 KB |
Output is correct |
44 |
Correct |
78 ms |
6788 KB |
Output is correct |
45 |
Correct |
365 ms |
6832 KB |
Output is correct |
46 |
Correct |
407 ms |
6772 KB |
Output is correct |
47 |
Correct |
10 ms |
2900 KB |
Output is correct |
48 |
Correct |
16 ms |
4752 KB |
Output is correct |
49 |
Correct |
18 ms |
5564 KB |
Output is correct |
50 |
Correct |
26 ms |
5724 KB |
Output is correct |
51 |
Correct |
18 ms |
4604 KB |
Output is correct |
52 |
Correct |
366 ms |
4712 KB |
Output is correct |
53 |
Correct |
408 ms |
6796 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 |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
212 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
2 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 |
2 ms |
584 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 |
452 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
260 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
468 KB |
Output is correct |
40 |
Correct |
25 ms |
1492 KB |
Output is correct |
41 |
Correct |
219 ms |
1748 KB |
Output is correct |
42 |
Correct |
402 ms |
6668 KB |
Output is correct |
43 |
Correct |
22 ms |
6744 KB |
Output is correct |
44 |
Correct |
78 ms |
6788 KB |
Output is correct |
45 |
Correct |
365 ms |
6832 KB |
Output is correct |
46 |
Correct |
407 ms |
6772 KB |
Output is correct |
47 |
Correct |
10 ms |
2900 KB |
Output is correct |
48 |
Correct |
16 ms |
4752 KB |
Output is correct |
49 |
Correct |
18 ms |
5564 KB |
Output is correct |
50 |
Correct |
26 ms |
5724 KB |
Output is correct |
51 |
Correct |
18 ms |
4604 KB |
Output is correct |
52 |
Correct |
366 ms |
4712 KB |
Output is correct |
53 |
Correct |
408 ms |
6796 KB |
Output is correct |
54 |
Execution timed out |
3064 ms |
38388 KB |
Time limit exceeded |
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 |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
212 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 |
332 KB |
Output is correct |
13 |
Execution timed out |
3053 ms |
52016 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |