#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=2505;
int N, A, B, C;
string s;
ll dp[MX][MX];
ll ndp[MX];
const int P=1e9+93, mod=2'147'483'647;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hsh[MX][MX], pw[MX], key[MX];
bool ok(int a, int b, int l, int r) {
if(l<=b) return false;
return hsh[a][b]==hsh[l][r];
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
for(int i=0;i<MX;i++)
for(int j=i;j<MX;j++)
dp[i][j]=2e18;
pw[0]=1;
for(int i=1;i<MX;i++) pw[i]=pw[i-1]*P%mod;
cin>>N;
cin>>s;
cin>>A>>B>>C;
s='#'+s;
for(int c=0;c<26;c++) key[c]=rng()%mod;
for(int l=1;l<=N;l++) {
for(int r=l;r<=N;r++) {
int p=r-l+1;
hsh[l][r]=hsh[l][r-1]+pw[p]*(key[s[r]-'a'])%mod;
hsh[l][r]%=mod;
}
}
for(int l=N;l>0;l--) {
for(int r=l;r<=N;r++) {
dp[l][r]=min(dp[l][r],dp[l][r-1]+A);
for(int k=r;k<=N;k++) ndp[k]=2e18;
int len=r-l+1;
ndp[r]=min(dp[l][r]+B+C,dp[l][r]+B+dp[l][r]);
for(int k=r+1;k<=N;k++) {
if(ok(l,r,k-len+1,k)) ndp[k]=min(ndp[k],ndp[k-len]+C);
ndp[k]=min(ndp[k],ndp[k-1]+A);
}
for(int k=r+1;k<=N;k++) {
dp[l][k]=min(dp[l][k],ndp[k]);
}
dp[l-1][r]=min(dp[l-1][r],dp[l][r]+A);
}
}
cout<<dp[1][N]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
50008 KB |
Output is correct |
2 |
Correct |
7 ms |
50012 KB |
Output is correct |
3 |
Correct |
7 ms |
50012 KB |
Output is correct |
4 |
Correct |
7 ms |
50104 KB |
Output is correct |
5 |
Correct |
8 ms |
50012 KB |
Output is correct |
6 |
Correct |
8 ms |
50008 KB |
Output is correct |
7 |
Correct |
7 ms |
50012 KB |
Output is correct |
8 |
Correct |
7 ms |
50012 KB |
Output is correct |
9 |
Correct |
7 ms |
50012 KB |
Output is correct |
10 |
Correct |
7 ms |
49892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
50012 KB |
Output is correct |
2 |
Correct |
7 ms |
50012 KB |
Output is correct |
3 |
Execution timed out |
3097 ms |
74952 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
50008 KB |
Output is correct |
2 |
Correct |
7 ms |
50012 KB |
Output is correct |
3 |
Correct |
7 ms |
50012 KB |
Output is correct |
4 |
Correct |
7 ms |
50104 KB |
Output is correct |
5 |
Correct |
8 ms |
50012 KB |
Output is correct |
6 |
Correct |
8 ms |
50008 KB |
Output is correct |
7 |
Correct |
7 ms |
50012 KB |
Output is correct |
8 |
Correct |
7 ms |
50012 KB |
Output is correct |
9 |
Correct |
7 ms |
50012 KB |
Output is correct |
10 |
Correct |
7 ms |
49892 KB |
Output is correct |
11 |
Correct |
7 ms |
50012 KB |
Output is correct |
12 |
Correct |
7 ms |
50012 KB |
Output is correct |
13 |
Correct |
7 ms |
50012 KB |
Output is correct |
14 |
Correct |
7 ms |
50096 KB |
Output is correct |
15 |
Correct |
7 ms |
50008 KB |
Output is correct |
16 |
Correct |
7 ms |
50140 KB |
Output is correct |
17 |
Correct |
7 ms |
50012 KB |
Output is correct |
18 |
Correct |
7 ms |
49896 KB |
Output is correct |
19 |
Correct |
8 ms |
50012 KB |
Output is correct |
20 |
Correct |
7 ms |
49912 KB |
Output is correct |
21 |
Correct |
7 ms |
50008 KB |
Output is correct |
22 |
Correct |
7 ms |
50012 KB |
Output is correct |
23 |
Correct |
7 ms |
50012 KB |
Output is correct |
24 |
Correct |
7 ms |
50012 KB |
Output is correct |
25 |
Correct |
7 ms |
50012 KB |
Output is correct |
26 |
Correct |
7 ms |
50012 KB |
Output is correct |
27 |
Correct |
6 ms |
50012 KB |
Output is correct |
28 |
Correct |
7 ms |
50188 KB |
Output is correct |
29 |
Correct |
11 ms |
50012 KB |
Output is correct |
30 |
Correct |
7 ms |
50264 KB |
Output is correct |
31 |
Correct |
7 ms |
50012 KB |
Output is correct |
32 |
Correct |
7 ms |
50012 KB |
Output is correct |
33 |
Correct |
8 ms |
50012 KB |
Output is correct |
34 |
Correct |
7 ms |
50012 KB |
Output is correct |
35 |
Correct |
7 ms |
50012 KB |
Output is correct |
36 |
Correct |
7 ms |
50012 KB |
Output is correct |
37 |
Correct |
7 ms |
50012 KB |
Output is correct |
38 |
Correct |
7 ms |
50012 KB |
Output is correct |
39 |
Correct |
7 ms |
50012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
50008 KB |
Output is correct |
2 |
Correct |
7 ms |
50012 KB |
Output is correct |
3 |
Correct |
7 ms |
50012 KB |
Output is correct |
4 |
Correct |
7 ms |
50104 KB |
Output is correct |
5 |
Correct |
8 ms |
50012 KB |
Output is correct |
6 |
Correct |
8 ms |
50008 KB |
Output is correct |
7 |
Correct |
7 ms |
50012 KB |
Output is correct |
8 |
Correct |
7 ms |
50012 KB |
Output is correct |
9 |
Correct |
7 ms |
50012 KB |
Output is correct |
10 |
Correct |
7 ms |
49892 KB |
Output is correct |
11 |
Correct |
7 ms |
50012 KB |
Output is correct |
12 |
Correct |
7 ms |
50012 KB |
Output is correct |
13 |
Correct |
7 ms |
50012 KB |
Output is correct |
14 |
Correct |
7 ms |
50096 KB |
Output is correct |
15 |
Correct |
7 ms |
50008 KB |
Output is correct |
16 |
Correct |
7 ms |
50140 KB |
Output is correct |
17 |
Correct |
7 ms |
50012 KB |
Output is correct |
18 |
Correct |
7 ms |
49896 KB |
Output is correct |
19 |
Correct |
8 ms |
50012 KB |
Output is correct |
20 |
Correct |
7 ms |
49912 KB |
Output is correct |
21 |
Correct |
7 ms |
50008 KB |
Output is correct |
22 |
Correct |
7 ms |
50012 KB |
Output is correct |
23 |
Correct |
7 ms |
50012 KB |
Output is correct |
24 |
Correct |
7 ms |
50012 KB |
Output is correct |
25 |
Correct |
7 ms |
50012 KB |
Output is correct |
26 |
Correct |
7 ms |
50012 KB |
Output is correct |
27 |
Correct |
6 ms |
50012 KB |
Output is correct |
28 |
Correct |
7 ms |
50188 KB |
Output is correct |
29 |
Correct |
11 ms |
50012 KB |
Output is correct |
30 |
Correct |
7 ms |
50264 KB |
Output is correct |
31 |
Correct |
7 ms |
50012 KB |
Output is correct |
32 |
Correct |
7 ms |
50012 KB |
Output is correct |
33 |
Correct |
8 ms |
50012 KB |
Output is correct |
34 |
Correct |
7 ms |
50012 KB |
Output is correct |
35 |
Correct |
7 ms |
50012 KB |
Output is correct |
36 |
Correct |
7 ms |
50012 KB |
Output is correct |
37 |
Correct |
7 ms |
50012 KB |
Output is correct |
38 |
Correct |
7 ms |
50012 KB |
Output is correct |
39 |
Correct |
7 ms |
50012 KB |
Output is correct |
40 |
Correct |
7 ms |
50264 KB |
Output is correct |
41 |
Correct |
12 ms |
50976 KB |
Output is correct |
42 |
Correct |
11 ms |
51036 KB |
Output is correct |
43 |
Correct |
11 ms |
51040 KB |
Output is correct |
44 |
Correct |
11 ms |
51072 KB |
Output is correct |
45 |
Correct |
11 ms |
51036 KB |
Output is correct |
46 |
Correct |
12 ms |
51036 KB |
Output is correct |
47 |
Correct |
11 ms |
51036 KB |
Output is correct |
48 |
Correct |
11 ms |
51036 KB |
Output is correct |
49 |
Correct |
11 ms |
50992 KB |
Output is correct |
50 |
Correct |
11 ms |
51036 KB |
Output is correct |
51 |
Correct |
11 ms |
51036 KB |
Output is correct |
52 |
Correct |
11 ms |
51036 KB |
Output is correct |
53 |
Correct |
12 ms |
51036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
50008 KB |
Output is correct |
2 |
Correct |
7 ms |
50012 KB |
Output is correct |
3 |
Correct |
7 ms |
50012 KB |
Output is correct |
4 |
Correct |
7 ms |
50104 KB |
Output is correct |
5 |
Correct |
8 ms |
50012 KB |
Output is correct |
6 |
Correct |
8 ms |
50008 KB |
Output is correct |
7 |
Correct |
7 ms |
50012 KB |
Output is correct |
8 |
Correct |
7 ms |
50012 KB |
Output is correct |
9 |
Correct |
7 ms |
50012 KB |
Output is correct |
10 |
Correct |
7 ms |
49892 KB |
Output is correct |
11 |
Correct |
7 ms |
50012 KB |
Output is correct |
12 |
Correct |
7 ms |
50012 KB |
Output is correct |
13 |
Correct |
7 ms |
50012 KB |
Output is correct |
14 |
Correct |
7 ms |
50096 KB |
Output is correct |
15 |
Correct |
7 ms |
50008 KB |
Output is correct |
16 |
Correct |
7 ms |
50140 KB |
Output is correct |
17 |
Correct |
7 ms |
50012 KB |
Output is correct |
18 |
Correct |
7 ms |
49896 KB |
Output is correct |
19 |
Correct |
8 ms |
50012 KB |
Output is correct |
20 |
Correct |
7 ms |
49912 KB |
Output is correct |
21 |
Correct |
7 ms |
50008 KB |
Output is correct |
22 |
Correct |
7 ms |
50012 KB |
Output is correct |
23 |
Correct |
7 ms |
50012 KB |
Output is correct |
24 |
Correct |
7 ms |
50012 KB |
Output is correct |
25 |
Correct |
7 ms |
50012 KB |
Output is correct |
26 |
Correct |
7 ms |
50012 KB |
Output is correct |
27 |
Correct |
6 ms |
50012 KB |
Output is correct |
28 |
Correct |
7 ms |
50188 KB |
Output is correct |
29 |
Correct |
11 ms |
50012 KB |
Output is correct |
30 |
Correct |
7 ms |
50264 KB |
Output is correct |
31 |
Correct |
7 ms |
50012 KB |
Output is correct |
32 |
Correct |
7 ms |
50012 KB |
Output is correct |
33 |
Correct |
8 ms |
50012 KB |
Output is correct |
34 |
Correct |
7 ms |
50012 KB |
Output is correct |
35 |
Correct |
7 ms |
50012 KB |
Output is correct |
36 |
Correct |
7 ms |
50012 KB |
Output is correct |
37 |
Correct |
7 ms |
50012 KB |
Output is correct |
38 |
Correct |
7 ms |
50012 KB |
Output is correct |
39 |
Correct |
7 ms |
50012 KB |
Output is correct |
40 |
Correct |
7 ms |
50264 KB |
Output is correct |
41 |
Correct |
12 ms |
50976 KB |
Output is correct |
42 |
Correct |
11 ms |
51036 KB |
Output is correct |
43 |
Correct |
11 ms |
51040 KB |
Output is correct |
44 |
Correct |
11 ms |
51072 KB |
Output is correct |
45 |
Correct |
11 ms |
51036 KB |
Output is correct |
46 |
Correct |
12 ms |
51036 KB |
Output is correct |
47 |
Correct |
11 ms |
51036 KB |
Output is correct |
48 |
Correct |
11 ms |
51036 KB |
Output is correct |
49 |
Correct |
11 ms |
50992 KB |
Output is correct |
50 |
Correct |
11 ms |
51036 KB |
Output is correct |
51 |
Correct |
11 ms |
51036 KB |
Output is correct |
52 |
Correct |
11 ms |
51036 KB |
Output is correct |
53 |
Correct |
12 ms |
51036 KB |
Output is correct |
54 |
Correct |
54 ms |
52572 KB |
Output is correct |
55 |
Correct |
441 ms |
59052 KB |
Output is correct |
56 |
Correct |
423 ms |
59216 KB |
Output is correct |
57 |
Correct |
426 ms |
59060 KB |
Output is correct |
58 |
Correct |
421 ms |
59060 KB |
Output is correct |
59 |
Correct |
411 ms |
59052 KB |
Output is correct |
60 |
Correct |
410 ms |
59052 KB |
Output is correct |
61 |
Correct |
427 ms |
59052 KB |
Output is correct |
62 |
Correct |
458 ms |
59060 KB |
Output is correct |
63 |
Correct |
420 ms |
59152 KB |
Output is correct |
64 |
Correct |
464 ms |
59052 KB |
Output is correct |
65 |
Correct |
431 ms |
58968 KB |
Output is correct |
66 |
Correct |
411 ms |
59052 KB |
Output is correct |
67 |
Correct |
400 ms |
58972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
50008 KB |
Output is correct |
2 |
Correct |
7 ms |
50012 KB |
Output is correct |
3 |
Correct |
7 ms |
50012 KB |
Output is correct |
4 |
Correct |
7 ms |
50104 KB |
Output is correct |
5 |
Correct |
8 ms |
50012 KB |
Output is correct |
6 |
Correct |
8 ms |
50008 KB |
Output is correct |
7 |
Correct |
7 ms |
50012 KB |
Output is correct |
8 |
Correct |
7 ms |
50012 KB |
Output is correct |
9 |
Correct |
7 ms |
50012 KB |
Output is correct |
10 |
Correct |
7 ms |
49892 KB |
Output is correct |
11 |
Correct |
7 ms |
50012 KB |
Output is correct |
12 |
Correct |
7 ms |
50012 KB |
Output is correct |
13 |
Execution timed out |
3097 ms |
74952 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |