#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3rfKJyA ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
//snuke's mod int
template <ll mod>
struct modint{
ll x; // typedef long long ll;
modint(ll x=0):x((x%mod+mod)%mod){}
modint operator-()const{return modint(-x);}
modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
modint operator+(const modint a)const{modint res(*this); return res+=a;}
modint operator-(const modint a)const{modint res(*this); return res-=a;}
modint operator*(const modint a)const{modint res(*this); return res*=a;}
modint pow(ll n)const{
modint res=1,x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
modint inv()const{return pow(mod-2);}
};
// include mint here
using hshmint=modint<1000000007>;
using hshmint1=modint<998244353>;
using magicpair=pair<hshmint,hshmint1>;
struct rollhsh{
hshmint _p=9973; // base
hshmint1 _p1=257; // base1
vec(hshmint) pw,invpw;
vec(hshmint1) pw1,invpw1;
vec(magicpair) hsh;
rollhsh(string s=""){
init(s);
}
void init(string s){
// put this out in global if multiple testcases
hsh.clear();
pw.clear();
invpw.clear();
pw1.clear();
invpw1.clear();
hsh.resize(sz(s)+1);
pw.resize(sz(s)+1);
invpw.resize(sz(s)+1);
pw1.resize(sz(s)+1);
invpw1.resize(sz(s)+1);
hsh[0]={1,1};
pw[0]=1;
pw1[0]=1;
invpw[0]=1;
invpw1[0]=1;
hshmint _invbase=_p.x;
hshmint1 _invbase1=_p1.x;
_invbase=_invbase.inv(); _invbase1=_invbase1.inv();
rep(i,sz(s)){
pw[i+1]=pw[i]*_p;
pw1[i+1]=pw1[i]*_p1;
invpw[i+1]=invpw[i]*_invbase;
invpw1[i+1]=invpw1[i]*_invbase1;
hshmint x=(int)(s[i]-'a'+1);
hshmint1 x1=(int)(s[i]-'a'+1);
hsh[i+1]={hsh[i].fi+pw[i]*x,hsh[i].se+pw1[i]*x1};
}
}
pair<ll,ll> get(int l,int r){
// from - to , inclusive
hshmint _f=invpw[l]*(hsh[r+1].fi-hsh[l].fi);
hshmint1 _s=invpw1[l]*(hsh[r+1].se-hsh[l].se);
return {_f.x,_s.x};
}
};
signed main(){
_3rfKJyA;
int n;
cin>>n;
string s;
cin>>s;
int c0,c1,c2;
cin>>c0>>c1>>c2;
using pll=pair<ll,ll>;
std::map<pll,vi> mp;
rollhsh hsh(s);
rep(i,n){
rng(j,i,n){
mp[hsh.get(i,j)].pb(i);
}
}
vec(vi) nxt(n,vi(n,-1));
rep(i,n){
rng(j,i,n){
pll p=hsh.get(i,j);
auto it=upper_bound(mp[p].begin(),mp[p].end(),j);
if(it!=mp[p].end()){
nxt[i][j]=*it;
}
}
}
const int inf=numeric_limits<int>::max();
vec(vi) f(n,vi(n,inf)),g;
g=f;
for(int len=1;len<=n;len++){
for(int i=0;i<n;i++){
if(i+len-1>=n){
break;
}
int j=i+len-1;
if(len>1){
g[i][j]=min({g[i][j],g[i+1][j],g[i][j-1]});
}
f[i][j]=len*c0;
if(g[i][j]!=inf){
f[i][j]=min(f[i][j],len*c0+g[i][j]);
}
int cnt=1;
int nj=i;
while(1){
g[i][nj+len-1]=min(g[i][nj+len-1],-len*c0*cnt+cnt*c2+f[i][i+len-1]+c1);
nj=nxt[nj][nj+len-1];
if(nj==-1) break;
cnt+=1;
}
}
}
print(f[0][n-1]);
// // cout<<f[0][1]<<"\n";
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
974 ms |
96784 KB |
Output is correct |
4 |
Correct |
1208 ms |
112756 KB |
Output is correct |
5 |
Correct |
1511 ms |
132480 KB |
Output is correct |
6 |
Correct |
1798 ms |
155164 KB |
Output is correct |
7 |
Correct |
2101 ms |
181476 KB |
Output is correct |
8 |
Correct |
2204 ms |
181476 KB |
Output is correct |
9 |
Correct |
2206 ms |
181472 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
3 ms |
832 KB |
Output is correct |
41 |
Correct |
5 ms |
1492 KB |
Output is correct |
42 |
Correct |
14 ms |
3284 KB |
Output is correct |
43 |
Correct |
15 ms |
3300 KB |
Output is correct |
44 |
Correct |
16 ms |
3404 KB |
Output is correct |
45 |
Correct |
16 ms |
3332 KB |
Output is correct |
46 |
Correct |
16 ms |
3344 KB |
Output is correct |
47 |
Correct |
12 ms |
2028 KB |
Output is correct |
48 |
Correct |
16 ms |
2516 KB |
Output is correct |
49 |
Correct |
15 ms |
2900 KB |
Output is correct |
50 |
Correct |
15 ms |
2828 KB |
Output is correct |
51 |
Correct |
14 ms |
2516 KB |
Output is correct |
52 |
Correct |
14 ms |
2568 KB |
Output is correct |
53 |
Correct |
17 ms |
3468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
3 ms |
832 KB |
Output is correct |
41 |
Correct |
5 ms |
1492 KB |
Output is correct |
42 |
Correct |
14 ms |
3284 KB |
Output is correct |
43 |
Correct |
15 ms |
3300 KB |
Output is correct |
44 |
Correct |
16 ms |
3404 KB |
Output is correct |
45 |
Correct |
16 ms |
3332 KB |
Output is correct |
46 |
Correct |
16 ms |
3344 KB |
Output is correct |
47 |
Correct |
12 ms |
2028 KB |
Output is correct |
48 |
Correct |
16 ms |
2516 KB |
Output is correct |
49 |
Correct |
15 ms |
2900 KB |
Output is correct |
50 |
Correct |
15 ms |
2828 KB |
Output is correct |
51 |
Correct |
14 ms |
2516 KB |
Output is correct |
52 |
Correct |
14 ms |
2568 KB |
Output is correct |
53 |
Correct |
17 ms |
3468 KB |
Output is correct |
54 |
Correct |
123 ms |
15660 KB |
Output is correct |
55 |
Correct |
214 ms |
29388 KB |
Output is correct |
56 |
Correct |
1109 ms |
77804 KB |
Output is correct |
57 |
Correct |
1107 ms |
78264 KB |
Output is correct |
58 |
Correct |
1161 ms |
78508 KB |
Output is correct |
59 |
Correct |
1200 ms |
78604 KB |
Output is correct |
60 |
Correct |
1115 ms |
78640 KB |
Output is correct |
61 |
Correct |
827 ms |
51660 KB |
Output is correct |
62 |
Correct |
256 ms |
30252 KB |
Output is correct |
63 |
Correct |
1084 ms |
65408 KB |
Output is correct |
64 |
Correct |
1114 ms |
66264 KB |
Output is correct |
65 |
Correct |
1049 ms |
52896 KB |
Output is correct |
66 |
Correct |
995 ms |
53004 KB |
Output is correct |
67 |
Correct |
1226 ms |
78552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
974 ms |
96784 KB |
Output is correct |
14 |
Correct |
1208 ms |
112756 KB |
Output is correct |
15 |
Correct |
1511 ms |
132480 KB |
Output is correct |
16 |
Correct |
1798 ms |
155164 KB |
Output is correct |
17 |
Correct |
2101 ms |
181476 KB |
Output is correct |
18 |
Correct |
2204 ms |
181476 KB |
Output is correct |
19 |
Correct |
2206 ms |
181472 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
456 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
320 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
324 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
3 ms |
832 KB |
Output is correct |
58 |
Correct |
5 ms |
1492 KB |
Output is correct |
59 |
Correct |
14 ms |
3284 KB |
Output is correct |
60 |
Correct |
15 ms |
3300 KB |
Output is correct |
61 |
Correct |
16 ms |
3404 KB |
Output is correct |
62 |
Correct |
16 ms |
3332 KB |
Output is correct |
63 |
Correct |
16 ms |
3344 KB |
Output is correct |
64 |
Correct |
12 ms |
2028 KB |
Output is correct |
65 |
Correct |
16 ms |
2516 KB |
Output is correct |
66 |
Correct |
15 ms |
2900 KB |
Output is correct |
67 |
Correct |
15 ms |
2828 KB |
Output is correct |
68 |
Correct |
14 ms |
2516 KB |
Output is correct |
69 |
Correct |
14 ms |
2568 KB |
Output is correct |
70 |
Correct |
17 ms |
3468 KB |
Output is correct |
71 |
Correct |
123 ms |
15660 KB |
Output is correct |
72 |
Correct |
214 ms |
29388 KB |
Output is correct |
73 |
Correct |
1109 ms |
77804 KB |
Output is correct |
74 |
Correct |
1107 ms |
78264 KB |
Output is correct |
75 |
Correct |
1161 ms |
78508 KB |
Output is correct |
76 |
Correct |
1200 ms |
78604 KB |
Output is correct |
77 |
Correct |
1115 ms |
78640 KB |
Output is correct |
78 |
Correct |
827 ms |
51660 KB |
Output is correct |
79 |
Correct |
256 ms |
30252 KB |
Output is correct |
80 |
Correct |
1084 ms |
65408 KB |
Output is correct |
81 |
Correct |
1114 ms |
66264 KB |
Output is correct |
82 |
Correct |
1049 ms |
52896 KB |
Output is correct |
83 |
Correct |
995 ms |
53004 KB |
Output is correct |
84 |
Correct |
1226 ms |
78552 KB |
Output is correct |
85 |
Execution timed out |
3074 ms |
155484 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |