#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)
pair<int, int> mod={(int)1e9+7, (int)1e9+9};
const int base=311;
struct Hash{
pair<int, int> value;
Hash (int x=0){
value={x%mod.first, x%mod.second};
}
Hash (int x, int y){
value={x, y};
}
Hash operator-(){
return Hash((mod.first-value.first)%mod.first,
(mod.second-value.second)%mod.second);
}
Hash operator+(Hash b){
return Hash((value.first+b.value.first)%mod.first,
(value.second+b.value.second)%mod.second);
}
Hash operator*(Hash b){
return Hash(value.first*b.value.first%mod.first,
value.second*b.value.second%mod.second);
}
bool operator<(Hash b) const {
return value<b.value;
}
bool operator==(Hash b) const {
return value==b.value;
}
};
const int N=2510, LG=12;
int f[N][N], add, cut, paste, n;
int g[N][N];
int jump[LG][N][N];
Hash pf[N];
Hash pw[N];
string s;
Hash get(int l, int r){
return pf[r]+(-pf[l-1])*pw[r-l+1];
}
void precalc(){
pw[0]=1;
for (int i=1; i<=n; ++i){
pw[i]=pw[i-1]*base;
}
for (int i=1; i<=n; ++i){
pf[i]=pf[i-1]*base+s[i];
}
for (int t=1; t<=n; ++t){
map<Hash, int> mp;
for (int r=t; r<=n; ++r){
Hash h1=get(r-t+1, r);
g[r][t]=mp.count(h1)?mp[h1]:0;
if (r-t+1>=t){
Hash h2=get(r-t+1-t+1, r-t+1);
mp[h2]=r-t+1;
}
}
}
for (int i=0; i<=n; ++i) for (int j=0; j<LG; ++j) jump[j][0][i]=0;
for (int i=1; i<=n; ++i){
for (int j=1; j<=i; ++j) jump[0][i][j]=g[i][j];
}
for (int k=1; k<LG; ++k){
for (int i=1; i<=n; ++i){
for (int j=1; j<=i; ++j) jump[k][i][j]=jump[k-1][jump[k-1][i][j]][j];
}
}
}
int count(int l, int r, int t){
int ans=0;
for (int i=LG-1; i>=0; --i){
if (jump[i][r][t]>=l+t-1){
ans+=1<<i;
r=jump[i][r][t];
}
}
return ans+1;
}
int dp(int l, int r){
if (l>r) return 0;
if (f[l][r]!=-1) return f[l][r];
int res=dp(l, r-1)+add;
for (int t=r; t>=l; --t){
int cnt=count(l, r, r-t+1);
if (cnt==1) break;
int ll=max(l, r-(r-l+1)/cnt+1), rr=t-1;
while (ll<=rr){
int mid=(ll+rr)>>1;
if (count(l, r, r-mid+1)==cnt) rr=mid-1;
else ll=mid+1;
}
t=ll;
res=min(res, dp(t, r)+cut+cnt*paste+add*(r-l+1-cnt*(r-t+1)));
}
return f[l][r]=res;
}
void solve(){
memset(f, -1, sizeof f);
cin >> n >> s;
s=" "+s;
precalc();
cin >> add >> cut >> paste;
cout << dp(1, n);
}
int32_t main(){
#ifdef sus
freopen("cf.inp", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
50776 KB |
Output is correct |
2 |
Correct |
12 ms |
50780 KB |
Output is correct |
3 |
Correct |
12 ms |
50820 KB |
Output is correct |
4 |
Correct |
12 ms |
50780 KB |
Output is correct |
5 |
Correct |
12 ms |
50688 KB |
Output is correct |
6 |
Correct |
12 ms |
50768 KB |
Output is correct |
7 |
Correct |
12 ms |
50780 KB |
Output is correct |
8 |
Correct |
13 ms |
50768 KB |
Output is correct |
9 |
Correct |
13 ms |
50780 KB |
Output is correct |
10 |
Correct |
12 ms |
51032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
51548 KB |
Output is correct |
2 |
Correct |
11 ms |
50860 KB |
Output is correct |
3 |
Correct |
1680 ms |
316896 KB |
Output is correct |
4 |
Correct |
2163 ms |
353316 KB |
Output is correct |
5 |
Correct |
2859 ms |
394444 KB |
Output is correct |
6 |
Execution timed out |
3035 ms |
438452 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
50776 KB |
Output is correct |
2 |
Correct |
12 ms |
50780 KB |
Output is correct |
3 |
Correct |
12 ms |
50820 KB |
Output is correct |
4 |
Correct |
12 ms |
50780 KB |
Output is correct |
5 |
Correct |
12 ms |
50688 KB |
Output is correct |
6 |
Correct |
12 ms |
50768 KB |
Output is correct |
7 |
Correct |
12 ms |
50780 KB |
Output is correct |
8 |
Correct |
13 ms |
50768 KB |
Output is correct |
9 |
Correct |
13 ms |
50780 KB |
Output is correct |
10 |
Correct |
12 ms |
51032 KB |
Output is correct |
11 |
Correct |
12 ms |
51288 KB |
Output is correct |
12 |
Correct |
12 ms |
51460 KB |
Output is correct |
13 |
Correct |
12 ms |
51548 KB |
Output is correct |
14 |
Correct |
13 ms |
51152 KB |
Output is correct |
15 |
Correct |
13 ms |
51544 KB |
Output is correct |
16 |
Correct |
14 ms |
51292 KB |
Output is correct |
17 |
Correct |
12 ms |
51036 KB |
Output is correct |
18 |
Correct |
12 ms |
50780 KB |
Output is correct |
19 |
Correct |
12 ms |
51292 KB |
Output is correct |
20 |
Correct |
12 ms |
50780 KB |
Output is correct |
21 |
Correct |
13 ms |
52316 KB |
Output is correct |
22 |
Correct |
12 ms |
52304 KB |
Output is correct |
23 |
Correct |
13 ms |
52272 KB |
Output is correct |
24 |
Correct |
13 ms |
52312 KB |
Output is correct |
25 |
Correct |
12 ms |
52316 KB |
Output is correct |
26 |
Correct |
12 ms |
52148 KB |
Output is correct |
27 |
Correct |
14 ms |
52568 KB |
Output is correct |
28 |
Correct |
16 ms |
52316 KB |
Output is correct |
29 |
Correct |
14 ms |
52316 KB |
Output is correct |
30 |
Correct |
13 ms |
52184 KB |
Output is correct |
31 |
Correct |
14 ms |
52316 KB |
Output is correct |
32 |
Correct |
14 ms |
52180 KB |
Output is correct |
33 |
Correct |
13 ms |
52316 KB |
Output is correct |
34 |
Correct |
14 ms |
50640 KB |
Output is correct |
35 |
Correct |
13 ms |
50804 KB |
Output is correct |
36 |
Correct |
13 ms |
50780 KB |
Output is correct |
37 |
Correct |
12 ms |
51032 KB |
Output is correct |
38 |
Correct |
12 ms |
51292 KB |
Output is correct |
39 |
Correct |
13 ms |
52316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
50776 KB |
Output is correct |
2 |
Correct |
12 ms |
50780 KB |
Output is correct |
3 |
Correct |
12 ms |
50820 KB |
Output is correct |
4 |
Correct |
12 ms |
50780 KB |
Output is correct |
5 |
Correct |
12 ms |
50688 KB |
Output is correct |
6 |
Correct |
12 ms |
50768 KB |
Output is correct |
7 |
Correct |
12 ms |
50780 KB |
Output is correct |
8 |
Correct |
13 ms |
50768 KB |
Output is correct |
9 |
Correct |
13 ms |
50780 KB |
Output is correct |
10 |
Correct |
12 ms |
51032 KB |
Output is correct |
11 |
Correct |
12 ms |
51288 KB |
Output is correct |
12 |
Correct |
12 ms |
51460 KB |
Output is correct |
13 |
Correct |
12 ms |
51548 KB |
Output is correct |
14 |
Correct |
13 ms |
51152 KB |
Output is correct |
15 |
Correct |
13 ms |
51544 KB |
Output is correct |
16 |
Correct |
14 ms |
51292 KB |
Output is correct |
17 |
Correct |
12 ms |
51036 KB |
Output is correct |
18 |
Correct |
12 ms |
50780 KB |
Output is correct |
19 |
Correct |
12 ms |
51292 KB |
Output is correct |
20 |
Correct |
12 ms |
50780 KB |
Output is correct |
21 |
Correct |
13 ms |
52316 KB |
Output is correct |
22 |
Correct |
12 ms |
52304 KB |
Output is correct |
23 |
Correct |
13 ms |
52272 KB |
Output is correct |
24 |
Correct |
13 ms |
52312 KB |
Output is correct |
25 |
Correct |
12 ms |
52316 KB |
Output is correct |
26 |
Correct |
12 ms |
52148 KB |
Output is correct |
27 |
Correct |
14 ms |
52568 KB |
Output is correct |
28 |
Correct |
16 ms |
52316 KB |
Output is correct |
29 |
Correct |
14 ms |
52316 KB |
Output is correct |
30 |
Correct |
13 ms |
52184 KB |
Output is correct |
31 |
Correct |
14 ms |
52316 KB |
Output is correct |
32 |
Correct |
14 ms |
52180 KB |
Output is correct |
33 |
Correct |
13 ms |
52316 KB |
Output is correct |
34 |
Correct |
14 ms |
50640 KB |
Output is correct |
35 |
Correct |
13 ms |
50804 KB |
Output is correct |
36 |
Correct |
13 ms |
50780 KB |
Output is correct |
37 |
Correct |
12 ms |
51032 KB |
Output is correct |
38 |
Correct |
12 ms |
51292 KB |
Output is correct |
39 |
Correct |
13 ms |
52316 KB |
Output is correct |
40 |
Correct |
14 ms |
55348 KB |
Output is correct |
41 |
Correct |
20 ms |
63224 KB |
Output is correct |
42 |
Correct |
19 ms |
63064 KB |
Output is correct |
43 |
Correct |
20 ms |
63068 KB |
Output is correct |
44 |
Correct |
22 ms |
63064 KB |
Output is correct |
45 |
Correct |
20 ms |
63056 KB |
Output is correct |
46 |
Correct |
19 ms |
63068 KB |
Output is correct |
47 |
Correct |
19 ms |
63232 KB |
Output is correct |
48 |
Correct |
20 ms |
63184 KB |
Output is correct |
49 |
Correct |
19 ms |
63064 KB |
Output is correct |
50 |
Correct |
19 ms |
63072 KB |
Output is correct |
51 |
Correct |
20 ms |
63088 KB |
Output is correct |
52 |
Correct |
20 ms |
63232 KB |
Output is correct |
53 |
Correct |
19 ms |
63068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
50776 KB |
Output is correct |
2 |
Correct |
12 ms |
50780 KB |
Output is correct |
3 |
Correct |
12 ms |
50820 KB |
Output is correct |
4 |
Correct |
12 ms |
50780 KB |
Output is correct |
5 |
Correct |
12 ms |
50688 KB |
Output is correct |
6 |
Correct |
12 ms |
50768 KB |
Output is correct |
7 |
Correct |
12 ms |
50780 KB |
Output is correct |
8 |
Correct |
13 ms |
50768 KB |
Output is correct |
9 |
Correct |
13 ms |
50780 KB |
Output is correct |
10 |
Correct |
12 ms |
51032 KB |
Output is correct |
11 |
Correct |
12 ms |
51288 KB |
Output is correct |
12 |
Correct |
12 ms |
51460 KB |
Output is correct |
13 |
Correct |
12 ms |
51548 KB |
Output is correct |
14 |
Correct |
13 ms |
51152 KB |
Output is correct |
15 |
Correct |
13 ms |
51544 KB |
Output is correct |
16 |
Correct |
14 ms |
51292 KB |
Output is correct |
17 |
Correct |
12 ms |
51036 KB |
Output is correct |
18 |
Correct |
12 ms |
50780 KB |
Output is correct |
19 |
Correct |
12 ms |
51292 KB |
Output is correct |
20 |
Correct |
12 ms |
50780 KB |
Output is correct |
21 |
Correct |
13 ms |
52316 KB |
Output is correct |
22 |
Correct |
12 ms |
52304 KB |
Output is correct |
23 |
Correct |
13 ms |
52272 KB |
Output is correct |
24 |
Correct |
13 ms |
52312 KB |
Output is correct |
25 |
Correct |
12 ms |
52316 KB |
Output is correct |
26 |
Correct |
12 ms |
52148 KB |
Output is correct |
27 |
Correct |
14 ms |
52568 KB |
Output is correct |
28 |
Correct |
16 ms |
52316 KB |
Output is correct |
29 |
Correct |
14 ms |
52316 KB |
Output is correct |
30 |
Correct |
13 ms |
52184 KB |
Output is correct |
31 |
Correct |
14 ms |
52316 KB |
Output is correct |
32 |
Correct |
14 ms |
52180 KB |
Output is correct |
33 |
Correct |
13 ms |
52316 KB |
Output is correct |
34 |
Correct |
14 ms |
50640 KB |
Output is correct |
35 |
Correct |
13 ms |
50804 KB |
Output is correct |
36 |
Correct |
13 ms |
50780 KB |
Output is correct |
37 |
Correct |
12 ms |
51032 KB |
Output is correct |
38 |
Correct |
12 ms |
51292 KB |
Output is correct |
39 |
Correct |
13 ms |
52316 KB |
Output is correct |
40 |
Correct |
14 ms |
55348 KB |
Output is correct |
41 |
Correct |
20 ms |
63224 KB |
Output is correct |
42 |
Correct |
19 ms |
63064 KB |
Output is correct |
43 |
Correct |
20 ms |
63068 KB |
Output is correct |
44 |
Correct |
22 ms |
63064 KB |
Output is correct |
45 |
Correct |
20 ms |
63056 KB |
Output is correct |
46 |
Correct |
19 ms |
63068 KB |
Output is correct |
47 |
Correct |
19 ms |
63232 KB |
Output is correct |
48 |
Correct |
20 ms |
63184 KB |
Output is correct |
49 |
Correct |
19 ms |
63064 KB |
Output is correct |
50 |
Correct |
19 ms |
63072 KB |
Output is correct |
51 |
Correct |
20 ms |
63088 KB |
Output is correct |
52 |
Correct |
20 ms |
63232 KB |
Output is correct |
53 |
Correct |
19 ms |
63068 KB |
Output is correct |
54 |
Correct |
37 ms |
84376 KB |
Output is correct |
55 |
Correct |
297 ms |
154284 KB |
Output is correct |
56 |
Correct |
120 ms |
153984 KB |
Output is correct |
57 |
Correct |
119 ms |
153980 KB |
Output is correct |
58 |
Correct |
119 ms |
154044 KB |
Output is correct |
59 |
Correct |
122 ms |
154164 KB |
Output is correct |
60 |
Correct |
119 ms |
154452 KB |
Output is correct |
61 |
Correct |
143 ms |
153936 KB |
Output is correct |
62 |
Correct |
246 ms |
153928 KB |
Output is correct |
63 |
Correct |
119 ms |
154280 KB |
Output is correct |
64 |
Correct |
114 ms |
153940 KB |
Output is correct |
65 |
Correct |
238 ms |
154156 KB |
Output is correct |
66 |
Correct |
243 ms |
153960 KB |
Output is correct |
67 |
Correct |
134 ms |
154036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
50776 KB |
Output is correct |
2 |
Correct |
12 ms |
50780 KB |
Output is correct |
3 |
Correct |
12 ms |
50820 KB |
Output is correct |
4 |
Correct |
12 ms |
50780 KB |
Output is correct |
5 |
Correct |
12 ms |
50688 KB |
Output is correct |
6 |
Correct |
12 ms |
50768 KB |
Output is correct |
7 |
Correct |
12 ms |
50780 KB |
Output is correct |
8 |
Correct |
13 ms |
50768 KB |
Output is correct |
9 |
Correct |
13 ms |
50780 KB |
Output is correct |
10 |
Correct |
12 ms |
51032 KB |
Output is correct |
11 |
Correct |
12 ms |
51548 KB |
Output is correct |
12 |
Correct |
11 ms |
50860 KB |
Output is correct |
13 |
Correct |
1680 ms |
316896 KB |
Output is correct |
14 |
Correct |
2163 ms |
353316 KB |
Output is correct |
15 |
Correct |
2859 ms |
394444 KB |
Output is correct |
16 |
Execution timed out |
3035 ms |
438452 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |