#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;
int f[N][N], add, cut, paste, 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 solve(){
cin >> n >> s;
s=" "+s;
cin >> add >> cut >> paste;
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];
}
memset(f, 0x3f, sizeof f);
for (int len=1; len<=n; ++len){
map<pair<int, int>, vector<int>> mp;
for (int r=len; r<=n; ++r){
int l=r-len+1;
mp[get(l, r).value].push_back(l);
f[l][r]=min({f[l][r], f[l][r-1]+add, f[l+1][r]+add});
if (l==r) f[l][r]=add;
}
vector<int> jump(n+1, -1);
for (auto &tmp:mp){
vector<int> &v=tmp.second;
for (int i=0, j=0; i<isz(v); ++i){
while (j<isz(v) && v[j]-v[i]<len) ++j;
jump[v[i]]=j==isz(v)?-1:v[j];
}
}
for (int i=1; i<=n; ++i){
int j=jump[i], cnt=2;
while (j!=-1){
f[i][j+len-1]=min(f[i][j+len-1], f[i][i+len-1]+cut+cnt*paste+add*(j+len-i-cnt*len));
j=jump[j];
++cnt;
}
}
}
cout << f[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 |
9 ms |
49756 KB |
Output is correct |
2 |
Correct |
8 ms |
49756 KB |
Output is correct |
3 |
Correct |
8 ms |
49812 KB |
Output is correct |
4 |
Correct |
8 ms |
49752 KB |
Output is correct |
5 |
Correct |
8 ms |
49752 KB |
Output is correct |
6 |
Correct |
8 ms |
49752 KB |
Output is correct |
7 |
Correct |
8 ms |
49756 KB |
Output is correct |
8 |
Correct |
7 ms |
49756 KB |
Output is correct |
9 |
Correct |
8 ms |
49756 KB |
Output is correct |
10 |
Correct |
7 ms |
49836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
49752 KB |
Output is correct |
2 |
Correct |
8 ms |
49756 KB |
Output is correct |
3 |
Correct |
152 ms |
49872 KB |
Output is correct |
4 |
Correct |
181 ms |
49872 KB |
Output is correct |
5 |
Correct |
227 ms |
49756 KB |
Output is correct |
6 |
Correct |
251 ms |
49756 KB |
Output is correct |
7 |
Correct |
320 ms |
49892 KB |
Output is correct |
8 |
Correct |
303 ms |
49752 KB |
Output is correct |
9 |
Correct |
320 ms |
49892 KB |
Output is correct |
10 |
Correct |
8 ms |
49756 KB |
Output is correct |
11 |
Correct |
7 ms |
49824 KB |
Output is correct |
12 |
Correct |
7 ms |
49616 KB |
Output is correct |
13 |
Correct |
8 ms |
49756 KB |
Output is correct |
14 |
Correct |
7 ms |
49824 KB |
Output is correct |
15 |
Correct |
7 ms |
49752 KB |
Output is correct |
16 |
Correct |
8 ms |
49756 KB |
Output is correct |
17 |
Correct |
7 ms |
49756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
49756 KB |
Output is correct |
2 |
Correct |
8 ms |
49756 KB |
Output is correct |
3 |
Correct |
8 ms |
49812 KB |
Output is correct |
4 |
Correct |
8 ms |
49752 KB |
Output is correct |
5 |
Correct |
8 ms |
49752 KB |
Output is correct |
6 |
Correct |
8 ms |
49752 KB |
Output is correct |
7 |
Correct |
8 ms |
49756 KB |
Output is correct |
8 |
Correct |
7 ms |
49756 KB |
Output is correct |
9 |
Correct |
8 ms |
49756 KB |
Output is correct |
10 |
Correct |
7 ms |
49836 KB |
Output is correct |
11 |
Correct |
7 ms |
49756 KB |
Output is correct |
12 |
Correct |
8 ms |
49756 KB |
Output is correct |
13 |
Correct |
7 ms |
49816 KB |
Output is correct |
14 |
Correct |
8 ms |
49756 KB |
Output is correct |
15 |
Correct |
7 ms |
49832 KB |
Output is correct |
16 |
Correct |
8 ms |
49756 KB |
Output is correct |
17 |
Correct |
7 ms |
49604 KB |
Output is correct |
18 |
Correct |
8 ms |
49752 KB |
Output is correct |
19 |
Correct |
8 ms |
49752 KB |
Output is correct |
20 |
Correct |
7 ms |
49756 KB |
Output is correct |
21 |
Correct |
7 ms |
49756 KB |
Output is correct |
22 |
Correct |
8 ms |
49852 KB |
Output is correct |
23 |
Correct |
8 ms |
49756 KB |
Output is correct |
24 |
Correct |
7 ms |
49756 KB |
Output is correct |
25 |
Correct |
8 ms |
49756 KB |
Output is correct |
26 |
Correct |
7 ms |
49756 KB |
Output is correct |
27 |
Correct |
8 ms |
49752 KB |
Output is correct |
28 |
Correct |
7 ms |
49756 KB |
Output is correct |
29 |
Correct |
8 ms |
49756 KB |
Output is correct |
30 |
Correct |
7 ms |
49756 KB |
Output is correct |
31 |
Correct |
8 ms |
49820 KB |
Output is correct |
32 |
Correct |
8 ms |
49756 KB |
Output is correct |
33 |
Correct |
7 ms |
49756 KB |
Output is correct |
34 |
Correct |
8 ms |
49816 KB |
Output is correct |
35 |
Correct |
8 ms |
49756 KB |
Output is correct |
36 |
Correct |
8 ms |
49756 KB |
Output is correct |
37 |
Correct |
7 ms |
49756 KB |
Output is correct |
38 |
Correct |
8 ms |
49824 KB |
Output is correct |
39 |
Correct |
7 ms |
49756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
49756 KB |
Output is correct |
2 |
Correct |
8 ms |
49756 KB |
Output is correct |
3 |
Correct |
8 ms |
49812 KB |
Output is correct |
4 |
Correct |
8 ms |
49752 KB |
Output is correct |
5 |
Correct |
8 ms |
49752 KB |
Output is correct |
6 |
Correct |
8 ms |
49752 KB |
Output is correct |
7 |
Correct |
8 ms |
49756 KB |
Output is correct |
8 |
Correct |
7 ms |
49756 KB |
Output is correct |
9 |
Correct |
8 ms |
49756 KB |
Output is correct |
10 |
Correct |
7 ms |
49836 KB |
Output is correct |
11 |
Correct |
7 ms |
49756 KB |
Output is correct |
12 |
Correct |
8 ms |
49756 KB |
Output is correct |
13 |
Correct |
7 ms |
49816 KB |
Output is correct |
14 |
Correct |
8 ms |
49756 KB |
Output is correct |
15 |
Correct |
7 ms |
49832 KB |
Output is correct |
16 |
Correct |
8 ms |
49756 KB |
Output is correct |
17 |
Correct |
7 ms |
49604 KB |
Output is correct |
18 |
Correct |
8 ms |
49752 KB |
Output is correct |
19 |
Correct |
8 ms |
49752 KB |
Output is correct |
20 |
Correct |
7 ms |
49756 KB |
Output is correct |
21 |
Correct |
7 ms |
49756 KB |
Output is correct |
22 |
Correct |
8 ms |
49852 KB |
Output is correct |
23 |
Correct |
8 ms |
49756 KB |
Output is correct |
24 |
Correct |
7 ms |
49756 KB |
Output is correct |
25 |
Correct |
8 ms |
49756 KB |
Output is correct |
26 |
Correct |
7 ms |
49756 KB |
Output is correct |
27 |
Correct |
8 ms |
49752 KB |
Output is correct |
28 |
Correct |
7 ms |
49756 KB |
Output is correct |
29 |
Correct |
8 ms |
49756 KB |
Output is correct |
30 |
Correct |
7 ms |
49756 KB |
Output is correct |
31 |
Correct |
8 ms |
49820 KB |
Output is correct |
32 |
Correct |
8 ms |
49756 KB |
Output is correct |
33 |
Correct |
7 ms |
49756 KB |
Output is correct |
34 |
Correct |
8 ms |
49816 KB |
Output is correct |
35 |
Correct |
8 ms |
49756 KB |
Output is correct |
36 |
Correct |
8 ms |
49756 KB |
Output is correct |
37 |
Correct |
7 ms |
49756 KB |
Output is correct |
38 |
Correct |
8 ms |
49824 KB |
Output is correct |
39 |
Correct |
7 ms |
49756 KB |
Output is correct |
40 |
Correct |
8 ms |
49756 KB |
Output is correct |
41 |
Correct |
8 ms |
49756 KB |
Output is correct |
42 |
Correct |
10 ms |
50008 KB |
Output is correct |
43 |
Correct |
11 ms |
49752 KB |
Output is correct |
44 |
Correct |
12 ms |
49756 KB |
Output is correct |
45 |
Correct |
11 ms |
49756 KB |
Output is correct |
46 |
Correct |
11 ms |
49856 KB |
Output is correct |
47 |
Correct |
10 ms |
49756 KB |
Output is correct |
48 |
Correct |
10 ms |
50008 KB |
Output is correct |
49 |
Correct |
11 ms |
49756 KB |
Output is correct |
50 |
Correct |
10 ms |
49868 KB |
Output is correct |
51 |
Correct |
11 ms |
49756 KB |
Output is correct |
52 |
Correct |
10 ms |
49840 KB |
Output is correct |
53 |
Correct |
12 ms |
49756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
49756 KB |
Output is correct |
2 |
Correct |
8 ms |
49756 KB |
Output is correct |
3 |
Correct |
8 ms |
49812 KB |
Output is correct |
4 |
Correct |
8 ms |
49752 KB |
Output is correct |
5 |
Correct |
8 ms |
49752 KB |
Output is correct |
6 |
Correct |
8 ms |
49752 KB |
Output is correct |
7 |
Correct |
8 ms |
49756 KB |
Output is correct |
8 |
Correct |
7 ms |
49756 KB |
Output is correct |
9 |
Correct |
8 ms |
49756 KB |
Output is correct |
10 |
Correct |
7 ms |
49836 KB |
Output is correct |
11 |
Correct |
7 ms |
49756 KB |
Output is correct |
12 |
Correct |
8 ms |
49756 KB |
Output is correct |
13 |
Correct |
7 ms |
49816 KB |
Output is correct |
14 |
Correct |
8 ms |
49756 KB |
Output is correct |
15 |
Correct |
7 ms |
49832 KB |
Output is correct |
16 |
Correct |
8 ms |
49756 KB |
Output is correct |
17 |
Correct |
7 ms |
49604 KB |
Output is correct |
18 |
Correct |
8 ms |
49752 KB |
Output is correct |
19 |
Correct |
8 ms |
49752 KB |
Output is correct |
20 |
Correct |
7 ms |
49756 KB |
Output is correct |
21 |
Correct |
7 ms |
49756 KB |
Output is correct |
22 |
Correct |
8 ms |
49852 KB |
Output is correct |
23 |
Correct |
8 ms |
49756 KB |
Output is correct |
24 |
Correct |
7 ms |
49756 KB |
Output is correct |
25 |
Correct |
8 ms |
49756 KB |
Output is correct |
26 |
Correct |
7 ms |
49756 KB |
Output is correct |
27 |
Correct |
8 ms |
49752 KB |
Output is correct |
28 |
Correct |
7 ms |
49756 KB |
Output is correct |
29 |
Correct |
8 ms |
49756 KB |
Output is correct |
30 |
Correct |
7 ms |
49756 KB |
Output is correct |
31 |
Correct |
8 ms |
49820 KB |
Output is correct |
32 |
Correct |
8 ms |
49756 KB |
Output is correct |
33 |
Correct |
7 ms |
49756 KB |
Output is correct |
34 |
Correct |
8 ms |
49816 KB |
Output is correct |
35 |
Correct |
8 ms |
49756 KB |
Output is correct |
36 |
Correct |
8 ms |
49756 KB |
Output is correct |
37 |
Correct |
7 ms |
49756 KB |
Output is correct |
38 |
Correct |
8 ms |
49824 KB |
Output is correct |
39 |
Correct |
7 ms |
49756 KB |
Output is correct |
40 |
Correct |
8 ms |
49756 KB |
Output is correct |
41 |
Correct |
8 ms |
49756 KB |
Output is correct |
42 |
Correct |
10 ms |
50008 KB |
Output is correct |
43 |
Correct |
11 ms |
49752 KB |
Output is correct |
44 |
Correct |
12 ms |
49756 KB |
Output is correct |
45 |
Correct |
11 ms |
49756 KB |
Output is correct |
46 |
Correct |
11 ms |
49856 KB |
Output is correct |
47 |
Correct |
10 ms |
49756 KB |
Output is correct |
48 |
Correct |
10 ms |
50008 KB |
Output is correct |
49 |
Correct |
11 ms |
49756 KB |
Output is correct |
50 |
Correct |
10 ms |
49868 KB |
Output is correct |
51 |
Correct |
11 ms |
49756 KB |
Output is correct |
52 |
Correct |
10 ms |
49840 KB |
Output is correct |
53 |
Correct |
12 ms |
49756 KB |
Output is correct |
54 |
Correct |
26 ms |
49756 KB |
Output is correct |
55 |
Correct |
35 ms |
49876 KB |
Output is correct |
56 |
Correct |
106 ms |
49960 KB |
Output is correct |
57 |
Correct |
100 ms |
49952 KB |
Output is correct |
58 |
Correct |
99 ms |
49956 KB |
Output is correct |
59 |
Correct |
97 ms |
49756 KB |
Output is correct |
60 |
Correct |
97 ms |
49956 KB |
Output is correct |
61 |
Correct |
81 ms |
49752 KB |
Output is correct |
62 |
Correct |
34 ms |
49752 KB |
Output is correct |
63 |
Correct |
98 ms |
49904 KB |
Output is correct |
64 |
Correct |
91 ms |
49756 KB |
Output is correct |
65 |
Correct |
83 ms |
49756 KB |
Output is correct |
66 |
Correct |
80 ms |
49756 KB |
Output is correct |
67 |
Correct |
97 ms |
49756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
49756 KB |
Output is correct |
2 |
Correct |
8 ms |
49756 KB |
Output is correct |
3 |
Correct |
8 ms |
49812 KB |
Output is correct |
4 |
Correct |
8 ms |
49752 KB |
Output is correct |
5 |
Correct |
8 ms |
49752 KB |
Output is correct |
6 |
Correct |
8 ms |
49752 KB |
Output is correct |
7 |
Correct |
8 ms |
49756 KB |
Output is correct |
8 |
Correct |
7 ms |
49756 KB |
Output is correct |
9 |
Correct |
8 ms |
49756 KB |
Output is correct |
10 |
Correct |
7 ms |
49836 KB |
Output is correct |
11 |
Correct |
8 ms |
49752 KB |
Output is correct |
12 |
Correct |
8 ms |
49756 KB |
Output is correct |
13 |
Correct |
152 ms |
49872 KB |
Output is correct |
14 |
Correct |
181 ms |
49872 KB |
Output is correct |
15 |
Correct |
227 ms |
49756 KB |
Output is correct |
16 |
Correct |
251 ms |
49756 KB |
Output is correct |
17 |
Correct |
320 ms |
49892 KB |
Output is correct |
18 |
Correct |
303 ms |
49752 KB |
Output is correct |
19 |
Correct |
320 ms |
49892 KB |
Output is correct |
20 |
Correct |
8 ms |
49756 KB |
Output is correct |
21 |
Correct |
7 ms |
49824 KB |
Output is correct |
22 |
Correct |
7 ms |
49616 KB |
Output is correct |
23 |
Correct |
8 ms |
49756 KB |
Output is correct |
24 |
Correct |
7 ms |
49824 KB |
Output is correct |
25 |
Correct |
7 ms |
49752 KB |
Output is correct |
26 |
Correct |
8 ms |
49756 KB |
Output is correct |
27 |
Correct |
7 ms |
49756 KB |
Output is correct |
28 |
Correct |
7 ms |
49756 KB |
Output is correct |
29 |
Correct |
8 ms |
49756 KB |
Output is correct |
30 |
Correct |
7 ms |
49816 KB |
Output is correct |
31 |
Correct |
8 ms |
49756 KB |
Output is correct |
32 |
Correct |
7 ms |
49832 KB |
Output is correct |
33 |
Correct |
8 ms |
49756 KB |
Output is correct |
34 |
Correct |
7 ms |
49604 KB |
Output is correct |
35 |
Correct |
8 ms |
49752 KB |
Output is correct |
36 |
Correct |
8 ms |
49752 KB |
Output is correct |
37 |
Correct |
7 ms |
49756 KB |
Output is correct |
38 |
Correct |
7 ms |
49756 KB |
Output is correct |
39 |
Correct |
8 ms |
49852 KB |
Output is correct |
40 |
Correct |
8 ms |
49756 KB |
Output is correct |
41 |
Correct |
7 ms |
49756 KB |
Output is correct |
42 |
Correct |
8 ms |
49756 KB |
Output is correct |
43 |
Correct |
7 ms |
49756 KB |
Output is correct |
44 |
Correct |
8 ms |
49752 KB |
Output is correct |
45 |
Correct |
7 ms |
49756 KB |
Output is correct |
46 |
Correct |
8 ms |
49756 KB |
Output is correct |
47 |
Correct |
7 ms |
49756 KB |
Output is correct |
48 |
Correct |
8 ms |
49820 KB |
Output is correct |
49 |
Correct |
8 ms |
49756 KB |
Output is correct |
50 |
Correct |
7 ms |
49756 KB |
Output is correct |
51 |
Correct |
8 ms |
49816 KB |
Output is correct |
52 |
Correct |
8 ms |
49756 KB |
Output is correct |
53 |
Correct |
8 ms |
49756 KB |
Output is correct |
54 |
Correct |
7 ms |
49756 KB |
Output is correct |
55 |
Correct |
8 ms |
49824 KB |
Output is correct |
56 |
Correct |
7 ms |
49756 KB |
Output is correct |
57 |
Correct |
8 ms |
49756 KB |
Output is correct |
58 |
Correct |
8 ms |
49756 KB |
Output is correct |
59 |
Correct |
10 ms |
50008 KB |
Output is correct |
60 |
Correct |
11 ms |
49752 KB |
Output is correct |
61 |
Correct |
12 ms |
49756 KB |
Output is correct |
62 |
Correct |
11 ms |
49756 KB |
Output is correct |
63 |
Correct |
11 ms |
49856 KB |
Output is correct |
64 |
Correct |
10 ms |
49756 KB |
Output is correct |
65 |
Correct |
10 ms |
50008 KB |
Output is correct |
66 |
Correct |
11 ms |
49756 KB |
Output is correct |
67 |
Correct |
10 ms |
49868 KB |
Output is correct |
68 |
Correct |
11 ms |
49756 KB |
Output is correct |
69 |
Correct |
10 ms |
49840 KB |
Output is correct |
70 |
Correct |
12 ms |
49756 KB |
Output is correct |
71 |
Correct |
26 ms |
49756 KB |
Output is correct |
72 |
Correct |
35 ms |
49876 KB |
Output is correct |
73 |
Correct |
106 ms |
49960 KB |
Output is correct |
74 |
Correct |
100 ms |
49952 KB |
Output is correct |
75 |
Correct |
99 ms |
49956 KB |
Output is correct |
76 |
Correct |
97 ms |
49756 KB |
Output is correct |
77 |
Correct |
97 ms |
49956 KB |
Output is correct |
78 |
Correct |
81 ms |
49752 KB |
Output is correct |
79 |
Correct |
34 ms |
49752 KB |
Output is correct |
80 |
Correct |
98 ms |
49904 KB |
Output is correct |
81 |
Correct |
91 ms |
49756 KB |
Output is correct |
82 |
Correct |
83 ms |
49756 KB |
Output is correct |
83 |
Correct |
80 ms |
49756 KB |
Output is correct |
84 |
Correct |
97 ms |
49756 KB |
Output is correct |
85 |
Correct |
254 ms |
50044 KB |
Output is correct |
86 |
Correct |
657 ms |
50012 KB |
Output is correct |
87 |
Correct |
658 ms |
50392 KB |
Output is correct |
88 |
Correct |
637 ms |
50160 KB |
Output is correct |
89 |
Correct |
643 ms |
50148 KB |
Output is correct |
90 |
Correct |
652 ms |
50008 KB |
Output is correct |
91 |
Correct |
304 ms |
49924 KB |
Output is correct |
92 |
Correct |
258 ms |
49756 KB |
Output is correct |
93 |
Correct |
570 ms |
50000 KB |
Output is correct |
94 |
Correct |
589 ms |
50260 KB |
Output is correct |
95 |
Correct |
580 ms |
49756 KB |
Output is correct |
96 |
Correct |
576 ms |
49924 KB |
Output is correct |
97 |
Correct |
647 ms |
50260 KB |
Output is correct |