#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
#define MP make_pair
ll t;
string s;
ll a,b,c;
ll n;
const ll MAXN = 2500;
ll dp[MAXN+10][MAXN+10];
ll type[MAXN+10][MAXN+10];
ll nxt[MAXN+10][MAXN+10];
ll z[MAXN+10][MAXN+10];
vector <ll> all[MAXN+10];
ll best[MAXN + 10];
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
cin>>t;
// while (t--){
cin>>s;
cin>>a>>b>>c;
n = sz(s);
s = "x" + s;
for (ll i = 1;i <= n;i ++){
for (ll j = i + 1,l = 0,r = 0;j <= n;j ++){
ll ptr;
if (r > j)ptr = min(r,j+z[i][i+j-l]-1);
else ptr = j-1;
while (ptr+1<=n&&s[ptr+1]==s[i+ptr+1-j])ptr++;
z[i][j] = ptr-j+1;
if (ptr > r){
r = ptr;
l = j;
}
}
}
for (ll j = 1;j <= n;j ++){
for (ll i = 1;i < j;i ++){
if (best[j] == 0)best[j] = i;
else{
if (z[best[j]][j] < z[i][j])best[j] = i;
}
}
}
for (ll k = 1;k <= n;k ++){
ll ptr = 1;
for (ll l = 1,r = k; r <= n; l++,r++){
if (l==1)type[k][l] = ptr++;
else{
if (z[best[l]][l] >= k)type[k][l] = type[k][best[l]];
else type[k][l] = ptr++;
}
}
}
for (ll k = 1;k <= n;k ++){
for (ll i = 1;i <= n;i ++)all[i].clear();
for (ll l = 1,r = k; r <= n; l++,r++){
all[type[k][l]].push_back(l);
}
for (ll i = 1;i <= n;i ++){
for (ll j = 0,ptr = 0;j < sz(all[i]);j ++){
while (ptr < sz(all[i]) && all[i][j] + k > all[i][ptr])ptr++;
if (ptr < sz(all[i]))nxt[k][all[i][j]] = all[i][ptr];
else nxt[k][all[i][j]] = -1;
}
}
// for (ll l = 1,r = k; r <= n; l++,r++){
// cout<<nxt[k][l]<<' ';
// }
// cout<<'\n';
}
for (ll k = 1;k <= n;k ++){
for (ll l = 1,r = k; r <= n; l++,r++){
dp[l][r] = (r-l+1)*a;
}
}
for (ll k = 1;k <= n;k ++){
for (ll l = 1,r = k; r <= n; l++,r++){
if (l > 1)dp[l-1][r] = min(dp[l-1][r],dp[l][r]+a);
if (r < n)dp[l][r+1] = min(dp[l][r+1],dp[l][r]+a);
ll cur = l;
ll cnt = 1;
while (1){
cur = nxt[k][cur];
cnt++;
if (cur==-1)break;
dp[l][cur + k - 1] = min(dp[l][cur + k - 1],dp[l][r] + b + c * cnt + (cur+k-1-l+1-cnt*k)*a);
}
}
}
// cout<<dp[3][3]<<'\n';
cout<<dp[1][n]<<'\n';
// }
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8732 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8736 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
144 ms |
150404 KB |
Output is correct |
4 |
Correct |
163 ms |
158632 KB |
Output is correct |
5 |
Correct |
195 ms |
175184 KB |
Output is correct |
6 |
Correct |
237 ms |
191316 KB |
Output is correct |
7 |
Correct |
290 ms |
197460 KB |
Output is correct |
8 |
Correct |
299 ms |
197708 KB |
Output is correct |
9 |
Correct |
294 ms |
197656 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10584 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8732 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8736 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10840 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
8540 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8732 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8736 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10840 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
8540 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10588 KB |
Output is correct |
40 |
Correct |
2 ms |
10588 KB |
Output is correct |
41 |
Correct |
3 ms |
18780 KB |
Output is correct |
42 |
Correct |
4 ms |
18780 KB |
Output is correct |
43 |
Correct |
3 ms |
18780 KB |
Output is correct |
44 |
Correct |
3 ms |
18780 KB |
Output is correct |
45 |
Correct |
3 ms |
18776 KB |
Output is correct |
46 |
Correct |
3 ms |
18780 KB |
Output is correct |
47 |
Correct |
3 ms |
18780 KB |
Output is correct |
48 |
Correct |
3 ms |
18780 KB |
Output is correct |
49 |
Correct |
3 ms |
18780 KB |
Output is correct |
50 |
Correct |
3 ms |
18780 KB |
Output is correct |
51 |
Correct |
3 ms |
18780 KB |
Output is correct |
52 |
Correct |
3 ms |
18780 KB |
Output is correct |
53 |
Correct |
3 ms |
18776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8732 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8736 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10840 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
8540 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10588 KB |
Output is correct |
40 |
Correct |
2 ms |
10588 KB |
Output is correct |
41 |
Correct |
3 ms |
18780 KB |
Output is correct |
42 |
Correct |
4 ms |
18780 KB |
Output is correct |
43 |
Correct |
3 ms |
18780 KB |
Output is correct |
44 |
Correct |
3 ms |
18780 KB |
Output is correct |
45 |
Correct |
3 ms |
18776 KB |
Output is correct |
46 |
Correct |
3 ms |
18780 KB |
Output is correct |
47 |
Correct |
3 ms |
18780 KB |
Output is correct |
48 |
Correct |
3 ms |
18780 KB |
Output is correct |
49 |
Correct |
3 ms |
18780 KB |
Output is correct |
50 |
Correct |
3 ms |
18780 KB |
Output is correct |
51 |
Correct |
3 ms |
18780 KB |
Output is correct |
52 |
Correct |
3 ms |
18780 KB |
Output is correct |
53 |
Correct |
3 ms |
18776 KB |
Output is correct |
54 |
Correct |
8 ms |
43612 KB |
Output is correct |
55 |
Correct |
27 ms |
84572 KB |
Output is correct |
56 |
Correct |
20 ms |
84592 KB |
Output is correct |
57 |
Correct |
20 ms |
84576 KB |
Output is correct |
58 |
Correct |
17 ms |
84568 KB |
Output is correct |
59 |
Correct |
17 ms |
84572 KB |
Output is correct |
60 |
Correct |
18 ms |
84572 KB |
Output is correct |
61 |
Correct |
18 ms |
84572 KB |
Output is correct |
62 |
Correct |
25 ms |
84676 KB |
Output is correct |
63 |
Correct |
18 ms |
84728 KB |
Output is correct |
64 |
Correct |
20 ms |
84572 KB |
Output is correct |
65 |
Correct |
23 ms |
84572 KB |
Output is correct |
66 |
Correct |
23 ms |
84560 KB |
Output is correct |
67 |
Correct |
17 ms |
84568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8732 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8736 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
144 ms |
150404 KB |
Output is correct |
14 |
Correct |
163 ms |
158632 KB |
Output is correct |
15 |
Correct |
195 ms |
175184 KB |
Output is correct |
16 |
Correct |
237 ms |
191316 KB |
Output is correct |
17 |
Correct |
290 ms |
197460 KB |
Output is correct |
18 |
Correct |
299 ms |
197708 KB |
Output is correct |
19 |
Correct |
294 ms |
197656 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
8540 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10840 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
1 ms |
10588 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
1 ms |
8540 KB |
Output is correct |
38 |
Correct |
1 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10588 KB |
Output is correct |
40 |
Correct |
2 ms |
10588 KB |
Output is correct |
41 |
Correct |
1 ms |
10588 KB |
Output is correct |
42 |
Correct |
2 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10584 KB |
Output is correct |
44 |
Correct |
1 ms |
10588 KB |
Output is correct |
45 |
Correct |
2 ms |
10588 KB |
Output is correct |
46 |
Correct |
1 ms |
10588 KB |
Output is correct |
47 |
Correct |
1 ms |
10588 KB |
Output is correct |
48 |
Correct |
2 ms |
10588 KB |
Output is correct |
49 |
Correct |
2 ms |
10588 KB |
Output is correct |
50 |
Correct |
2 ms |
10588 KB |
Output is correct |
51 |
Correct |
1 ms |
6492 KB |
Output is correct |
52 |
Correct |
1 ms |
8540 KB |
Output is correct |
53 |
Correct |
1 ms |
8540 KB |
Output is correct |
54 |
Correct |
2 ms |
10588 KB |
Output is correct |
55 |
Correct |
2 ms |
10588 KB |
Output is correct |
56 |
Correct |
2 ms |
10588 KB |
Output is correct |
57 |
Correct |
2 ms |
10588 KB |
Output is correct |
58 |
Correct |
3 ms |
18780 KB |
Output is correct |
59 |
Correct |
4 ms |
18780 KB |
Output is correct |
60 |
Correct |
3 ms |
18780 KB |
Output is correct |
61 |
Correct |
3 ms |
18780 KB |
Output is correct |
62 |
Correct |
3 ms |
18776 KB |
Output is correct |
63 |
Correct |
3 ms |
18780 KB |
Output is correct |
64 |
Correct |
3 ms |
18780 KB |
Output is correct |
65 |
Correct |
3 ms |
18780 KB |
Output is correct |
66 |
Correct |
3 ms |
18780 KB |
Output is correct |
67 |
Correct |
3 ms |
18780 KB |
Output is correct |
68 |
Correct |
3 ms |
18780 KB |
Output is correct |
69 |
Correct |
3 ms |
18780 KB |
Output is correct |
70 |
Correct |
3 ms |
18776 KB |
Output is correct |
71 |
Correct |
8 ms |
43612 KB |
Output is correct |
72 |
Correct |
27 ms |
84572 KB |
Output is correct |
73 |
Correct |
20 ms |
84592 KB |
Output is correct |
74 |
Correct |
20 ms |
84576 KB |
Output is correct |
75 |
Correct |
17 ms |
84568 KB |
Output is correct |
76 |
Correct |
17 ms |
84572 KB |
Output is correct |
77 |
Correct |
18 ms |
84572 KB |
Output is correct |
78 |
Correct |
18 ms |
84572 KB |
Output is correct |
79 |
Correct |
25 ms |
84676 KB |
Output is correct |
80 |
Correct |
18 ms |
84728 KB |
Output is correct |
81 |
Correct |
20 ms |
84572 KB |
Output is correct |
82 |
Correct |
23 ms |
84572 KB |
Output is correct |
83 |
Correct |
23 ms |
84560 KB |
Output is correct |
84 |
Correct |
17 ms |
84568 KB |
Output is correct |
85 |
Correct |
51 ms |
134080 KB |
Output is correct |
86 |
Correct |
137 ms |
197652 KB |
Output is correct |
87 |
Correct |
114 ms |
197836 KB |
Output is correct |
88 |
Correct |
97 ms |
197716 KB |
Output is correct |
89 |
Correct |
95 ms |
197712 KB |
Output is correct |
90 |
Correct |
88 ms |
197724 KB |
Output is correct |
91 |
Correct |
165 ms |
197752 KB |
Output is correct |
92 |
Correct |
198 ms |
197464 KB |
Output is correct |
93 |
Correct |
105 ms |
197460 KB |
Output is correct |
94 |
Correct |
116 ms |
197704 KB |
Output is correct |
95 |
Correct |
189 ms |
197712 KB |
Output is correct |
96 |
Correct |
190 ms |
197460 KB |
Output is correct |
97 |
Correct |
87 ms |
197720 KB |
Output is correct |