#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;
const int M=1e7;
vector<int> v[M];
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];
bool vis[M];
signed main(){
quick
int n;
cin>>n;
cin>>s;
double St=clock();
build(n);
int a,b,c;
cin>>a>>b>>c;
map<int,int > mx;
int cnt=1;
rep(i,1,n){
rep(j,i,n){
dp[i][j]=(j-i+1)*a;
if(!mx[pref[i][j]]) mx[pref[i][j]]=cnt++;
v[mx[pref[i][j]]].eb(i);
}
}
rep(L,1,n){
fill(nxt,nxt+n+1,-1);
rep(l,1,n-L+1){
int r=l+L-1;
int qx=mx[pref[l][r]];
int n2=sz(v[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){
int _r=0;
int n2=sz(v[qx]);
rep(i,0,n2-1){
while(_r<n2&&v[qx][_r]<=v[qx][i]+L-1) _r++;
nxt[v[qx][i]]=(_r>=n2 ? -1 : v[qx][_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];
}
if((clock()-St)/CLOCKS_PER_SEC>=2.8) break;
}
if((clock()-St)/CLOCKS_PER_SEC>=2.8) break;
}
cout<<dp[1][n]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
235272 KB |
Output is correct |
2 |
Correct |
122 ms |
235180 KB |
Output is correct |
3 |
Correct |
124 ms |
235124 KB |
Output is correct |
4 |
Correct |
126 ms |
235096 KB |
Output is correct |
5 |
Correct |
122 ms |
235180 KB |
Output is correct |
6 |
Correct |
124 ms |
235176 KB |
Output is correct |
7 |
Correct |
135 ms |
235136 KB |
Output is correct |
8 |
Correct |
139 ms |
235204 KB |
Output is correct |
9 |
Correct |
126 ms |
235164 KB |
Output is correct |
10 |
Correct |
129 ms |
235212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
235288 KB |
Output is correct |
2 |
Correct |
120 ms |
235176 KB |
Output is correct |
3 |
Correct |
1033 ms |
294216 KB |
Output is correct |
4 |
Correct |
1157 ms |
301948 KB |
Output is correct |
5 |
Correct |
1409 ms |
312008 KB |
Output is correct |
6 |
Correct |
1623 ms |
323976 KB |
Output is correct |
7 |
Correct |
1842 ms |
335812 KB |
Output is correct |
8 |
Correct |
1825 ms |
335824 KB |
Output is correct |
9 |
Correct |
1856 ms |
335844 KB |
Output is correct |
10 |
Correct |
127 ms |
235084 KB |
Output is correct |
11 |
Correct |
126 ms |
235164 KB |
Output is correct |
12 |
Correct |
131 ms |
235180 KB |
Output is correct |
13 |
Correct |
124 ms |
235084 KB |
Output is correct |
14 |
Correct |
125 ms |
235336 KB |
Output is correct |
15 |
Correct |
126 ms |
235200 KB |
Output is correct |
16 |
Correct |
141 ms |
235360 KB |
Output is correct |
17 |
Correct |
127 ms |
235188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
235272 KB |
Output is correct |
2 |
Correct |
122 ms |
235180 KB |
Output is correct |
3 |
Correct |
124 ms |
235124 KB |
Output is correct |
4 |
Correct |
126 ms |
235096 KB |
Output is correct |
5 |
Correct |
122 ms |
235180 KB |
Output is correct |
6 |
Correct |
124 ms |
235176 KB |
Output is correct |
7 |
Correct |
135 ms |
235136 KB |
Output is correct |
8 |
Correct |
139 ms |
235204 KB |
Output is correct |
9 |
Correct |
126 ms |
235164 KB |
Output is correct |
10 |
Correct |
129 ms |
235212 KB |
Output is correct |
11 |
Correct |
127 ms |
235140 KB |
Output is correct |
12 |
Correct |
132 ms |
235292 KB |
Output is correct |
13 |
Correct |
129 ms |
235232 KB |
Output is correct |
14 |
Correct |
134 ms |
235140 KB |
Output is correct |
15 |
Correct |
130 ms |
235216 KB |
Output is correct |
16 |
Correct |
123 ms |
235224 KB |
Output is correct |
17 |
Correct |
127 ms |
235184 KB |
Output is correct |
18 |
Correct |
127 ms |
235140 KB |
Output is correct |
19 |
Correct |
138 ms |
235192 KB |
Output is correct |
20 |
Correct |
131 ms |
235124 KB |
Output is correct |
21 |
Correct |
138 ms |
235488 KB |
Output is correct |
22 |
Correct |
125 ms |
235316 KB |
Output is correct |
23 |
Correct |
128 ms |
235340 KB |
Output is correct |
24 |
Correct |
124 ms |
235340 KB |
Output is correct |
25 |
Correct |
129 ms |
235436 KB |
Output is correct |
26 |
Correct |
132 ms |
235352 KB |
Output is correct |
27 |
Correct |
126 ms |
235420 KB |
Output is correct |
28 |
Correct |
125 ms |
235412 KB |
Output is correct |
29 |
Correct |
132 ms |
235420 KB |
Output is correct |
30 |
Correct |
132 ms |
235428 KB |
Output is correct |
31 |
Correct |
133 ms |
235364 KB |
Output is correct |
32 |
Correct |
134 ms |
235376 KB |
Output is correct |
33 |
Correct |
138 ms |
235508 KB |
Output is correct |
34 |
Correct |
123 ms |
235092 KB |
Output is correct |
35 |
Correct |
131 ms |
235068 KB |
Output is correct |
36 |
Correct |
195 ms |
235176 KB |
Output is correct |
37 |
Correct |
163 ms |
235348 KB |
Output is correct |
38 |
Correct |
180 ms |
235164 KB |
Output is correct |
39 |
Correct |
191 ms |
235376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
235272 KB |
Output is correct |
2 |
Correct |
122 ms |
235180 KB |
Output is correct |
3 |
Correct |
124 ms |
235124 KB |
Output is correct |
4 |
Correct |
126 ms |
235096 KB |
Output is correct |
5 |
Correct |
122 ms |
235180 KB |
Output is correct |
6 |
Correct |
124 ms |
235176 KB |
Output is correct |
7 |
Correct |
135 ms |
235136 KB |
Output is correct |
8 |
Correct |
139 ms |
235204 KB |
Output is correct |
9 |
Correct |
126 ms |
235164 KB |
Output is correct |
10 |
Correct |
129 ms |
235212 KB |
Output is correct |
11 |
Correct |
127 ms |
235140 KB |
Output is correct |
12 |
Correct |
132 ms |
235292 KB |
Output is correct |
13 |
Correct |
129 ms |
235232 KB |
Output is correct |
14 |
Correct |
134 ms |
235140 KB |
Output is correct |
15 |
Correct |
130 ms |
235216 KB |
Output is correct |
16 |
Correct |
123 ms |
235224 KB |
Output is correct |
17 |
Correct |
127 ms |
235184 KB |
Output is correct |
18 |
Correct |
127 ms |
235140 KB |
Output is correct |
19 |
Correct |
138 ms |
235192 KB |
Output is correct |
20 |
Correct |
131 ms |
235124 KB |
Output is correct |
21 |
Correct |
138 ms |
235488 KB |
Output is correct |
22 |
Correct |
125 ms |
235316 KB |
Output is correct |
23 |
Correct |
128 ms |
235340 KB |
Output is correct |
24 |
Correct |
124 ms |
235340 KB |
Output is correct |
25 |
Correct |
129 ms |
235436 KB |
Output is correct |
26 |
Correct |
132 ms |
235352 KB |
Output is correct |
27 |
Correct |
126 ms |
235420 KB |
Output is correct |
28 |
Correct |
125 ms |
235412 KB |
Output is correct |
29 |
Correct |
132 ms |
235420 KB |
Output is correct |
30 |
Correct |
132 ms |
235428 KB |
Output is correct |
31 |
Correct |
133 ms |
235364 KB |
Output is correct |
32 |
Correct |
134 ms |
235376 KB |
Output is correct |
33 |
Correct |
138 ms |
235508 KB |
Output is correct |
34 |
Correct |
123 ms |
235092 KB |
Output is correct |
35 |
Correct |
131 ms |
235068 KB |
Output is correct |
36 |
Correct |
195 ms |
235176 KB |
Output is correct |
37 |
Correct |
163 ms |
235348 KB |
Output is correct |
38 |
Correct |
180 ms |
235164 KB |
Output is correct |
39 |
Correct |
191 ms |
235376 KB |
Output is correct |
40 |
Correct |
168 ms |
236088 KB |
Output is correct |
41 |
Correct |
175 ms |
237324 KB |
Output is correct |
42 |
Correct |
197 ms |
238812 KB |
Output is correct |
43 |
Correct |
142 ms |
238920 KB |
Output is correct |
44 |
Correct |
151 ms |
238948 KB |
Output is correct |
45 |
Correct |
146 ms |
238964 KB |
Output is correct |
46 |
Correct |
148 ms |
238928 KB |
Output is correct |
47 |
Correct |
168 ms |
237756 KB |
Output is correct |
48 |
Correct |
146 ms |
238156 KB |
Output is correct |
49 |
Correct |
142 ms |
238540 KB |
Output is correct |
50 |
Correct |
146 ms |
238412 KB |
Output is correct |
51 |
Correct |
175 ms |
238164 KB |
Output is correct |
52 |
Correct |
143 ms |
238160 KB |
Output is correct |
53 |
Correct |
152 ms |
238960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
235272 KB |
Output is correct |
2 |
Correct |
122 ms |
235180 KB |
Output is correct |
3 |
Correct |
124 ms |
235124 KB |
Output is correct |
4 |
Correct |
126 ms |
235096 KB |
Output is correct |
5 |
Correct |
122 ms |
235180 KB |
Output is correct |
6 |
Correct |
124 ms |
235176 KB |
Output is correct |
7 |
Correct |
135 ms |
235136 KB |
Output is correct |
8 |
Correct |
139 ms |
235204 KB |
Output is correct |
9 |
Correct |
126 ms |
235164 KB |
Output is correct |
10 |
Correct |
129 ms |
235212 KB |
Output is correct |
11 |
Correct |
127 ms |
235140 KB |
Output is correct |
12 |
Correct |
132 ms |
235292 KB |
Output is correct |
13 |
Correct |
129 ms |
235232 KB |
Output is correct |
14 |
Correct |
134 ms |
235140 KB |
Output is correct |
15 |
Correct |
130 ms |
235216 KB |
Output is correct |
16 |
Correct |
123 ms |
235224 KB |
Output is correct |
17 |
Correct |
127 ms |
235184 KB |
Output is correct |
18 |
Correct |
127 ms |
235140 KB |
Output is correct |
19 |
Correct |
138 ms |
235192 KB |
Output is correct |
20 |
Correct |
131 ms |
235124 KB |
Output is correct |
21 |
Correct |
138 ms |
235488 KB |
Output is correct |
22 |
Correct |
125 ms |
235316 KB |
Output is correct |
23 |
Correct |
128 ms |
235340 KB |
Output is correct |
24 |
Correct |
124 ms |
235340 KB |
Output is correct |
25 |
Correct |
129 ms |
235436 KB |
Output is correct |
26 |
Correct |
132 ms |
235352 KB |
Output is correct |
27 |
Correct |
126 ms |
235420 KB |
Output is correct |
28 |
Correct |
125 ms |
235412 KB |
Output is correct |
29 |
Correct |
132 ms |
235420 KB |
Output is correct |
30 |
Correct |
132 ms |
235428 KB |
Output is correct |
31 |
Correct |
133 ms |
235364 KB |
Output is correct |
32 |
Correct |
134 ms |
235376 KB |
Output is correct |
33 |
Correct |
138 ms |
235508 KB |
Output is correct |
34 |
Correct |
123 ms |
235092 KB |
Output is correct |
35 |
Correct |
131 ms |
235068 KB |
Output is correct |
36 |
Correct |
195 ms |
235176 KB |
Output is correct |
37 |
Correct |
163 ms |
235348 KB |
Output is correct |
38 |
Correct |
180 ms |
235164 KB |
Output is correct |
39 |
Correct |
191 ms |
235376 KB |
Output is correct |
40 |
Correct |
168 ms |
236088 KB |
Output is correct |
41 |
Correct |
175 ms |
237324 KB |
Output is correct |
42 |
Correct |
197 ms |
238812 KB |
Output is correct |
43 |
Correct |
142 ms |
238920 KB |
Output is correct |
44 |
Correct |
151 ms |
238948 KB |
Output is correct |
45 |
Correct |
146 ms |
238964 KB |
Output is correct |
46 |
Correct |
148 ms |
238928 KB |
Output is correct |
47 |
Correct |
168 ms |
237756 KB |
Output is correct |
48 |
Correct |
146 ms |
238156 KB |
Output is correct |
49 |
Correct |
142 ms |
238540 KB |
Output is correct |
50 |
Correct |
146 ms |
238412 KB |
Output is correct |
51 |
Correct |
175 ms |
238164 KB |
Output is correct |
52 |
Correct |
143 ms |
238160 KB |
Output is correct |
53 |
Correct |
152 ms |
238960 KB |
Output is correct |
54 |
Correct |
270 ms |
249616 KB |
Output is correct |
55 |
Correct |
353 ms |
256488 KB |
Output is correct |
56 |
Correct |
1250 ms |
297612 KB |
Output is correct |
57 |
Correct |
1254 ms |
298052 KB |
Output is correct |
58 |
Correct |
1294 ms |
298280 KB |
Output is correct |
59 |
Correct |
1264 ms |
298372 KB |
Output is correct |
60 |
Correct |
1292 ms |
298572 KB |
Output is correct |
61 |
Correct |
932 ms |
275724 KB |
Output is correct |
62 |
Correct |
408 ms |
257228 KB |
Output is correct |
63 |
Correct |
1133 ms |
286984 KB |
Output is correct |
64 |
Correct |
1128 ms |
287692 KB |
Output is correct |
65 |
Correct |
951 ms |
276384 KB |
Output is correct |
66 |
Correct |
930 ms |
276436 KB |
Output is correct |
67 |
Correct |
1274 ms |
298388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
235272 KB |
Output is correct |
2 |
Correct |
122 ms |
235180 KB |
Output is correct |
3 |
Correct |
124 ms |
235124 KB |
Output is correct |
4 |
Correct |
126 ms |
235096 KB |
Output is correct |
5 |
Correct |
122 ms |
235180 KB |
Output is correct |
6 |
Correct |
124 ms |
235176 KB |
Output is correct |
7 |
Correct |
135 ms |
235136 KB |
Output is correct |
8 |
Correct |
139 ms |
235204 KB |
Output is correct |
9 |
Correct |
126 ms |
235164 KB |
Output is correct |
10 |
Correct |
129 ms |
235212 KB |
Output is correct |
11 |
Correct |
133 ms |
235288 KB |
Output is correct |
12 |
Correct |
120 ms |
235176 KB |
Output is correct |
13 |
Correct |
1033 ms |
294216 KB |
Output is correct |
14 |
Correct |
1157 ms |
301948 KB |
Output is correct |
15 |
Correct |
1409 ms |
312008 KB |
Output is correct |
16 |
Correct |
1623 ms |
323976 KB |
Output is correct |
17 |
Correct |
1842 ms |
335812 KB |
Output is correct |
18 |
Correct |
1825 ms |
335824 KB |
Output is correct |
19 |
Correct |
1856 ms |
335844 KB |
Output is correct |
20 |
Correct |
127 ms |
235084 KB |
Output is correct |
21 |
Correct |
126 ms |
235164 KB |
Output is correct |
22 |
Correct |
131 ms |
235180 KB |
Output is correct |
23 |
Correct |
124 ms |
235084 KB |
Output is correct |
24 |
Correct |
125 ms |
235336 KB |
Output is correct |
25 |
Correct |
126 ms |
235200 KB |
Output is correct |
26 |
Correct |
141 ms |
235360 KB |
Output is correct |
27 |
Correct |
127 ms |
235188 KB |
Output is correct |
28 |
Correct |
127 ms |
235140 KB |
Output is correct |
29 |
Correct |
132 ms |
235292 KB |
Output is correct |
30 |
Correct |
129 ms |
235232 KB |
Output is correct |
31 |
Correct |
134 ms |
235140 KB |
Output is correct |
32 |
Correct |
130 ms |
235216 KB |
Output is correct |
33 |
Correct |
123 ms |
235224 KB |
Output is correct |
34 |
Correct |
127 ms |
235184 KB |
Output is correct |
35 |
Correct |
127 ms |
235140 KB |
Output is correct |
36 |
Correct |
138 ms |
235192 KB |
Output is correct |
37 |
Correct |
131 ms |
235124 KB |
Output is correct |
38 |
Correct |
138 ms |
235488 KB |
Output is correct |
39 |
Correct |
125 ms |
235316 KB |
Output is correct |
40 |
Correct |
128 ms |
235340 KB |
Output is correct |
41 |
Correct |
124 ms |
235340 KB |
Output is correct |
42 |
Correct |
129 ms |
235436 KB |
Output is correct |
43 |
Correct |
132 ms |
235352 KB |
Output is correct |
44 |
Correct |
126 ms |
235420 KB |
Output is correct |
45 |
Correct |
125 ms |
235412 KB |
Output is correct |
46 |
Correct |
132 ms |
235420 KB |
Output is correct |
47 |
Correct |
132 ms |
235428 KB |
Output is correct |
48 |
Correct |
133 ms |
235364 KB |
Output is correct |
49 |
Correct |
134 ms |
235376 KB |
Output is correct |
50 |
Correct |
138 ms |
235508 KB |
Output is correct |
51 |
Correct |
123 ms |
235092 KB |
Output is correct |
52 |
Correct |
131 ms |
235068 KB |
Output is correct |
53 |
Correct |
195 ms |
235176 KB |
Output is correct |
54 |
Correct |
163 ms |
235348 KB |
Output is correct |
55 |
Correct |
180 ms |
235164 KB |
Output is correct |
56 |
Correct |
191 ms |
235376 KB |
Output is correct |
57 |
Correct |
168 ms |
236088 KB |
Output is correct |
58 |
Correct |
175 ms |
237324 KB |
Output is correct |
59 |
Correct |
197 ms |
238812 KB |
Output is correct |
60 |
Correct |
142 ms |
238920 KB |
Output is correct |
61 |
Correct |
151 ms |
238948 KB |
Output is correct |
62 |
Correct |
146 ms |
238964 KB |
Output is correct |
63 |
Correct |
148 ms |
238928 KB |
Output is correct |
64 |
Correct |
168 ms |
237756 KB |
Output is correct |
65 |
Correct |
146 ms |
238156 KB |
Output is correct |
66 |
Correct |
142 ms |
238540 KB |
Output is correct |
67 |
Correct |
146 ms |
238412 KB |
Output is correct |
68 |
Correct |
175 ms |
238164 KB |
Output is correct |
69 |
Correct |
143 ms |
238160 KB |
Output is correct |
70 |
Correct |
152 ms |
238960 KB |
Output is correct |
71 |
Correct |
270 ms |
249616 KB |
Output is correct |
72 |
Correct |
353 ms |
256488 KB |
Output is correct |
73 |
Correct |
1250 ms |
297612 KB |
Output is correct |
74 |
Correct |
1254 ms |
298052 KB |
Output is correct |
75 |
Correct |
1294 ms |
298280 KB |
Output is correct |
76 |
Correct |
1264 ms |
298372 KB |
Output is correct |
77 |
Correct |
1292 ms |
298572 KB |
Output is correct |
78 |
Correct |
932 ms |
275724 KB |
Output is correct |
79 |
Correct |
408 ms |
257228 KB |
Output is correct |
80 |
Correct |
1133 ms |
286984 KB |
Output is correct |
81 |
Correct |
1128 ms |
287692 KB |
Output is correct |
82 |
Correct |
951 ms |
276384 KB |
Output is correct |
83 |
Correct |
930 ms |
276436 KB |
Output is correct |
84 |
Correct |
1274 ms |
298388 KB |
Output is correct |
85 |
Execution timed out |
3076 ms |
384928 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |