#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define hash hashy
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 2e3 + 5;
const int mod = 1e9 + 7, oo = 1e18 + 7;
const int md1 = 1e9 + 87, md2 = 1e9 + 123;
int n, a, b, c;
string s;
int dp[N][N];
int mx[N][N];
int sparse[N][N][15];
ii hash[N];
ii pw26[N];
ii get(int l, int r){
int temp1 = (hash[r].fi - hash[l - 1].fi * pw26[r - l + 1].fi + md1 * md1) % md1;
int temp2 = (hash[r].se - hash[l - 1].se * pw26[r - l + 1].se + md2 * md2) % md2;
return {temp1, temp2};
}
void process(){
cin >> n >> s >> a >> b >> c;
s = '*' + s;
pw26[0] = {1, 1};
for(int i = 1; i <= n; i++){
hash[i].fi = (hash[i - 1].fi * 31 + s[i] - 'a') % md1;
hash[i].se = (hash[i - 1].se * 37 + s[i] - 'a') % md2;
pw26[i].fi = (pw26[i - 1].fi * 31) % md1;
pw26[i].se = (pw26[i - 1].se * 37) % md2;
}
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
if(s[i] != s[j]){
mx[i][j] = mx[j][i] = 0;
continue;
}
int le = i, ri = n;
while(le < ri){
int mid = (le + ri + 1) >> 1;
if((mid + (j - i)) > n){
ri = mid - 1;
continue;
}
ii temp1 = get(i, mid), temp2 = get(j, mid + (j - i));
if(temp1 == temp2) le = mid;
else ri = mid - 1;
}
mx[i][j] = mx[j][i] = (le - i + 1);
//cout << i << " " << j << " " << mx[i][j] << "\n";
}
}
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++) sparse[i][j][0] = mx[i][j];
for(int j = 1; j <= 13; j++){
for(int k = 1; (k + (1LL << j) - 1) <= n; k++){
sparse[i][k][j] = max(sparse[i][k][j - 1], sparse[i][k + (1LL << (j - 1))][j - 1]);
// cout << i << " " << j << " " << k << "\n";
}
}
}
for(int i = 1; i <= n; i++) dp[i][i] = a;
for(int le = n; le >= 1; le--){
for(int ri = le + 1; ri <= n; ri++) dp[le][ri] = oo;
for(int ri = le; ri <= n; ri++){
dp[le][ri] = min(dp[le][ri], min(dp[le][ri - 1], dp[le + 1][ri]) + a);
vector<int> pos;
pos.pb(le);
int itr = ri + 1;
while(itr <= n){
for(int i = 13; i >= 0; i--){
if((itr + (1LL << i) - 1) > n) continue;
// cout << le << " " << ri << " " << itr << " " << i << " " << sparse[le][itr][i] << "\n";
if(sparse[le][itr][i] < (ri - le + 1)) itr += (1LL << i);
}
if(sparse[le][itr][0] < (ri - le + 1)) break;
pos.pb(itr);
itr += (ri - le + 1);
}
int cnt = 0;
for(auto it : pos){
// cout << "OK " << le << " " << ri << " " << it << "\n";
cnt++;
int rem = ((it + ri - le) - le + 1) - (ri - le + 1) * cnt;
// cout << it + (ri - le) << "\n";
if(cnt > 1) dp[le][it + (ri - le)] = min(dp[le][it + (ri - le)], dp[le][ri] + b + c * cnt + a * rem);
}
}
for(int ri = le; ri <= n; ri++){
// cout << le << " " << ri << " " << dp[le][ri] << "\n";
}
}
cout << dp[1][n] << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
//freopen("KEK.inp", "r", stdin);
//freopen("KEK.out", "w", stdout);
cin.tie(0);
process();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
1 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 |
Correct |
959 ms |
448632 KB |
Output is correct |
4 |
Correct |
1142 ms |
519288 KB |
Output is correct |
5 |
Runtime error |
914 ms |
960740 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
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 |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
596 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
712 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
712 KB |
Output is correct |
25 |
Correct |
1 ms |
716 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 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 |
708 KB |
Output is correct |
34 |
Correct |
0 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
324 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 |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
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 |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
596 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
712 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
712 KB |
Output is correct |
25 |
Correct |
1 ms |
716 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 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 |
708 KB |
Output is correct |
34 |
Correct |
0 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
324 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 |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
2260 KB |
Output is correct |
41 |
Correct |
11 ms |
7880 KB |
Output is correct |
42 |
Correct |
10 ms |
7892 KB |
Output is correct |
43 |
Correct |
7 ms |
7880 KB |
Output is correct |
44 |
Correct |
7 ms |
7892 KB |
Output is correct |
45 |
Correct |
8 ms |
7892 KB |
Output is correct |
46 |
Correct |
6 ms |
7960 KB |
Output is correct |
47 |
Correct |
9 ms |
7964 KB |
Output is correct |
48 |
Correct |
7 ms |
7892 KB |
Output is correct |
49 |
Correct |
7 ms |
7892 KB |
Output is correct |
50 |
Correct |
7 ms |
7880 KB |
Output is correct |
51 |
Correct |
8 ms |
7956 KB |
Output is correct |
52 |
Correct |
9 ms |
7880 KB |
Output is correct |
53 |
Correct |
6 ms |
7880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
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 |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
596 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
712 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
712 KB |
Output is correct |
25 |
Correct |
1 ms |
716 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 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 |
708 KB |
Output is correct |
34 |
Correct |
0 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
324 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 |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
2260 KB |
Output is correct |
41 |
Correct |
11 ms |
7880 KB |
Output is correct |
42 |
Correct |
10 ms |
7892 KB |
Output is correct |
43 |
Correct |
7 ms |
7880 KB |
Output is correct |
44 |
Correct |
7 ms |
7892 KB |
Output is correct |
45 |
Correct |
8 ms |
7892 KB |
Output is correct |
46 |
Correct |
6 ms |
7960 KB |
Output is correct |
47 |
Correct |
9 ms |
7964 KB |
Output is correct |
48 |
Correct |
7 ms |
7892 KB |
Output is correct |
49 |
Correct |
7 ms |
7892 KB |
Output is correct |
50 |
Correct |
7 ms |
7880 KB |
Output is correct |
51 |
Correct |
8 ms |
7956 KB |
Output is correct |
52 |
Correct |
9 ms |
7880 KB |
Output is correct |
53 |
Correct |
6 ms |
7880 KB |
Output is correct |
54 |
Correct |
41 ms |
31576 KB |
Output is correct |
55 |
Correct |
287 ms |
141628 KB |
Output is correct |
56 |
Correct |
165 ms |
141640 KB |
Output is correct |
57 |
Correct |
146 ms |
141624 KB |
Output is correct |
58 |
Correct |
138 ms |
141592 KB |
Output is correct |
59 |
Correct |
124 ms |
141556 KB |
Output is correct |
60 |
Correct |
119 ms |
141612 KB |
Output is correct |
61 |
Correct |
142 ms |
141592 KB |
Output is correct |
62 |
Correct |
252 ms |
141644 KB |
Output is correct |
63 |
Correct |
137 ms |
141540 KB |
Output is correct |
64 |
Correct |
148 ms |
141528 KB |
Output is correct |
65 |
Correct |
209 ms |
141552 KB |
Output is correct |
66 |
Correct |
213 ms |
141584 KB |
Output is correct |
67 |
Correct |
123 ms |
141516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
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 |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
959 ms |
448632 KB |
Output is correct |
14 |
Correct |
1142 ms |
519288 KB |
Output is correct |
15 |
Runtime error |
914 ms |
960740 KB |
Execution killed with signal 11 |
16 |
Halted |
0 ms |
0 KB |
- |