#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 = 2500 + 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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 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 |
328 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1056 ms |
454012 KB |
Output is correct |
4 |
Correct |
1223 ms |
529012 KB |
Output is correct |
5 |
Correct |
1423 ms |
614148 KB |
Output is correct |
6 |
Correct |
1746 ms |
711696 KB |
Output is correct |
7 |
Correct |
2029 ms |
818220 KB |
Output is correct |
8 |
Correct |
2058 ms |
818384 KB |
Output is correct |
9 |
Correct |
1992 ms |
818224 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 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 |
328 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 |
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 |
0 ms |
340 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 |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 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 |
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 |
724 KB |
Output is correct |
34 |
Correct |
0 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 |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 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 |
328 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 |
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 |
0 ms |
340 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 |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 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 |
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 |
724 KB |
Output is correct |
34 |
Correct |
0 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 |
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 |
14 ms |
7972 KB |
Output is correct |
42 |
Correct |
8 ms |
8048 KB |
Output is correct |
43 |
Correct |
7 ms |
7984 KB |
Output is correct |
44 |
Correct |
8 ms |
7892 KB |
Output is correct |
45 |
Correct |
7 ms |
7892 KB |
Output is correct |
46 |
Correct |
7 ms |
7892 KB |
Output is correct |
47 |
Correct |
9 ms |
7956 KB |
Output is correct |
48 |
Correct |
7 ms |
7892 KB |
Output is correct |
49 |
Correct |
7 ms |
7968 KB |
Output is correct |
50 |
Correct |
7 ms |
7892 KB |
Output is correct |
51 |
Correct |
9 ms |
8020 KB |
Output is correct |
52 |
Correct |
8 ms |
7980 KB |
Output is correct |
53 |
Correct |
6 ms |
7900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 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 |
328 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 |
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 |
0 ms |
340 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 |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 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 |
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 |
724 KB |
Output is correct |
34 |
Correct |
0 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 |
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 |
14 ms |
7972 KB |
Output is correct |
42 |
Correct |
8 ms |
8048 KB |
Output is correct |
43 |
Correct |
7 ms |
7984 KB |
Output is correct |
44 |
Correct |
8 ms |
7892 KB |
Output is correct |
45 |
Correct |
7 ms |
7892 KB |
Output is correct |
46 |
Correct |
7 ms |
7892 KB |
Output is correct |
47 |
Correct |
9 ms |
7956 KB |
Output is correct |
48 |
Correct |
7 ms |
7892 KB |
Output is correct |
49 |
Correct |
7 ms |
7968 KB |
Output is correct |
50 |
Correct |
7 ms |
7892 KB |
Output is correct |
51 |
Correct |
9 ms |
8020 KB |
Output is correct |
52 |
Correct |
8 ms |
7980 KB |
Output is correct |
53 |
Correct |
6 ms |
7900 KB |
Output is correct |
54 |
Correct |
35 ms |
31580 KB |
Output is correct |
55 |
Correct |
284 ms |
141760 KB |
Output is correct |
56 |
Correct |
172 ms |
141740 KB |
Output is correct |
57 |
Correct |
191 ms |
141668 KB |
Output is correct |
58 |
Correct |
143 ms |
141668 KB |
Output is correct |
59 |
Correct |
151 ms |
141688 KB |
Output is correct |
60 |
Correct |
134 ms |
141756 KB |
Output is correct |
61 |
Correct |
155 ms |
141672 KB |
Output is correct |
62 |
Correct |
261 ms |
141752 KB |
Output is correct |
63 |
Correct |
190 ms |
141676 KB |
Output is correct |
64 |
Correct |
161 ms |
141648 KB |
Output is correct |
65 |
Correct |
211 ms |
141780 KB |
Output is correct |
66 |
Correct |
237 ms |
141788 KB |
Output is correct |
67 |
Correct |
133 ms |
141712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 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 |
328 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 |
1056 ms |
454012 KB |
Output is correct |
14 |
Correct |
1223 ms |
529012 KB |
Output is correct |
15 |
Correct |
1423 ms |
614148 KB |
Output is correct |
16 |
Correct |
1746 ms |
711696 KB |
Output is correct |
17 |
Correct |
2029 ms |
818220 KB |
Output is correct |
18 |
Correct |
2058 ms |
818384 KB |
Output is correct |
19 |
Correct |
1992 ms |
818224 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
328 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
340 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 |
596 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
724 KB |
Output is correct |
39 |
Correct |
1 ms |
724 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
1 ms |
724 KB |
Output is correct |
42 |
Correct |
1 ms |
724 KB |
Output is correct |
43 |
Correct |
1 ms |
724 KB |
Output is correct |
44 |
Correct |
1 ms |
724 KB |
Output is correct |
45 |
Correct |
1 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
724 KB |
Output is correct |
47 |
Correct |
1 ms |
724 KB |
Output is correct |
48 |
Correct |
1 ms |
724 KB |
Output is correct |
49 |
Correct |
1 ms |
724 KB |
Output is correct |
50 |
Correct |
1 ms |
724 KB |
Output is correct |
51 |
Correct |
0 ms |
340 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 |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
724 KB |
Output is correct |
57 |
Correct |
2 ms |
2260 KB |
Output is correct |
58 |
Correct |
14 ms |
7972 KB |
Output is correct |
59 |
Correct |
8 ms |
8048 KB |
Output is correct |
60 |
Correct |
7 ms |
7984 KB |
Output is correct |
61 |
Correct |
8 ms |
7892 KB |
Output is correct |
62 |
Correct |
7 ms |
7892 KB |
Output is correct |
63 |
Correct |
7 ms |
7892 KB |
Output is correct |
64 |
Correct |
9 ms |
7956 KB |
Output is correct |
65 |
Correct |
7 ms |
7892 KB |
Output is correct |
66 |
Correct |
7 ms |
7968 KB |
Output is correct |
67 |
Correct |
7 ms |
7892 KB |
Output is correct |
68 |
Correct |
9 ms |
8020 KB |
Output is correct |
69 |
Correct |
8 ms |
7980 KB |
Output is correct |
70 |
Correct |
6 ms |
7900 KB |
Output is correct |
71 |
Correct |
35 ms |
31580 KB |
Output is correct |
72 |
Correct |
284 ms |
141760 KB |
Output is correct |
73 |
Correct |
172 ms |
141740 KB |
Output is correct |
74 |
Correct |
191 ms |
141668 KB |
Output is correct |
75 |
Correct |
143 ms |
141668 KB |
Output is correct |
76 |
Correct |
151 ms |
141688 KB |
Output is correct |
77 |
Correct |
134 ms |
141756 KB |
Output is correct |
78 |
Correct |
155 ms |
141672 KB |
Output is correct |
79 |
Correct |
261 ms |
141752 KB |
Output is correct |
80 |
Correct |
190 ms |
141676 KB |
Output is correct |
81 |
Correct |
161 ms |
141648 KB |
Output is correct |
82 |
Correct |
211 ms |
141780 KB |
Output is correct |
83 |
Correct |
237 ms |
141788 KB |
Output is correct |
84 |
Correct |
133 ms |
141712 KB |
Output is correct |
85 |
Correct |
480 ms |
342372 KB |
Output is correct |
86 |
Correct |
1074 ms |
818164 KB |
Output is correct |
87 |
Correct |
951 ms |
818364 KB |
Output is correct |
88 |
Correct |
866 ms |
818248 KB |
Output is correct |
89 |
Correct |
887 ms |
818116 KB |
Output is correct |
90 |
Correct |
797 ms |
818156 KB |
Output is correct |
91 |
Correct |
1518 ms |
818204 KB |
Output is correct |
92 |
Correct |
1727 ms |
818392 KB |
Output is correct |
93 |
Correct |
914 ms |
818316 KB |
Output is correct |
94 |
Correct |
988 ms |
818164 KB |
Output is correct |
95 |
Correct |
1580 ms |
818220 KB |
Output is correct |
96 |
Correct |
1521 ms |
818192 KB |
Output is correct |
97 |
Correct |
789 ms |
818424 KB |
Output is correct |