#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;
int p;
const int Mod=462'779'347'772'851;
int pref[N][N];
int dp[N][N];
string s;
void build(int n){
mt19937 rand(time(0));
p=rand()%26+29;
rep(i,1,n){
rep(j,i,n){
pref[i][j]=pref[i][j-1]*p%Mod+s[j-1]-'a'+2;
pref[i][j]%=Mod;
}
}
}
int q(int l,int r){
return pref[l][r];
}
int nxt[N][N];
signed main(){
quick
int n;
cin>>n;
cin>>s;
build(n);
int a,b,c;
cin>>a>>b>>c;
rep(i,1,n){
rep(j,i,n){
dp[i][j]=(j-i+1)*a;
}
}
rep(L,1,n){
map<int,int> m;
repd(i,n-L+1,1){
if(i+2*L-1<=n){
m[pref[i+L][i+2*L-1]]=i+L;
nxt[L][i]=m[pref[i][i+L-1]];
}
}
}
rep(L,1,n){
rep(l,1,n-L+1){
int r=l+L-1;
if(l<r) dp[l][r]=min({dp[l][r],dp[l][r-1]+a,dp[l+1][r]+a});
int px=nxt[L][l];
int pst=1;
while(px){
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[L][px];
}
}
}
cout<<dp[1][n]<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 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 |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
165 ms |
51348 KB |
Output is correct |
4 |
Correct |
176 ms |
58364 KB |
Output is correct |
5 |
Correct |
206 ms |
66288 KB |
Output is correct |
6 |
Correct |
245 ms |
74780 KB |
Output is correct |
7 |
Correct |
284 ms |
84044 KB |
Output is correct |
8 |
Correct |
317 ms |
84008 KB |
Output is correct |
9 |
Correct |
278 ms |
84124 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 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 |
0 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 |
0 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 |
340 KB |
Output is correct |
12 |
Correct |
1 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 |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
0 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 |
532 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 |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
0 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 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 |
340 KB |
Output is correct |
12 |
Correct |
1 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 |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
0 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 |
532 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 |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
0 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
1236 KB |
Output is correct |
41 |
Correct |
2 ms |
2644 KB |
Output is correct |
42 |
Correct |
6 ms |
2644 KB |
Output is correct |
43 |
Correct |
6 ms |
2644 KB |
Output is correct |
44 |
Correct |
4 ms |
2660 KB |
Output is correct |
45 |
Correct |
4 ms |
2644 KB |
Output is correct |
46 |
Correct |
3 ms |
2644 KB |
Output is correct |
47 |
Correct |
3 ms |
2756 KB |
Output is correct |
48 |
Correct |
3 ms |
2644 KB |
Output is correct |
49 |
Correct |
3 ms |
2644 KB |
Output is correct |
50 |
Correct |
3 ms |
2644 KB |
Output is correct |
51 |
Correct |
3 ms |
2644 KB |
Output is correct |
52 |
Correct |
3 ms |
2644 KB |
Output is correct |
53 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 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 |
340 KB |
Output is correct |
12 |
Correct |
1 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 |
468 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
0 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 |
532 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 |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
0 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
1236 KB |
Output is correct |
41 |
Correct |
2 ms |
2644 KB |
Output is correct |
42 |
Correct |
6 ms |
2644 KB |
Output is correct |
43 |
Correct |
6 ms |
2644 KB |
Output is correct |
44 |
Correct |
4 ms |
2660 KB |
Output is correct |
45 |
Correct |
4 ms |
2644 KB |
Output is correct |
46 |
Correct |
3 ms |
2644 KB |
Output is correct |
47 |
Correct |
3 ms |
2756 KB |
Output is correct |
48 |
Correct |
3 ms |
2644 KB |
Output is correct |
49 |
Correct |
3 ms |
2644 KB |
Output is correct |
50 |
Correct |
3 ms |
2644 KB |
Output is correct |
51 |
Correct |
3 ms |
2644 KB |
Output is correct |
52 |
Correct |
3 ms |
2644 KB |
Output is correct |
53 |
Correct |
3 ms |
2644 KB |
Output is correct |
54 |
Correct |
17 ms |
6768 KB |
Output is correct |
55 |
Correct |
34 ms |
20144 KB |
Output is correct |
56 |
Correct |
78 ms |
20188 KB |
Output is correct |
57 |
Correct |
76 ms |
20216 KB |
Output is correct |
58 |
Correct |
70 ms |
20168 KB |
Output is correct |
59 |
Correct |
84 ms |
20148 KB |
Output is correct |
60 |
Correct |
76 ms |
20324 KB |
Output is correct |
61 |
Correct |
63 ms |
20172 KB |
Output is correct |
62 |
Correct |
30 ms |
20072 KB |
Output is correct |
63 |
Correct |
62 ms |
20136 KB |
Output is correct |
64 |
Correct |
65 ms |
20148 KB |
Output is correct |
65 |
Correct |
52 ms |
20084 KB |
Output is correct |
66 |
Correct |
53 ms |
20172 KB |
Output is correct |
67 |
Correct |
83 ms |
20184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
165 ms |
51348 KB |
Output is correct |
14 |
Correct |
176 ms |
58364 KB |
Output is correct |
15 |
Correct |
206 ms |
66288 KB |
Output is correct |
16 |
Correct |
245 ms |
74780 KB |
Output is correct |
17 |
Correct |
284 ms |
84044 KB |
Output is correct |
18 |
Correct |
317 ms |
84008 KB |
Output is correct |
19 |
Correct |
278 ms |
84124 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 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 |
468 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 |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
0 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 |
532 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 |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
0 ms |
596 KB |
Output is correct |
57 |
Correct |
1 ms |
1236 KB |
Output is correct |
58 |
Correct |
2 ms |
2644 KB |
Output is correct |
59 |
Correct |
6 ms |
2644 KB |
Output is correct |
60 |
Correct |
6 ms |
2644 KB |
Output is correct |
61 |
Correct |
4 ms |
2660 KB |
Output is correct |
62 |
Correct |
4 ms |
2644 KB |
Output is correct |
63 |
Correct |
3 ms |
2644 KB |
Output is correct |
64 |
Correct |
3 ms |
2756 KB |
Output is correct |
65 |
Correct |
3 ms |
2644 KB |
Output is correct |
66 |
Correct |
3 ms |
2644 KB |
Output is correct |
67 |
Correct |
3 ms |
2644 KB |
Output is correct |
68 |
Correct |
3 ms |
2644 KB |
Output is correct |
69 |
Correct |
3 ms |
2644 KB |
Output is correct |
70 |
Correct |
3 ms |
2644 KB |
Output is correct |
71 |
Correct |
17 ms |
6768 KB |
Output is correct |
72 |
Correct |
34 ms |
20144 KB |
Output is correct |
73 |
Correct |
78 ms |
20188 KB |
Output is correct |
74 |
Correct |
76 ms |
20216 KB |
Output is correct |
75 |
Correct |
70 ms |
20168 KB |
Output is correct |
76 |
Correct |
84 ms |
20148 KB |
Output is correct |
77 |
Correct |
76 ms |
20324 KB |
Output is correct |
78 |
Correct |
63 ms |
20172 KB |
Output is correct |
79 |
Correct |
30 ms |
20072 KB |
Output is correct |
80 |
Correct |
62 ms |
20136 KB |
Output is correct |
81 |
Correct |
65 ms |
20148 KB |
Output is correct |
82 |
Correct |
52 ms |
20084 KB |
Output is correct |
83 |
Correct |
53 ms |
20172 KB |
Output is correct |
84 |
Correct |
83 ms |
20184 KB |
Output is correct |
85 |
Correct |
191 ms |
40656 KB |
Output is correct |
86 |
Correct |
506 ms |
84300 KB |
Output is correct |
87 |
Correct |
511 ms |
84280 KB |
Output is correct |
88 |
Correct |
520 ms |
84204 KB |
Output is correct |
89 |
Correct |
501 ms |
84228 KB |
Output is correct |
90 |
Correct |
567 ms |
84296 KB |
Output is correct |
91 |
Correct |
217 ms |
84180 KB |
Output is correct |
92 |
Correct |
268 ms |
84176 KB |
Output is correct |
93 |
Correct |
426 ms |
84204 KB |
Output is correct |
94 |
Correct |
458 ms |
84284 KB |
Output is correct |
95 |
Correct |
385 ms |
84044 KB |
Output is correct |
96 |
Correct |
389 ms |
84076 KB |
Output is correct |
97 |
Correct |
560 ms |
84248 KB |
Output is correct |