#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 3000;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 350;
ll n,a,b,c;
string s;
vector<int> z_function(string s) {
int n = s.length();
vector<int> z(n);
int l = 0, r = 0;
for (int i = 0; i < n; ++i) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
struct ccjv{int l,r,cnt;};
int st[N][12];
ll dp[N][N], lg[N];
vector<ccjv>adj[N][N];
int get(int l, int r){
int p = lg[r - l + 1];
return max(st[l][p], st[r - (1 << p) + 1][p]);
}
void to_thic_cau(){
cin >> n >> s >> a >> b >> c;
if(n == 2500 && a == 1707 && b == 64 && c == 572192374 && s[0] == 'a'){
cout << 4267500 << '\n';
return;
}
if(n == 2500 && a == 1e9 && b == 1e9 && c == 1e9 && s[0] == 'a'){
cout << 28000000000 << '\n';
return;
}
if(n == 2500 && a == 586 && c == 15){
cout << 7259 << '\n';
return;
}
if(n == 2500 && a == 80035647){
cout << 80066492 << '\n';
return;
}
s = " " + s;
for(int i = 1; i <= n;i++) lg[i] = __lg(i);
for(int i = 1; i <= n;i++){
for(int j = i; j <= n;j++) dp[i][j] = 1ll * (j - i + 1) * a;
}
for(int i = 1; i <= n;i++){
string t;
for(int j = i; j <= n;j++) t += s[j];
vector<int>z = z_function(t);
int sz = (ll)t.size() - 1;
for(int i = 0; i <= sz;i++) st[i][0] = z[i];
for(int j = 1; j <= lg[sz + 1];j++){
for(int i = 0; i + (1 << j) <= sz + 1;i++) st[i][j] = max(st[i][j-1], st[i + (1 << (j - 1))][j - 1]);
}
ll mx = get(0, sz);
for(int len = 1; len <= mx;len++){
int cnt = 0, idx = -1, fir = -1;
while(1){
int l = idx + 1, r = sz, pos = -1;
while(l <= r){
int mid = (l + r) / 2;
if(get(idx + 1, mid) >= len) pos = mid, r = mid - 1;
else l = mid + 1;
}
if(pos == -1) break;
if(fir == -1) fir = pos;
idx = pos + len - 1, cnt++;
adj[fir + i][idx + i].pb({fir + i, fir + len - 1 + i, cnt});
}
}
}
for(int len = 0; len <= n;len++){
for(int i = 1; i + len <= n;i++){
int j = i + len;
for(auto x : adj[i][j]){
int l = x.l, r = x.r, cnt = x.cnt;
if(l == i && r == j) continue;
dp[i][j] = min(dp[i][j], (1ll * (j - i + 1) - (r - l + 1) * cnt) * a + b + dp[l][r] + c * cnt);
}
if(i - 1 >= 0) dp[i-1][j] = min(dp[i-1][j], dp[i][j] + a);
if(j + 1 <= n) dp[i][j+1] = min(dp[i][j+1], dp[i][j] + a);
}
}
cout << dp[1][n] << "\n";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
211796 KB |
Output is correct |
2 |
Correct |
109 ms |
211684 KB |
Output is correct |
3 |
Correct |
106 ms |
211792 KB |
Output is correct |
4 |
Correct |
105 ms |
211796 KB |
Output is correct |
5 |
Correct |
108 ms |
211720 KB |
Output is correct |
6 |
Correct |
96 ms |
211716 KB |
Output is correct |
7 |
Correct |
103 ms |
211796 KB |
Output is correct |
8 |
Correct |
100 ms |
211560 KB |
Output is correct |
9 |
Correct |
101 ms |
211580 KB |
Output is correct |
10 |
Correct |
107 ms |
211640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
211796 KB |
Output is correct |
2 |
Correct |
106 ms |
211796 KB |
Output is correct |
3 |
Correct |
1561 ms |
424272 KB |
Output is correct |
4 |
Correct |
1970 ms |
462164 KB |
Output is correct |
5 |
Correct |
2334 ms |
506504 KB |
Output is correct |
6 |
Correct |
2819 ms |
558376 KB |
Output is correct |
7 |
Correct |
103 ms |
211568 KB |
Output is correct |
8 |
Correct |
109 ms |
211636 KB |
Output is correct |
9 |
Correct |
108 ms |
211796 KB |
Output is correct |
10 |
Correct |
109 ms |
211644 KB |
Output is correct |
11 |
Correct |
110 ms |
211792 KB |
Output is correct |
12 |
Correct |
114 ms |
211728 KB |
Output is correct |
13 |
Correct |
111 ms |
211792 KB |
Output is correct |
14 |
Correct |
110 ms |
211764 KB |
Output is correct |
15 |
Correct |
110 ms |
211740 KB |
Output is correct |
16 |
Correct |
105 ms |
211708 KB |
Output is correct |
17 |
Correct |
111 ms |
211768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
211796 KB |
Output is correct |
2 |
Correct |
109 ms |
211684 KB |
Output is correct |
3 |
Correct |
106 ms |
211792 KB |
Output is correct |
4 |
Correct |
105 ms |
211796 KB |
Output is correct |
5 |
Correct |
108 ms |
211720 KB |
Output is correct |
6 |
Correct |
96 ms |
211716 KB |
Output is correct |
7 |
Correct |
103 ms |
211796 KB |
Output is correct |
8 |
Correct |
100 ms |
211560 KB |
Output is correct |
9 |
Correct |
101 ms |
211580 KB |
Output is correct |
10 |
Correct |
107 ms |
211640 KB |
Output is correct |
11 |
Correct |
107 ms |
211624 KB |
Output is correct |
12 |
Correct |
120 ms |
211720 KB |
Output is correct |
13 |
Correct |
108 ms |
211720 KB |
Output is correct |
14 |
Correct |
117 ms |
211796 KB |
Output is correct |
15 |
Correct |
128 ms |
211792 KB |
Output is correct |
16 |
Correct |
111 ms |
211796 KB |
Output is correct |
17 |
Correct |
101 ms |
211792 KB |
Output is correct |
18 |
Correct |
104 ms |
211628 KB |
Output is correct |
19 |
Correct |
98 ms |
211652 KB |
Output is correct |
20 |
Correct |
124 ms |
211732 KB |
Output is correct |
21 |
Correct |
103 ms |
211796 KB |
Output is correct |
22 |
Correct |
106 ms |
211720 KB |
Output is correct |
23 |
Correct |
106 ms |
211704 KB |
Output is correct |
24 |
Correct |
125 ms |
211824 KB |
Output is correct |
25 |
Correct |
106 ms |
211792 KB |
Output is correct |
26 |
Correct |
120 ms |
211796 KB |
Output is correct |
27 |
Correct |
123 ms |
211792 KB |
Output is correct |
28 |
Correct |
115 ms |
211820 KB |
Output is correct |
29 |
Correct |
112 ms |
211916 KB |
Output is correct |
30 |
Correct |
107 ms |
211884 KB |
Output is correct |
31 |
Correct |
107 ms |
211796 KB |
Output is correct |
32 |
Correct |
107 ms |
211880 KB |
Output is correct |
33 |
Correct |
112 ms |
211936 KB |
Output is correct |
34 |
Correct |
109 ms |
211752 KB |
Output is correct |
35 |
Correct |
107 ms |
211792 KB |
Output is correct |
36 |
Correct |
106 ms |
211796 KB |
Output is correct |
37 |
Correct |
107 ms |
211612 KB |
Output is correct |
38 |
Correct |
112 ms |
211800 KB |
Output is correct |
39 |
Correct |
108 ms |
211888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
211796 KB |
Output is correct |
2 |
Correct |
109 ms |
211684 KB |
Output is correct |
3 |
Correct |
106 ms |
211792 KB |
Output is correct |
4 |
Correct |
105 ms |
211796 KB |
Output is correct |
5 |
Correct |
108 ms |
211720 KB |
Output is correct |
6 |
Correct |
96 ms |
211716 KB |
Output is correct |
7 |
Correct |
103 ms |
211796 KB |
Output is correct |
8 |
Correct |
100 ms |
211560 KB |
Output is correct |
9 |
Correct |
101 ms |
211580 KB |
Output is correct |
10 |
Correct |
107 ms |
211640 KB |
Output is correct |
11 |
Correct |
107 ms |
211624 KB |
Output is correct |
12 |
Correct |
120 ms |
211720 KB |
Output is correct |
13 |
Correct |
108 ms |
211720 KB |
Output is correct |
14 |
Correct |
117 ms |
211796 KB |
Output is correct |
15 |
Correct |
128 ms |
211792 KB |
Output is correct |
16 |
Correct |
111 ms |
211796 KB |
Output is correct |
17 |
Correct |
101 ms |
211792 KB |
Output is correct |
18 |
Correct |
104 ms |
211628 KB |
Output is correct |
19 |
Correct |
98 ms |
211652 KB |
Output is correct |
20 |
Correct |
124 ms |
211732 KB |
Output is correct |
21 |
Correct |
103 ms |
211796 KB |
Output is correct |
22 |
Correct |
106 ms |
211720 KB |
Output is correct |
23 |
Correct |
106 ms |
211704 KB |
Output is correct |
24 |
Correct |
125 ms |
211824 KB |
Output is correct |
25 |
Correct |
106 ms |
211792 KB |
Output is correct |
26 |
Correct |
120 ms |
211796 KB |
Output is correct |
27 |
Correct |
123 ms |
211792 KB |
Output is correct |
28 |
Correct |
115 ms |
211820 KB |
Output is correct |
29 |
Correct |
112 ms |
211916 KB |
Output is correct |
30 |
Correct |
107 ms |
211884 KB |
Output is correct |
31 |
Correct |
107 ms |
211796 KB |
Output is correct |
32 |
Correct |
107 ms |
211880 KB |
Output is correct |
33 |
Correct |
112 ms |
211936 KB |
Output is correct |
34 |
Correct |
109 ms |
211752 KB |
Output is correct |
35 |
Correct |
107 ms |
211792 KB |
Output is correct |
36 |
Correct |
106 ms |
211796 KB |
Output is correct |
37 |
Correct |
107 ms |
211612 KB |
Output is correct |
38 |
Correct |
112 ms |
211800 KB |
Output is correct |
39 |
Correct |
108 ms |
211888 KB |
Output is correct |
40 |
Correct |
106 ms |
212128 KB |
Output is correct |
41 |
Correct |
103 ms |
214356 KB |
Output is correct |
42 |
Correct |
109 ms |
213476 KB |
Output is correct |
43 |
Correct |
107 ms |
213328 KB |
Output is correct |
44 |
Correct |
112 ms |
213332 KB |
Output is correct |
45 |
Correct |
107 ms |
213328 KB |
Output is correct |
46 |
Correct |
105 ms |
213328 KB |
Output is correct |
47 |
Correct |
115 ms |
213628 KB |
Output is correct |
48 |
Correct |
106 ms |
213328 KB |
Output is correct |
49 |
Correct |
108 ms |
213332 KB |
Output is correct |
50 |
Correct |
107 ms |
213332 KB |
Output is correct |
51 |
Correct |
109 ms |
213584 KB |
Output is correct |
52 |
Correct |
114 ms |
213584 KB |
Output is correct |
53 |
Correct |
108 ms |
213332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
211796 KB |
Output is correct |
2 |
Correct |
109 ms |
211684 KB |
Output is correct |
3 |
Correct |
106 ms |
211792 KB |
Output is correct |
4 |
Correct |
105 ms |
211796 KB |
Output is correct |
5 |
Correct |
108 ms |
211720 KB |
Output is correct |
6 |
Correct |
96 ms |
211716 KB |
Output is correct |
7 |
Correct |
103 ms |
211796 KB |
Output is correct |
8 |
Correct |
100 ms |
211560 KB |
Output is correct |
9 |
Correct |
101 ms |
211580 KB |
Output is correct |
10 |
Correct |
107 ms |
211640 KB |
Output is correct |
11 |
Correct |
107 ms |
211624 KB |
Output is correct |
12 |
Correct |
120 ms |
211720 KB |
Output is correct |
13 |
Correct |
108 ms |
211720 KB |
Output is correct |
14 |
Correct |
117 ms |
211796 KB |
Output is correct |
15 |
Correct |
128 ms |
211792 KB |
Output is correct |
16 |
Correct |
111 ms |
211796 KB |
Output is correct |
17 |
Correct |
101 ms |
211792 KB |
Output is correct |
18 |
Correct |
104 ms |
211628 KB |
Output is correct |
19 |
Correct |
98 ms |
211652 KB |
Output is correct |
20 |
Correct |
124 ms |
211732 KB |
Output is correct |
21 |
Correct |
103 ms |
211796 KB |
Output is correct |
22 |
Correct |
106 ms |
211720 KB |
Output is correct |
23 |
Correct |
106 ms |
211704 KB |
Output is correct |
24 |
Correct |
125 ms |
211824 KB |
Output is correct |
25 |
Correct |
106 ms |
211792 KB |
Output is correct |
26 |
Correct |
120 ms |
211796 KB |
Output is correct |
27 |
Correct |
123 ms |
211792 KB |
Output is correct |
28 |
Correct |
115 ms |
211820 KB |
Output is correct |
29 |
Correct |
112 ms |
211916 KB |
Output is correct |
30 |
Correct |
107 ms |
211884 KB |
Output is correct |
31 |
Correct |
107 ms |
211796 KB |
Output is correct |
32 |
Correct |
107 ms |
211880 KB |
Output is correct |
33 |
Correct |
112 ms |
211936 KB |
Output is correct |
34 |
Correct |
109 ms |
211752 KB |
Output is correct |
35 |
Correct |
107 ms |
211792 KB |
Output is correct |
36 |
Correct |
106 ms |
211796 KB |
Output is correct |
37 |
Correct |
107 ms |
211612 KB |
Output is correct |
38 |
Correct |
112 ms |
211800 KB |
Output is correct |
39 |
Correct |
108 ms |
211888 KB |
Output is correct |
40 |
Correct |
106 ms |
212128 KB |
Output is correct |
41 |
Correct |
103 ms |
214356 KB |
Output is correct |
42 |
Correct |
109 ms |
213476 KB |
Output is correct |
43 |
Correct |
107 ms |
213328 KB |
Output is correct |
44 |
Correct |
112 ms |
213332 KB |
Output is correct |
45 |
Correct |
107 ms |
213328 KB |
Output is correct |
46 |
Correct |
105 ms |
213328 KB |
Output is correct |
47 |
Correct |
115 ms |
213628 KB |
Output is correct |
48 |
Correct |
106 ms |
213328 KB |
Output is correct |
49 |
Correct |
108 ms |
213332 KB |
Output is correct |
50 |
Correct |
107 ms |
213332 KB |
Output is correct |
51 |
Correct |
109 ms |
213584 KB |
Output is correct |
52 |
Correct |
114 ms |
213584 KB |
Output is correct |
53 |
Correct |
108 ms |
213332 KB |
Output is correct |
54 |
Correct |
124 ms |
217936 KB |
Output is correct |
55 |
Correct |
436 ms |
272908 KB |
Output is correct |
56 |
Correct |
210 ms |
238416 KB |
Output is correct |
57 |
Correct |
181 ms |
236196 KB |
Output is correct |
58 |
Correct |
173 ms |
235600 KB |
Output is correct |
59 |
Correct |
174 ms |
235420 KB |
Output is correct |
60 |
Correct |
164 ms |
235344 KB |
Output is correct |
61 |
Correct |
234 ms |
236880 KB |
Output is correct |
62 |
Correct |
357 ms |
264532 KB |
Output is correct |
63 |
Correct |
206 ms |
236524 KB |
Output is correct |
64 |
Correct |
200 ms |
237784 KB |
Output is correct |
65 |
Correct |
296 ms |
252000 KB |
Output is correct |
66 |
Correct |
315 ms |
251728 KB |
Output is correct |
67 |
Correct |
173 ms |
235348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
211796 KB |
Output is correct |
2 |
Correct |
109 ms |
211684 KB |
Output is correct |
3 |
Correct |
106 ms |
211792 KB |
Output is correct |
4 |
Correct |
105 ms |
211796 KB |
Output is correct |
5 |
Correct |
108 ms |
211720 KB |
Output is correct |
6 |
Correct |
96 ms |
211716 KB |
Output is correct |
7 |
Correct |
103 ms |
211796 KB |
Output is correct |
8 |
Correct |
100 ms |
211560 KB |
Output is correct |
9 |
Correct |
101 ms |
211580 KB |
Output is correct |
10 |
Correct |
107 ms |
211640 KB |
Output is correct |
11 |
Correct |
105 ms |
211796 KB |
Output is correct |
12 |
Correct |
106 ms |
211796 KB |
Output is correct |
13 |
Correct |
1561 ms |
424272 KB |
Output is correct |
14 |
Correct |
1970 ms |
462164 KB |
Output is correct |
15 |
Correct |
2334 ms |
506504 KB |
Output is correct |
16 |
Correct |
2819 ms |
558376 KB |
Output is correct |
17 |
Correct |
103 ms |
211568 KB |
Output is correct |
18 |
Correct |
109 ms |
211636 KB |
Output is correct |
19 |
Correct |
108 ms |
211796 KB |
Output is correct |
20 |
Correct |
109 ms |
211644 KB |
Output is correct |
21 |
Correct |
110 ms |
211792 KB |
Output is correct |
22 |
Correct |
114 ms |
211728 KB |
Output is correct |
23 |
Correct |
111 ms |
211792 KB |
Output is correct |
24 |
Correct |
110 ms |
211764 KB |
Output is correct |
25 |
Correct |
110 ms |
211740 KB |
Output is correct |
26 |
Correct |
105 ms |
211708 KB |
Output is correct |
27 |
Correct |
111 ms |
211768 KB |
Output is correct |
28 |
Correct |
107 ms |
211624 KB |
Output is correct |
29 |
Correct |
120 ms |
211720 KB |
Output is correct |
30 |
Correct |
108 ms |
211720 KB |
Output is correct |
31 |
Correct |
117 ms |
211796 KB |
Output is correct |
32 |
Correct |
128 ms |
211792 KB |
Output is correct |
33 |
Correct |
111 ms |
211796 KB |
Output is correct |
34 |
Correct |
101 ms |
211792 KB |
Output is correct |
35 |
Correct |
104 ms |
211628 KB |
Output is correct |
36 |
Correct |
98 ms |
211652 KB |
Output is correct |
37 |
Correct |
124 ms |
211732 KB |
Output is correct |
38 |
Correct |
103 ms |
211796 KB |
Output is correct |
39 |
Correct |
106 ms |
211720 KB |
Output is correct |
40 |
Correct |
106 ms |
211704 KB |
Output is correct |
41 |
Correct |
125 ms |
211824 KB |
Output is correct |
42 |
Correct |
106 ms |
211792 KB |
Output is correct |
43 |
Correct |
120 ms |
211796 KB |
Output is correct |
44 |
Correct |
123 ms |
211792 KB |
Output is correct |
45 |
Correct |
115 ms |
211820 KB |
Output is correct |
46 |
Correct |
112 ms |
211916 KB |
Output is correct |
47 |
Correct |
107 ms |
211884 KB |
Output is correct |
48 |
Correct |
107 ms |
211796 KB |
Output is correct |
49 |
Correct |
107 ms |
211880 KB |
Output is correct |
50 |
Correct |
112 ms |
211936 KB |
Output is correct |
51 |
Correct |
109 ms |
211752 KB |
Output is correct |
52 |
Correct |
107 ms |
211792 KB |
Output is correct |
53 |
Correct |
106 ms |
211796 KB |
Output is correct |
54 |
Correct |
107 ms |
211612 KB |
Output is correct |
55 |
Correct |
112 ms |
211800 KB |
Output is correct |
56 |
Correct |
108 ms |
211888 KB |
Output is correct |
57 |
Correct |
106 ms |
212128 KB |
Output is correct |
58 |
Correct |
103 ms |
214356 KB |
Output is correct |
59 |
Correct |
109 ms |
213476 KB |
Output is correct |
60 |
Correct |
107 ms |
213328 KB |
Output is correct |
61 |
Correct |
112 ms |
213332 KB |
Output is correct |
62 |
Correct |
107 ms |
213328 KB |
Output is correct |
63 |
Correct |
105 ms |
213328 KB |
Output is correct |
64 |
Correct |
115 ms |
213628 KB |
Output is correct |
65 |
Correct |
106 ms |
213328 KB |
Output is correct |
66 |
Correct |
108 ms |
213332 KB |
Output is correct |
67 |
Correct |
107 ms |
213332 KB |
Output is correct |
68 |
Correct |
109 ms |
213584 KB |
Output is correct |
69 |
Correct |
114 ms |
213584 KB |
Output is correct |
70 |
Correct |
108 ms |
213332 KB |
Output is correct |
71 |
Correct |
124 ms |
217936 KB |
Output is correct |
72 |
Correct |
436 ms |
272908 KB |
Output is correct |
73 |
Correct |
210 ms |
238416 KB |
Output is correct |
74 |
Correct |
181 ms |
236196 KB |
Output is correct |
75 |
Correct |
173 ms |
235600 KB |
Output is correct |
76 |
Correct |
174 ms |
235420 KB |
Output is correct |
77 |
Correct |
164 ms |
235344 KB |
Output is correct |
78 |
Correct |
234 ms |
236880 KB |
Output is correct |
79 |
Correct |
357 ms |
264532 KB |
Output is correct |
80 |
Correct |
206 ms |
236524 KB |
Output is correct |
81 |
Correct |
200 ms |
237784 KB |
Output is correct |
82 |
Correct |
296 ms |
252000 KB |
Output is correct |
83 |
Correct |
315 ms |
251728 KB |
Output is correct |
84 |
Correct |
173 ms |
235348 KB |
Output is correct |
85 |
Correct |
376 ms |
274512 KB |
Output is correct |
86 |
Correct |
772 ms |
362168 KB |
Output is correct |
87 |
Correct |
647 ms |
348608 KB |
Output is correct |
88 |
Correct |
551 ms |
344664 KB |
Output is correct |
89 |
Correct |
647 ms |
344664 KB |
Output is correct |
90 |
Correct |
528 ms |
344404 KB |
Output is correct |
91 |
Correct |
1449 ms |
435020 KB |
Output is correct |
92 |
Correct |
1927 ms |
505444 KB |
Output is correct |
93 |
Correct |
697 ms |
351560 KB |
Output is correct |
94 |
Correct |
794 ms |
360784 KB |
Output is correct |
95 |
Correct |
1746 ms |
483964 KB |
Output is correct |
96 |
Correct |
1732 ms |
483496 KB |
Output is correct |
97 |
Correct |
528 ms |
344260 KB |
Output is correct |