/*input
18
aababbbababbbaabbb
1000000000
100000
10000000
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<ll,ll>
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
const ll maxn=3e3+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())+1
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<P.f<<' '<<P.s;
return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?" ":"");
return out;
}
ll mult(ll a,ll b){
if(a<0) a+=MOD;
return (a*b)%MOD;
}
ll mypow(ll a,ll b){
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=mult(res,a);
a=mult(a,a);
b>>=1;
}
return res;
}
ll dp[maxn][maxn];
struct node{
node* ch[26];
int idx;
};
int tot=0;
node* create(){
node* x=new node();
REP(i,26) x->ch[i]=NULL;
x->idx=(tot++);
return x;
}
node *rt=create();
int nxt[maxn][maxn];
bool ok[maxn][maxn];
vector<int> fac[maxn];
vector<pii> vv[maxn*maxn];
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0);
int n;
ll A,b,c;
string s;
cin>>n>>s>>A>>b>>c;
s="0"+s;
REP1(i,n){
node *cur=rt;
for(int j=i;j<=n;j++){
if(!cur->ch[s[j]-'a']){
cur->ch[s[j]-'a']=create();
}
cur=cur->ch[s[j]-'a'];
vv[cur->idx].pb({i,j});
}
}
REP(i,tot){
int p=0;
for(auto x:vv[i]){
while(p<sz(vv[i]) and vv[i][p].f<=x.s) ++p;
if(p==sz(vv[i])) nxt[x.f][x.s]=INF;
else nxt[x.f][x.s]=vv[i][p].f;
}
}
for(int i=n;i>=1;i--){
for(int j=i;j<=n;j++) dp[i][j]=dp[i+1][j]+A;
for(int j=i;j<=n;j++){
MNTO(dp[i][j],dp[i][j-1]+A);
int len=(j-i+1);
int r=i;
int cnt=1;
while(nxt[r][r+len-1]!=INF){
r=nxt[r][r+len-1];
++cnt;
MNTO(dp[i][r+len-1],dp[i][j]+b+c*cnt+A*(r-i-(cnt-1)*len));
}
//cout<<i<<' '<<j<<' '<<dp[i][j]<<'\n';
}
}
cout<<dp[1][n];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
212384 KB |
Output is correct |
2 |
Correct |
102 ms |
212464 KB |
Output is correct |
3 |
Correct |
114 ms |
212488 KB |
Output is correct |
4 |
Correct |
104 ms |
212364 KB |
Output is correct |
5 |
Correct |
110 ms |
212448 KB |
Output is correct |
6 |
Correct |
105 ms |
212360 KB |
Output is correct |
7 |
Correct |
106 ms |
212348 KB |
Output is correct |
8 |
Correct |
103 ms |
212468 KB |
Output is correct |
9 |
Correct |
102 ms |
212448 KB |
Output is correct |
10 |
Correct |
104 ms |
212412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
212500 KB |
Output is correct |
2 |
Correct |
110 ms |
212348 KB |
Output is correct |
3 |
Correct |
324 ms |
281604 KB |
Output is correct |
4 |
Correct |
370 ms |
290468 KB |
Output is correct |
5 |
Correct |
440 ms |
302720 KB |
Output is correct |
6 |
Correct |
480 ms |
317748 KB |
Output is correct |
7 |
Correct |
591 ms |
334168 KB |
Output is correct |
8 |
Correct |
598 ms |
334124 KB |
Output is correct |
9 |
Correct |
595 ms |
334140 KB |
Output is correct |
10 |
Correct |
101 ms |
212428 KB |
Output is correct |
11 |
Correct |
111 ms |
212332 KB |
Output is correct |
12 |
Correct |
104 ms |
212472 KB |
Output is correct |
13 |
Correct |
112 ms |
212496 KB |
Output is correct |
14 |
Correct |
103 ms |
212612 KB |
Output is correct |
15 |
Correct |
100 ms |
212536 KB |
Output is correct |
16 |
Correct |
109 ms |
212820 KB |
Output is correct |
17 |
Correct |
106 ms |
212428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
212384 KB |
Output is correct |
2 |
Correct |
102 ms |
212464 KB |
Output is correct |
3 |
Correct |
114 ms |
212488 KB |
Output is correct |
4 |
Correct |
104 ms |
212364 KB |
Output is correct |
5 |
Correct |
110 ms |
212448 KB |
Output is correct |
6 |
Correct |
105 ms |
212360 KB |
Output is correct |
7 |
Correct |
106 ms |
212348 KB |
Output is correct |
8 |
Correct |
103 ms |
212468 KB |
Output is correct |
9 |
Correct |
102 ms |
212448 KB |
Output is correct |
10 |
Correct |
104 ms |
212412 KB |
Output is correct |
11 |
Correct |
104 ms |
212536 KB |
Output is correct |
12 |
Correct |
109 ms |
212664 KB |
Output is correct |
13 |
Correct |
104 ms |
212564 KB |
Output is correct |
14 |
Correct |
104 ms |
212428 KB |
Output is correct |
15 |
Correct |
100 ms |
212632 KB |
Output is correct |
16 |
Correct |
104 ms |
212632 KB |
Output is correct |
17 |
Correct |
105 ms |
212412 KB |
Output is correct |
18 |
Correct |
105 ms |
212360 KB |
Output is correct |
19 |
Correct |
104 ms |
212564 KB |
Output is correct |
20 |
Correct |
101 ms |
212440 KB |
Output is correct |
21 |
Correct |
111 ms |
212716 KB |
Output is correct |
22 |
Correct |
106 ms |
212784 KB |
Output is correct |
23 |
Correct |
103 ms |
212728 KB |
Output is correct |
24 |
Correct |
100 ms |
212684 KB |
Output is correct |
25 |
Correct |
101 ms |
212728 KB |
Output is correct |
26 |
Correct |
103 ms |
212804 KB |
Output is correct |
27 |
Correct |
101 ms |
212684 KB |
Output is correct |
28 |
Correct |
105 ms |
212676 KB |
Output is correct |
29 |
Correct |
106 ms |
212780 KB |
Output is correct |
30 |
Correct |
101 ms |
212720 KB |
Output is correct |
31 |
Correct |
101 ms |
212732 KB |
Output is correct |
32 |
Correct |
102 ms |
212688 KB |
Output is correct |
33 |
Correct |
104 ms |
212712 KB |
Output is correct |
34 |
Correct |
106 ms |
212428 KB |
Output is correct |
35 |
Correct |
115 ms |
212348 KB |
Output is correct |
36 |
Correct |
102 ms |
212488 KB |
Output is correct |
37 |
Correct |
102 ms |
212608 KB |
Output is correct |
38 |
Correct |
104 ms |
212440 KB |
Output is correct |
39 |
Correct |
110 ms |
212804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
212384 KB |
Output is correct |
2 |
Correct |
102 ms |
212464 KB |
Output is correct |
3 |
Correct |
114 ms |
212488 KB |
Output is correct |
4 |
Correct |
104 ms |
212364 KB |
Output is correct |
5 |
Correct |
110 ms |
212448 KB |
Output is correct |
6 |
Correct |
105 ms |
212360 KB |
Output is correct |
7 |
Correct |
106 ms |
212348 KB |
Output is correct |
8 |
Correct |
103 ms |
212468 KB |
Output is correct |
9 |
Correct |
102 ms |
212448 KB |
Output is correct |
10 |
Correct |
104 ms |
212412 KB |
Output is correct |
11 |
Correct |
104 ms |
212536 KB |
Output is correct |
12 |
Correct |
109 ms |
212664 KB |
Output is correct |
13 |
Correct |
104 ms |
212564 KB |
Output is correct |
14 |
Correct |
104 ms |
212428 KB |
Output is correct |
15 |
Correct |
100 ms |
212632 KB |
Output is correct |
16 |
Correct |
104 ms |
212632 KB |
Output is correct |
17 |
Correct |
105 ms |
212412 KB |
Output is correct |
18 |
Correct |
105 ms |
212360 KB |
Output is correct |
19 |
Correct |
104 ms |
212564 KB |
Output is correct |
20 |
Correct |
101 ms |
212440 KB |
Output is correct |
21 |
Correct |
111 ms |
212716 KB |
Output is correct |
22 |
Correct |
106 ms |
212784 KB |
Output is correct |
23 |
Correct |
103 ms |
212728 KB |
Output is correct |
24 |
Correct |
100 ms |
212684 KB |
Output is correct |
25 |
Correct |
101 ms |
212728 KB |
Output is correct |
26 |
Correct |
103 ms |
212804 KB |
Output is correct |
27 |
Correct |
101 ms |
212684 KB |
Output is correct |
28 |
Correct |
105 ms |
212676 KB |
Output is correct |
29 |
Correct |
106 ms |
212780 KB |
Output is correct |
30 |
Correct |
101 ms |
212720 KB |
Output is correct |
31 |
Correct |
101 ms |
212732 KB |
Output is correct |
32 |
Correct |
102 ms |
212688 KB |
Output is correct |
33 |
Correct |
104 ms |
212712 KB |
Output is correct |
34 |
Correct |
106 ms |
212428 KB |
Output is correct |
35 |
Correct |
115 ms |
212348 KB |
Output is correct |
36 |
Correct |
102 ms |
212488 KB |
Output is correct |
37 |
Correct |
102 ms |
212608 KB |
Output is correct |
38 |
Correct |
104 ms |
212440 KB |
Output is correct |
39 |
Correct |
110 ms |
212804 KB |
Output is correct |
40 |
Correct |
106 ms |
213948 KB |
Output is correct |
41 |
Correct |
102 ms |
214772 KB |
Output is correct |
42 |
Correct |
110 ms |
218972 KB |
Output is correct |
43 |
Correct |
112 ms |
219128 KB |
Output is correct |
44 |
Correct |
108 ms |
219240 KB |
Output is correct |
45 |
Correct |
106 ms |
219152 KB |
Output is correct |
46 |
Correct |
134 ms |
219192 KB |
Output is correct |
47 |
Correct |
115 ms |
215972 KB |
Output is correct |
48 |
Correct |
110 ms |
217520 KB |
Output is correct |
49 |
Correct |
104 ms |
218072 KB |
Output is correct |
50 |
Correct |
111 ms |
218080 KB |
Output is correct |
51 |
Correct |
114 ms |
217192 KB |
Output is correct |
52 |
Correct |
104 ms |
217112 KB |
Output is correct |
53 |
Correct |
105 ms |
219216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
212384 KB |
Output is correct |
2 |
Correct |
102 ms |
212464 KB |
Output is correct |
3 |
Correct |
114 ms |
212488 KB |
Output is correct |
4 |
Correct |
104 ms |
212364 KB |
Output is correct |
5 |
Correct |
110 ms |
212448 KB |
Output is correct |
6 |
Correct |
105 ms |
212360 KB |
Output is correct |
7 |
Correct |
106 ms |
212348 KB |
Output is correct |
8 |
Correct |
103 ms |
212468 KB |
Output is correct |
9 |
Correct |
102 ms |
212448 KB |
Output is correct |
10 |
Correct |
104 ms |
212412 KB |
Output is correct |
11 |
Correct |
104 ms |
212536 KB |
Output is correct |
12 |
Correct |
109 ms |
212664 KB |
Output is correct |
13 |
Correct |
104 ms |
212564 KB |
Output is correct |
14 |
Correct |
104 ms |
212428 KB |
Output is correct |
15 |
Correct |
100 ms |
212632 KB |
Output is correct |
16 |
Correct |
104 ms |
212632 KB |
Output is correct |
17 |
Correct |
105 ms |
212412 KB |
Output is correct |
18 |
Correct |
105 ms |
212360 KB |
Output is correct |
19 |
Correct |
104 ms |
212564 KB |
Output is correct |
20 |
Correct |
101 ms |
212440 KB |
Output is correct |
21 |
Correct |
111 ms |
212716 KB |
Output is correct |
22 |
Correct |
106 ms |
212784 KB |
Output is correct |
23 |
Correct |
103 ms |
212728 KB |
Output is correct |
24 |
Correct |
100 ms |
212684 KB |
Output is correct |
25 |
Correct |
101 ms |
212728 KB |
Output is correct |
26 |
Correct |
103 ms |
212804 KB |
Output is correct |
27 |
Correct |
101 ms |
212684 KB |
Output is correct |
28 |
Correct |
105 ms |
212676 KB |
Output is correct |
29 |
Correct |
106 ms |
212780 KB |
Output is correct |
30 |
Correct |
101 ms |
212720 KB |
Output is correct |
31 |
Correct |
101 ms |
212732 KB |
Output is correct |
32 |
Correct |
102 ms |
212688 KB |
Output is correct |
33 |
Correct |
104 ms |
212712 KB |
Output is correct |
34 |
Correct |
106 ms |
212428 KB |
Output is correct |
35 |
Correct |
115 ms |
212348 KB |
Output is correct |
36 |
Correct |
102 ms |
212488 KB |
Output is correct |
37 |
Correct |
102 ms |
212608 KB |
Output is correct |
38 |
Correct |
104 ms |
212440 KB |
Output is correct |
39 |
Correct |
110 ms |
212804 KB |
Output is correct |
40 |
Correct |
106 ms |
213948 KB |
Output is correct |
41 |
Correct |
102 ms |
214772 KB |
Output is correct |
42 |
Correct |
110 ms |
218972 KB |
Output is correct |
43 |
Correct |
112 ms |
219128 KB |
Output is correct |
44 |
Correct |
108 ms |
219240 KB |
Output is correct |
45 |
Correct |
106 ms |
219152 KB |
Output is correct |
46 |
Correct |
134 ms |
219192 KB |
Output is correct |
47 |
Correct |
115 ms |
215972 KB |
Output is correct |
48 |
Correct |
110 ms |
217520 KB |
Output is correct |
49 |
Correct |
104 ms |
218072 KB |
Output is correct |
50 |
Correct |
111 ms |
218080 KB |
Output is correct |
51 |
Correct |
114 ms |
217192 KB |
Output is correct |
52 |
Correct |
104 ms |
217112 KB |
Output is correct |
53 |
Correct |
105 ms |
219216 KB |
Output is correct |
54 |
Correct |
133 ms |
241544 KB |
Output is correct |
55 |
Correct |
162 ms |
236892 KB |
Output is correct |
56 |
Correct |
207 ms |
349696 KB |
Output is correct |
57 |
Correct |
217 ms |
350512 KB |
Output is correct |
58 |
Correct |
218 ms |
351092 KB |
Output is correct |
59 |
Correct |
230 ms |
351180 KB |
Output is correct |
60 |
Correct |
222 ms |
351348 KB |
Output is correct |
61 |
Correct |
181 ms |
289888 KB |
Output is correct |
62 |
Correct |
155 ms |
238952 KB |
Output is correct |
63 |
Correct |
226 ms |
322204 KB |
Output is correct |
64 |
Correct |
204 ms |
323456 KB |
Output is correct |
65 |
Correct |
207 ms |
293776 KB |
Output is correct |
66 |
Correct |
201 ms |
293692 KB |
Output is correct |
67 |
Correct |
207 ms |
351388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
212384 KB |
Output is correct |
2 |
Correct |
102 ms |
212464 KB |
Output is correct |
3 |
Correct |
114 ms |
212488 KB |
Output is correct |
4 |
Correct |
104 ms |
212364 KB |
Output is correct |
5 |
Correct |
110 ms |
212448 KB |
Output is correct |
6 |
Correct |
105 ms |
212360 KB |
Output is correct |
7 |
Correct |
106 ms |
212348 KB |
Output is correct |
8 |
Correct |
103 ms |
212468 KB |
Output is correct |
9 |
Correct |
102 ms |
212448 KB |
Output is correct |
10 |
Correct |
104 ms |
212412 KB |
Output is correct |
11 |
Correct |
103 ms |
212500 KB |
Output is correct |
12 |
Correct |
110 ms |
212348 KB |
Output is correct |
13 |
Correct |
324 ms |
281604 KB |
Output is correct |
14 |
Correct |
370 ms |
290468 KB |
Output is correct |
15 |
Correct |
440 ms |
302720 KB |
Output is correct |
16 |
Correct |
480 ms |
317748 KB |
Output is correct |
17 |
Correct |
591 ms |
334168 KB |
Output is correct |
18 |
Correct |
598 ms |
334124 KB |
Output is correct |
19 |
Correct |
595 ms |
334140 KB |
Output is correct |
20 |
Correct |
101 ms |
212428 KB |
Output is correct |
21 |
Correct |
111 ms |
212332 KB |
Output is correct |
22 |
Correct |
104 ms |
212472 KB |
Output is correct |
23 |
Correct |
112 ms |
212496 KB |
Output is correct |
24 |
Correct |
103 ms |
212612 KB |
Output is correct |
25 |
Correct |
100 ms |
212536 KB |
Output is correct |
26 |
Correct |
109 ms |
212820 KB |
Output is correct |
27 |
Correct |
106 ms |
212428 KB |
Output is correct |
28 |
Correct |
104 ms |
212536 KB |
Output is correct |
29 |
Correct |
109 ms |
212664 KB |
Output is correct |
30 |
Correct |
104 ms |
212564 KB |
Output is correct |
31 |
Correct |
104 ms |
212428 KB |
Output is correct |
32 |
Correct |
100 ms |
212632 KB |
Output is correct |
33 |
Correct |
104 ms |
212632 KB |
Output is correct |
34 |
Correct |
105 ms |
212412 KB |
Output is correct |
35 |
Correct |
105 ms |
212360 KB |
Output is correct |
36 |
Correct |
104 ms |
212564 KB |
Output is correct |
37 |
Correct |
101 ms |
212440 KB |
Output is correct |
38 |
Correct |
111 ms |
212716 KB |
Output is correct |
39 |
Correct |
106 ms |
212784 KB |
Output is correct |
40 |
Correct |
103 ms |
212728 KB |
Output is correct |
41 |
Correct |
100 ms |
212684 KB |
Output is correct |
42 |
Correct |
101 ms |
212728 KB |
Output is correct |
43 |
Correct |
103 ms |
212804 KB |
Output is correct |
44 |
Correct |
101 ms |
212684 KB |
Output is correct |
45 |
Correct |
105 ms |
212676 KB |
Output is correct |
46 |
Correct |
106 ms |
212780 KB |
Output is correct |
47 |
Correct |
101 ms |
212720 KB |
Output is correct |
48 |
Correct |
101 ms |
212732 KB |
Output is correct |
49 |
Correct |
102 ms |
212688 KB |
Output is correct |
50 |
Correct |
104 ms |
212712 KB |
Output is correct |
51 |
Correct |
106 ms |
212428 KB |
Output is correct |
52 |
Correct |
115 ms |
212348 KB |
Output is correct |
53 |
Correct |
102 ms |
212488 KB |
Output is correct |
54 |
Correct |
102 ms |
212608 KB |
Output is correct |
55 |
Correct |
104 ms |
212440 KB |
Output is correct |
56 |
Correct |
110 ms |
212804 KB |
Output is correct |
57 |
Correct |
106 ms |
213948 KB |
Output is correct |
58 |
Correct |
102 ms |
214772 KB |
Output is correct |
59 |
Correct |
110 ms |
218972 KB |
Output is correct |
60 |
Correct |
112 ms |
219128 KB |
Output is correct |
61 |
Correct |
108 ms |
219240 KB |
Output is correct |
62 |
Correct |
106 ms |
219152 KB |
Output is correct |
63 |
Correct |
134 ms |
219192 KB |
Output is correct |
64 |
Correct |
115 ms |
215972 KB |
Output is correct |
65 |
Correct |
110 ms |
217520 KB |
Output is correct |
66 |
Correct |
104 ms |
218072 KB |
Output is correct |
67 |
Correct |
111 ms |
218080 KB |
Output is correct |
68 |
Correct |
114 ms |
217192 KB |
Output is correct |
69 |
Correct |
104 ms |
217112 KB |
Output is correct |
70 |
Correct |
105 ms |
219216 KB |
Output is correct |
71 |
Correct |
133 ms |
241544 KB |
Output is correct |
72 |
Correct |
162 ms |
236892 KB |
Output is correct |
73 |
Correct |
207 ms |
349696 KB |
Output is correct |
74 |
Correct |
217 ms |
350512 KB |
Output is correct |
75 |
Correct |
218 ms |
351092 KB |
Output is correct |
76 |
Correct |
230 ms |
351180 KB |
Output is correct |
77 |
Correct |
222 ms |
351348 KB |
Output is correct |
78 |
Correct |
181 ms |
289888 KB |
Output is correct |
79 |
Correct |
155 ms |
238952 KB |
Output is correct |
80 |
Correct |
226 ms |
322204 KB |
Output is correct |
81 |
Correct |
204 ms |
323456 KB |
Output is correct |
82 |
Correct |
207 ms |
293776 KB |
Output is correct |
83 |
Correct |
201 ms |
293692 KB |
Output is correct |
84 |
Correct |
207 ms |
351388 KB |
Output is correct |
85 |
Correct |
405 ms |
549684 KB |
Output is correct |
86 |
Correct |
1074 ms |
1045992 KB |
Output is correct |
87 |
Correct |
861 ms |
1048344 KB |
Output is correct |
88 |
Correct |
813 ms |
1049684 KB |
Output is correct |
89 |
Correct |
908 ms |
1050504 KB |
Output is correct |
90 |
Correct |
815 ms |
1050844 KB |
Output is correct |
91 |
Correct |
527 ms |
381800 KB |
Output is correct |
92 |
Correct |
392 ms |
349864 KB |
Output is correct |
93 |
Correct |
806 ms |
838352 KB |
Output is correct |
94 |
Correct |
776 ms |
875848 KB |
Output is correct |
95 |
Correct |
733 ms |
686172 KB |
Output is correct |
96 |
Correct |
710 ms |
686256 KB |
Output is correct |
97 |
Correct |
809 ms |
1050828 KB |
Output is correct |