#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include<bits/stdc++.h>
#define int long long
#define quick ios::sync_with_stdio(0);cin.tie(0);
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define repd(x,a,b) for(int x=a;x>=b;x--)
#define lowbit(x) (x&-x)
#define sz(x) (int)(x.size())
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define mp make_pair
#define eb emplace_back
using namespace std;
typedef pair<int,int> pii;
void debug(){
cout<<"\n";
}
template <class T,class ... U >
void debug(T a, U ... b){
cout<<a<<" ",debug(b...);
}
const int N=25e2+7;
const int INF=1e18;
const int p=29;
const int Mod=462'779'347'772'851;
int pref[N][N];
int dp[N][N];
string s;
void build(int n){
rep(i,1,n){
rep(j,i,n){
pref[i][j]=pref[i][j-1]*p%Mod+s[j-1]-'a'+2;
if(pref[i][j]>=Mod) pref[i][j]-=Mod;
}
}
}
int nxt[N];
signed main(){
quick
int n;
cin>>n;
cin>>s;
build(n);
int a,b,c;
cin>>a>>b>>c;
map<int,vector<int> > mx;
rep(i,1,n){
rep(j,i,n){
dp[i][j]=(j-i+1)*a;
mx[pref[i][j]].eb(i);
}
}
map<int,bool> vis;
rep(L,1,n){
fill(nxt,nxt+n+1,-1);
rep(l,1,n-L+1){
int r=l+L-1;
int qx=pref[l][r];
int n2=sz(mx[qx]);
if(l<r) dp[l][r]=min({dp[l][r],dp[l][r-1]+a,dp[l+1][r]+a});
if(!vis[qx]&&n2!=1){
auto _r=mx[qx].begin();
auto E=mx[qx].end();
for(auto i=mx[qx].begin();i!=E;++i){
while(_r!=E&&(*_r)<=(*i)+L-1) _r++;
nxt[*i]=(_r==E ? -1 : *_r);
}
vis[qx]=true;
}
int px=nxt[l];
int pst=1;
while(px!=-1){
dp[l][px+L-1]=min(dp[l][px+L-1],(px-l-pst*L)*a+pst*c+b+c+dp[l][r]);
pst++;
px=nxt[px];
}
}
}
cout<<dp[1][n]<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
294 ms |
59120 KB |
Output is correct |
4 |
Correct |
356 ms |
67000 KB |
Output is correct |
5 |
Correct |
427 ms |
77512 KB |
Output is correct |
6 |
Correct |
499 ms |
88560 KB |
Output is correct |
7 |
Correct |
590 ms |
101424 KB |
Output is correct |
8 |
Correct |
641 ms |
101420 KB |
Output is correct |
9 |
Correct |
597 ms |
101352 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
644 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
644 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
1532 KB |
Output is correct |
41 |
Correct |
3 ms |
2528 KB |
Output is correct |
42 |
Correct |
17 ms |
5500 KB |
Output is correct |
43 |
Correct |
19 ms |
5588 KB |
Output is correct |
44 |
Correct |
15 ms |
5532 KB |
Output is correct |
45 |
Correct |
18 ms |
5632 KB |
Output is correct |
46 |
Correct |
16 ms |
5588 KB |
Output is correct |
47 |
Correct |
12 ms |
3256 KB |
Output is correct |
48 |
Correct |
16 ms |
4244 KB |
Output is correct |
49 |
Correct |
16 ms |
4772 KB |
Output is correct |
50 |
Correct |
16 ms |
4820 KB |
Output is correct |
51 |
Correct |
14 ms |
4120 KB |
Output is correct |
52 |
Correct |
13 ms |
4180 KB |
Output is correct |
53 |
Correct |
19 ms |
5656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
644 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
1532 KB |
Output is correct |
41 |
Correct |
3 ms |
2528 KB |
Output is correct |
42 |
Correct |
17 ms |
5500 KB |
Output is correct |
43 |
Correct |
19 ms |
5588 KB |
Output is correct |
44 |
Correct |
15 ms |
5532 KB |
Output is correct |
45 |
Correct |
18 ms |
5632 KB |
Output is correct |
46 |
Correct |
16 ms |
5588 KB |
Output is correct |
47 |
Correct |
12 ms |
3256 KB |
Output is correct |
48 |
Correct |
16 ms |
4244 KB |
Output is correct |
49 |
Correct |
16 ms |
4772 KB |
Output is correct |
50 |
Correct |
16 ms |
4820 KB |
Output is correct |
51 |
Correct |
14 ms |
4120 KB |
Output is correct |
52 |
Correct |
13 ms |
4180 KB |
Output is correct |
53 |
Correct |
19 ms |
5656 KB |
Output is correct |
54 |
Correct |
153 ms |
22072 KB |
Output is correct |
55 |
Correct |
65 ms |
21660 KB |
Output is correct |
56 |
Correct |
1432 ms |
100948 KB |
Output is correct |
57 |
Correct |
1479 ms |
101608 KB |
Output is correct |
58 |
Correct |
1455 ms |
101852 KB |
Output is correct |
59 |
Correct |
1447 ms |
102132 KB |
Output is correct |
60 |
Correct |
1386 ms |
102084 KB |
Output is correct |
61 |
Correct |
912 ms |
57296 KB |
Output is correct |
62 |
Correct |
96 ms |
22592 KB |
Output is correct |
63 |
Correct |
1242 ms |
81120 KB |
Output is correct |
64 |
Correct |
1068 ms |
82000 KB |
Output is correct |
65 |
Correct |
848 ms |
60424 KB |
Output is correct |
66 |
Correct |
985 ms |
60392 KB |
Output is correct |
67 |
Correct |
1504 ms |
102168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
294 ms |
59120 KB |
Output is correct |
14 |
Correct |
356 ms |
67000 KB |
Output is correct |
15 |
Correct |
427 ms |
77512 KB |
Output is correct |
16 |
Correct |
499 ms |
88560 KB |
Output is correct |
17 |
Correct |
590 ms |
101424 KB |
Output is correct |
18 |
Correct |
641 ms |
101420 KB |
Output is correct |
19 |
Correct |
597 ms |
101352 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
644 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
596 KB |
Output is correct |
57 |
Correct |
3 ms |
1532 KB |
Output is correct |
58 |
Correct |
3 ms |
2528 KB |
Output is correct |
59 |
Correct |
17 ms |
5500 KB |
Output is correct |
60 |
Correct |
19 ms |
5588 KB |
Output is correct |
61 |
Correct |
15 ms |
5532 KB |
Output is correct |
62 |
Correct |
18 ms |
5632 KB |
Output is correct |
63 |
Correct |
16 ms |
5588 KB |
Output is correct |
64 |
Correct |
12 ms |
3256 KB |
Output is correct |
65 |
Correct |
16 ms |
4244 KB |
Output is correct |
66 |
Correct |
16 ms |
4772 KB |
Output is correct |
67 |
Correct |
16 ms |
4820 KB |
Output is correct |
68 |
Correct |
14 ms |
4120 KB |
Output is correct |
69 |
Correct |
13 ms |
4180 KB |
Output is correct |
70 |
Correct |
19 ms |
5656 KB |
Output is correct |
71 |
Correct |
153 ms |
22072 KB |
Output is correct |
72 |
Correct |
65 ms |
21660 KB |
Output is correct |
73 |
Correct |
1432 ms |
100948 KB |
Output is correct |
74 |
Correct |
1479 ms |
101608 KB |
Output is correct |
75 |
Correct |
1455 ms |
101852 KB |
Output is correct |
76 |
Correct |
1447 ms |
102132 KB |
Output is correct |
77 |
Correct |
1386 ms |
102084 KB |
Output is correct |
78 |
Correct |
912 ms |
57296 KB |
Output is correct |
79 |
Correct |
96 ms |
22592 KB |
Output is correct |
80 |
Correct |
1242 ms |
81120 KB |
Output is correct |
81 |
Correct |
1068 ms |
82000 KB |
Output is correct |
82 |
Correct |
848 ms |
60424 KB |
Output is correct |
83 |
Correct |
985 ms |
60392 KB |
Output is correct |
84 |
Correct |
1504 ms |
102168 KB |
Output is correct |
85 |
Execution timed out |
3074 ms |
218388 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |