#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll ans[2501][2501];
ll num_ma[2501][2501];
int n;
string s;
ll a, b, c;
unordered_map<string, int> ma, revma;
vector<vector<int>> apps;
vector<vector<int>> ne;
vector<vector<vector<int>>> lift;
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 + (y - x + 1) / 2; i < y; i++) {
if(a * (y - i) < c) break;
int str_idx = num_ma[i][y];
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 = 1;
// Optimize with binary lifting if this is right
/*
while(apps[str_idx][l] >= x) {
ctApps++;
l = ne[str_idx][l];
if(l == -1) break;
}
*/
// Optimized
for(int z = lift[str_idx][l].size() - 1; z >= 0; z--) {
int tent = lift[str_idx][l][z];
if(tent != -1) {
if(apps[str_idx][tent] >= x) {
l = tent;
ctApps += (1 << z);
}
}
}
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++) {
string s2 = s.substr(i, 1);
for(int j = i+1; j <= n; j++) {
int x;
if(!ma.count(s2)) {
x = ma[s2] = ma.size();
apps.push_back(vector<int>());
siz.push_back(j-i);
}
else x = ma[s2];
apps[x].push_back(i);
num_ma[i][j] = x;
if(j != n) s2.push_back(s[j]);
}
}
for(auto p : ma) {
string s2 = p.first;
reverse(s2.begin(), s2.end());
revma[s2] = p.second;
}
ne = apps;
lift.resize(ne.size());
for(int j = 0; j < ne.size(); j++) {
lift[j].resize(ne[j].size());
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;
}
for(int k = 0; k < v.size(); k++) {
lift[j][k].push_back(ne[j][k]);
}
for(int l = 1; l < 13; l++) {
for(int k = 0; k < v.size(); k++) {
lift[j][k].push_back(-1);
if(lift[j][k][l-1] == -1) lift[j][k][l] = -1;
else lift[j][k][l] = lift[j][lift[j][k][l-1]][l-1];
}
if(lift[j].back()[l] == -1) break;
}
}
cout << (ll)f(0, n) << '\n';
}
Compilation message
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:91:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int j = 0; j < ne.size(); j++) {
| ~~^~~~~~~~~~~
copypaste3.cpp:109:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int k = 0; k < v.size(); k++) {
| ~~^~~~~~~~~~
copypaste3.cpp:113:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int k = 0; k < v.size(); k++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Execution timed out |
3077 ms |
171840 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
412 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
680 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
412 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
680 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
5 ms |
2516 KB |
Output is correct |
41 |
Correct |
18 ms |
3916 KB |
Output is correct |
42 |
Correct |
35 ms |
12120 KB |
Output is correct |
43 |
Correct |
36 ms |
12212 KB |
Output is correct |
44 |
Correct |
28 ms |
12300 KB |
Output is correct |
45 |
Correct |
43 ms |
12292 KB |
Output is correct |
46 |
Correct |
34 ms |
12340 KB |
Output is correct |
47 |
Correct |
12 ms |
5976 KB |
Output is correct |
48 |
Correct |
22 ms |
9244 KB |
Output is correct |
49 |
Correct |
24 ms |
10544 KB |
Output is correct |
50 |
Correct |
22 ms |
10632 KB |
Output is correct |
51 |
Correct |
19 ms |
9132 KB |
Output is correct |
52 |
Correct |
28 ms |
9048 KB |
Output is correct |
53 |
Correct |
30 ms |
12360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
412 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
680 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
724 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
5 ms |
2516 KB |
Output is correct |
41 |
Correct |
18 ms |
3916 KB |
Output is correct |
42 |
Correct |
35 ms |
12120 KB |
Output is correct |
43 |
Correct |
36 ms |
12212 KB |
Output is correct |
44 |
Correct |
28 ms |
12300 KB |
Output is correct |
45 |
Correct |
43 ms |
12292 KB |
Output is correct |
46 |
Correct |
34 ms |
12340 KB |
Output is correct |
47 |
Correct |
12 ms |
5976 KB |
Output is correct |
48 |
Correct |
22 ms |
9244 KB |
Output is correct |
49 |
Correct |
24 ms |
10544 KB |
Output is correct |
50 |
Correct |
22 ms |
10632 KB |
Output is correct |
51 |
Correct |
19 ms |
9132 KB |
Output is correct |
52 |
Correct |
28 ms |
9048 KB |
Output is correct |
53 |
Correct |
30 ms |
12360 KB |
Output is correct |
54 |
Correct |
289 ms |
73028 KB |
Output is correct |
55 |
Correct |
1890 ms |
55408 KB |
Output is correct |
56 |
Correct |
2048 ms |
534556 KB |
Output is correct |
57 |
Correct |
2070 ms |
535376 KB |
Output is correct |
58 |
Correct |
2145 ms |
535936 KB |
Output is correct |
59 |
Correct |
2094 ms |
536240 KB |
Output is correct |
60 |
Correct |
2115 ms |
536472 KB |
Output is correct |
61 |
Correct |
1497 ms |
302640 KB |
Output is correct |
62 |
Correct |
1348 ms |
58236 KB |
Output is correct |
63 |
Correct |
1989 ms |
467860 KB |
Output is correct |
64 |
Correct |
1475 ms |
475092 KB |
Output is correct |
65 |
Correct |
1921 ms |
365220 KB |
Output is correct |
66 |
Correct |
989 ms |
365292 KB |
Output is correct |
67 |
Correct |
1986 ms |
536172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
13 |
Execution timed out |
3077 ms |
171840 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |