#include <bits/stdc++.h>
#define int long long
using namespace std;
const int base = 101LL;
const int mod = 1e9 + 7;
int n,a,b,c;
string s;
const int maxn = 2510;
int pre[maxn][maxn],nnext[maxn][maxn],dp[maxn][maxn];
struct fen_tree2
{
int f[maxn][maxn];
void reset(int rt, int sz)
{
fill_n(f[rt],sz+1,1e18);
}
void update(int rt, int x, int val)
{
while (x>0)
{
f[rt][x]=min(f[rt][x],val);
x-=(x&-x);
}
}
int get(int rt, int sz, int x)
{
int temp=1e18;
while (x<=sz)
{
temp=min(temp,f[rt][x]);
x+=(x&-x);
}
return temp;
}
};
fen_tree2 dp2;
map<int,int> mp;
struct fen_tree3
{
int f[maxn][maxn];
void reset(int rt, int sz)
{
fill_n(f[rt],sz+1,1e18);
}
void update(int rt, int sz, int x, int val)
{
while (x<=sz)
{
f[rt][x]=min(f[rt][x],val);
x+=(x&-x);
}
}
int get(int rt, int x)
{
int temp=1e18;
while (x>0)
{
temp=min(temp,f[rt][x]);
x-=(x&-x);
}
return temp;
}
};
fen_tree3 dp3;
int h[maxn],p[maxn];
inline int get(int l, int r)
{
return (h[r]-(h[l-1]*p[r-l+1]%mod)+mod)%mod;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>n;
cin>>s;
cin>>a>>b>>c;
p[0]=1LL;
for (int i=1; i<=n; i++) p[i]=p[i-1]*base%mod;
h[0]=0LL;
for (int i=1; i<=n; i++) h[i]=(h[i-1]*base%mod + (s[i-1]-'a'+1))%mod;
for (int i=1; i<=n; i++)
for (int j=i; j<=n; j++) dp[i][j] = a*(j-i+1);
for (int i=1; i<=n; i++)
{
dp2.reset(i,i);
dp3.reset(i,n-i+1);
}
for (int len=1; len<n; len++)
{
mp.clear();
for (int i=1; i+len-1<=n; i++)
{
if (i>len) mp[get(i-len,i-1)] = i-len;
int j=i+len-1;
if (!mp.count(get(i,j))) pre[i][j]=-1;
else pre[i][j]=mp[get(i,j)];
}
mp.clear();
for (int j=n; j>=len; j--)
{
int i=j-len+1;
if (n-j>=len) mp[get(j+1,j+len)] = j+len;
if (!mp.count(get(i,j))) nnext[i][j]=-1;
else nnext[i][j]=mp[get(i,j)];
}
}
for (int len=1; len<n; len++)
for (int j=len; j<=n; j++)
{
int i=j-len+1;
int val = min(dp[i][j],min(dp2.get(j,j,i)-i*a,dp3.get(i,j-i+1)+j*a));
// cerr<<i<<' '<<j<<" := "<<val<<endl;
int num = 1;
int last = i-1;
int x=i; int y=j;
while (pre[x][y]!=-1)
{
int z = pre[x][y];
// cerr<<last<<' '<<j<<endl;
dp2.update(j,last,val+b+num*c+(j+1-(num*len))*a);
last=z;
num++;
x=z; y=z+len-1;
}
dp2.update(j,last,val+b+num*c+(j+1-(num*len))*a);
dp2.update(j,i-1,val+i*a);
// cerr<<":>"<<endl;
// for (int z=i-1; z>=1; z--)
// {
// if (r-z+1>=len)
// {
// if (get(z,z+len-1) == get(i,j))
// {
// num++;
// r=z-1;
// }
// }
// dp[z][j]=min(dp[z][j],dp[i][j]+b+num*c+(j-z+1-(num*len))*a);
// dp[z][j]=min(dp[z][j],dp[i][j]+(i-z)*a);
// }
num=1;
last=j+1;
x=i; y=j;
while (nnext[x][y]!=-1)
{
int z=nnext[x][y];
dp3.update(i,n-i+1,last-i+1,val+b+num*c+(-i+1-(num*len))*a);
last=z;
num++;
x=z-len+1; y=z;
}
// cerr<<i<<' '<<last<<' '<<num<<endl;
dp3.update(i,n-i+1,last-i+1,val+b+num*c+(-i+1-(num*len))*a);
dp3.update(i,n-i+1,2,val+(-j)*a);
// cerr<<":>"<<endl;
// int l=j+1;
// for (int z=j+1; z<=n; z++)
// {
// if (z-l+1>=len)
// {
// if (get(z-len+1,z) == get(i,j))
// {
// num++;
// l=z+1;
// }
// }
// dp[i][z]=min(dp[i][z],dp[i][j]+b+num*c+(z-i+1-(num*len))*a);
// dp[i][z]=min(dp[i][z],dp[i][j]+(z-j)*a);
// }
// cout<<i<<' '<<j<<" := "<<dp[i][j]<<'\n';
}
int ans = min(dp[1][n],min(dp2.get(n,n,1)-1*a,dp3.get(1,n)+n*a));
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
608 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1165 ms |
102536 KB |
Output is correct |
4 |
Correct |
1377 ms |
116596 KB |
Output is correct |
5 |
Correct |
1784 ms |
132448 KB |
Output is correct |
6 |
Correct |
2198 ms |
149420 KB |
Output is correct |
7 |
Correct |
2582 ms |
168012 KB |
Output is correct |
8 |
Correct |
2979 ms |
168012 KB |
Output is correct |
9 |
Correct |
2596 ms |
168016 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
852 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
976 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
976 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
2132 KB |
Output is correct |
41 |
Correct |
6 ms |
5076 KB |
Output is correct |
42 |
Correct |
7 ms |
5068 KB |
Output is correct |
43 |
Correct |
7 ms |
5072 KB |
Output is correct |
44 |
Correct |
9 ms |
5076 KB |
Output is correct |
45 |
Correct |
7 ms |
5076 KB |
Output is correct |
46 |
Correct |
9 ms |
5072 KB |
Output is correct |
47 |
Correct |
6 ms |
5152 KB |
Output is correct |
48 |
Correct |
6 ms |
5076 KB |
Output is correct |
49 |
Correct |
7 ms |
5076 KB |
Output is correct |
50 |
Correct |
7 ms |
5076 KB |
Output is correct |
51 |
Correct |
7 ms |
5076 KB |
Output is correct |
52 |
Correct |
7 ms |
5076 KB |
Output is correct |
53 |
Correct |
7 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
976 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
1 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
852 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
464 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
2132 KB |
Output is correct |
41 |
Correct |
6 ms |
5076 KB |
Output is correct |
42 |
Correct |
7 ms |
5068 KB |
Output is correct |
43 |
Correct |
7 ms |
5072 KB |
Output is correct |
44 |
Correct |
9 ms |
5076 KB |
Output is correct |
45 |
Correct |
7 ms |
5076 KB |
Output is correct |
46 |
Correct |
9 ms |
5072 KB |
Output is correct |
47 |
Correct |
6 ms |
5152 KB |
Output is correct |
48 |
Correct |
6 ms |
5076 KB |
Output is correct |
49 |
Correct |
7 ms |
5076 KB |
Output is correct |
50 |
Correct |
7 ms |
5076 KB |
Output is correct |
51 |
Correct |
7 ms |
5076 KB |
Output is correct |
52 |
Correct |
7 ms |
5076 KB |
Output is correct |
53 |
Correct |
7 ms |
5076 KB |
Output is correct |
54 |
Correct |
35 ms |
13188 KB |
Output is correct |
55 |
Correct |
208 ms |
40068 KB |
Output is correct |
56 |
Correct |
186 ms |
40136 KB |
Output is correct |
57 |
Correct |
197 ms |
40216 KB |
Output is correct |
58 |
Correct |
188 ms |
40012 KB |
Output is correct |
59 |
Correct |
179 ms |
40188 KB |
Output is correct |
60 |
Correct |
178 ms |
40012 KB |
Output is correct |
61 |
Correct |
188 ms |
40060 KB |
Output is correct |
62 |
Correct |
203 ms |
40076 KB |
Output is correct |
63 |
Correct |
178 ms |
40108 KB |
Output is correct |
64 |
Correct |
190 ms |
40220 KB |
Output is correct |
65 |
Correct |
213 ms |
40080 KB |
Output is correct |
66 |
Correct |
240 ms |
40092 KB |
Output is correct |
67 |
Correct |
174 ms |
40136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
608 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1165 ms |
102536 KB |
Output is correct |
14 |
Correct |
1377 ms |
116596 KB |
Output is correct |
15 |
Correct |
1784 ms |
132448 KB |
Output is correct |
16 |
Correct |
2198 ms |
149420 KB |
Output is correct |
17 |
Correct |
2582 ms |
168012 KB |
Output is correct |
18 |
Correct |
2979 ms |
168012 KB |
Output is correct |
19 |
Correct |
2596 ms |
168016 KB |
Output is correct |
20 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
980 KB |
Output is correct |
39 |
Correct |
1 ms |
980 KB |
Output is correct |
40 |
Correct |
1 ms |
976 KB |
Output is correct |
41 |
Correct |
1 ms |
980 KB |
Output is correct |
42 |
Correct |
1 ms |
972 KB |
Output is correct |
43 |
Correct |
1 ms |
852 KB |
Output is correct |
44 |
Correct |
1 ms |
852 KB |
Output is correct |
45 |
Correct |
1 ms |
980 KB |
Output is correct |
46 |
Correct |
1 ms |
980 KB |
Output is correct |
47 |
Correct |
1 ms |
980 KB |
Output is correct |
48 |
Correct |
1 ms |
980 KB |
Output is correct |
49 |
Correct |
1 ms |
980 KB |
Output is correct |
50 |
Correct |
1 ms |
852 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
464 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
56 |
Correct |
1 ms |
980 KB |
Output is correct |
57 |
Correct |
2 ms |
2132 KB |
Output is correct |
58 |
Correct |
6 ms |
5076 KB |
Output is correct |
59 |
Correct |
7 ms |
5068 KB |
Output is correct |
60 |
Correct |
7 ms |
5072 KB |
Output is correct |
61 |
Correct |
9 ms |
5076 KB |
Output is correct |
62 |
Correct |
7 ms |
5076 KB |
Output is correct |
63 |
Correct |
9 ms |
5072 KB |
Output is correct |
64 |
Correct |
6 ms |
5152 KB |
Output is correct |
65 |
Correct |
6 ms |
5076 KB |
Output is correct |
66 |
Correct |
7 ms |
5076 KB |
Output is correct |
67 |
Correct |
7 ms |
5076 KB |
Output is correct |
68 |
Correct |
7 ms |
5076 KB |
Output is correct |
69 |
Correct |
7 ms |
5076 KB |
Output is correct |
70 |
Correct |
7 ms |
5076 KB |
Output is correct |
71 |
Correct |
35 ms |
13188 KB |
Output is correct |
72 |
Correct |
208 ms |
40068 KB |
Output is correct |
73 |
Correct |
186 ms |
40136 KB |
Output is correct |
74 |
Correct |
197 ms |
40216 KB |
Output is correct |
75 |
Correct |
188 ms |
40012 KB |
Output is correct |
76 |
Correct |
179 ms |
40188 KB |
Output is correct |
77 |
Correct |
178 ms |
40012 KB |
Output is correct |
78 |
Correct |
188 ms |
40060 KB |
Output is correct |
79 |
Correct |
203 ms |
40076 KB |
Output is correct |
80 |
Correct |
178 ms |
40108 KB |
Output is correct |
81 |
Correct |
190 ms |
40220 KB |
Output is correct |
82 |
Correct |
213 ms |
40080 KB |
Output is correct |
83 |
Correct |
240 ms |
40092 KB |
Output is correct |
84 |
Correct |
174 ms |
40136 KB |
Output is correct |
85 |
Correct |
513 ms |
81116 KB |
Output is correct |
86 |
Correct |
1663 ms |
168160 KB |
Output is correct |
87 |
Correct |
1474 ms |
168268 KB |
Output is correct |
88 |
Correct |
1491 ms |
168120 KB |
Output is correct |
89 |
Incorrect |
1454 ms |
168164 KB |
Output isn't correct |
90 |
Halted |
0 ms |
0 KB |
- |