#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 dp[maxn][maxn];
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 len=1; len<n; len++)
for (int j=len; j<=n; j++)
{
int i=j-len+1;
int num = 1; int r = i-1;
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;
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';
}
cout<<dp[1][n]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Execution timed out |
3056 ms |
20744 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 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 |
0 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
332 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 |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
460 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 |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
456 KB |
Output is correct |
34 |
Correct |
1 ms |
320 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
328 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 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 |
0 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
332 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 |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
460 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 |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
456 KB |
Output is correct |
34 |
Correct |
1 ms |
320 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
328 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
14 ms |
1296 KB |
Output is correct |
42 |
Correct |
18 ms |
1236 KB |
Output is correct |
43 |
Correct |
16 ms |
1236 KB |
Output is correct |
44 |
Correct |
17 ms |
1236 KB |
Output is correct |
45 |
Correct |
17 ms |
1304 KB |
Output is correct |
46 |
Correct |
18 ms |
1304 KB |
Output is correct |
47 |
Correct |
13 ms |
1236 KB |
Output is correct |
48 |
Correct |
21 ms |
1236 KB |
Output is correct |
49 |
Correct |
16 ms |
1236 KB |
Output is correct |
50 |
Correct |
17 ms |
1236 KB |
Output is correct |
51 |
Correct |
14 ms |
1236 KB |
Output is correct |
52 |
Correct |
14 ms |
1300 KB |
Output is correct |
53 |
Correct |
17 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 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 |
0 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
332 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 |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
460 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 |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
456 KB |
Output is correct |
34 |
Correct |
1 ms |
320 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
328 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
14 ms |
1296 KB |
Output is correct |
42 |
Correct |
18 ms |
1236 KB |
Output is correct |
43 |
Correct |
16 ms |
1236 KB |
Output is correct |
44 |
Correct |
17 ms |
1236 KB |
Output is correct |
45 |
Correct |
17 ms |
1304 KB |
Output is correct |
46 |
Correct |
18 ms |
1304 KB |
Output is correct |
47 |
Correct |
13 ms |
1236 KB |
Output is correct |
48 |
Correct |
21 ms |
1236 KB |
Output is correct |
49 |
Correct |
16 ms |
1236 KB |
Output is correct |
50 |
Correct |
17 ms |
1236 KB |
Output is correct |
51 |
Correct |
14 ms |
1236 KB |
Output is correct |
52 |
Correct |
14 ms |
1300 KB |
Output is correct |
53 |
Correct |
17 ms |
1228 KB |
Output is correct |
54 |
Correct |
182 ms |
2908 KB |
Output is correct |
55 |
Correct |
1137 ms |
8276 KB |
Output is correct |
56 |
Correct |
2141 ms |
8276 KB |
Output is correct |
57 |
Correct |
2140 ms |
8276 KB |
Output is correct |
58 |
Correct |
2101 ms |
8276 KB |
Output is correct |
59 |
Correct |
2298 ms |
8400 KB |
Output is correct |
60 |
Correct |
2084 ms |
8276 KB |
Output is correct |
61 |
Correct |
1785 ms |
8276 KB |
Output is correct |
62 |
Correct |
1211 ms |
8276 KB |
Output is correct |
63 |
Correct |
1980 ms |
8276 KB |
Output is correct |
64 |
Correct |
1829 ms |
8276 KB |
Output is correct |
65 |
Correct |
1550 ms |
8276 KB |
Output is correct |
66 |
Correct |
1577 ms |
8276 KB |
Output is correct |
67 |
Correct |
2518 ms |
8276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
3056 ms |
20744 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |