#include<bits/stdc++.h>
using namespace std;
long long br[2501][2501];
long long dp[2501][2501];
long long pos[2501][2501];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n,a,b,d;
string s;
cin >> n >> s >> a >> b >> d;
s = "0"+s;
for(long long i = 1; i <= n; i++) {
for(long long j = i+1; j <= n; j++) {
long long c = 0;
if(i > 1) {
c = br[j-1][i-1];
}
if(s[i] == s[j]) {
c++;
}
else {
c = 0;
}
c = min(c,j-i);
br[j][i] = c;
}
}
for(long long i = 1; i <= n; i++) {
for(long long j = 1; j <= n; j++) {
pos[i][j] = -1;
}
long long y = 1;
for(long long j = i-1; j >= 1; j--) {
while(y <= br[i][j]) {
pos[i][y] = j-y+1;
y++;
}
}
}
for(long long i = 1; i <= n; i++) {
for(long long j = 1; j <= n; j++) {
dp[i][j] = LLONG_MAX;
}
dp[i][i] = a;
for(long long j = i; j >= 1; j--) {
if(i != j) {
dp[j][i] = min(dp[j][i],dp[j+1][i]+a);
dp[j][i] = min(dp[j][i],dp[j][i-1]+a);
}
long long c = j,br = 1;
while(c >= 1) {
dp[c][i] = min(dp[c][i],dp[j][i]+br*d+b+a*((i-c+1)-br*(i-j+1)));
br++;
c = pos[c+(i-j)][i-j+1];
}
}
}
cout << dp[1][n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4560 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4560 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
84 ms |
115476 KB |
Output is correct |
4 |
Correct |
109 ms |
121640 KB |
Output is correct |
5 |
Correct |
113 ms |
133968 KB |
Output is correct |
6 |
Correct |
130 ms |
140104 KB |
Output is correct |
7 |
Correct |
155 ms |
147280 KB |
Output is correct |
8 |
Correct |
159 ms |
147280 KB |
Output is correct |
9 |
Correct |
158 ms |
147280 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
4444 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
0 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4560 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4560 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4448 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
0 ms |
6492 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
1 ms |
8540 KB |
Output is correct |
25 |
Correct |
1 ms |
8540 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
1 ms |
8536 KB |
Output is correct |
28 |
Correct |
1 ms |
8540 KB |
Output is correct |
29 |
Correct |
1 ms |
8540 KB |
Output is correct |
30 |
Correct |
1 ms |
8540 KB |
Output is correct |
31 |
Correct |
1 ms |
8540 KB |
Output is correct |
32 |
Correct |
1 ms |
8540 KB |
Output is correct |
33 |
Correct |
1 ms |
8540 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
4444 KB |
Output is correct |
36 |
Correct |
0 ms |
4444 KB |
Output is correct |
37 |
Correct |
0 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4560 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4560 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4448 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
0 ms |
6492 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
1 ms |
8540 KB |
Output is correct |
25 |
Correct |
1 ms |
8540 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
1 ms |
8536 KB |
Output is correct |
28 |
Correct |
1 ms |
8540 KB |
Output is correct |
29 |
Correct |
1 ms |
8540 KB |
Output is correct |
30 |
Correct |
1 ms |
8540 KB |
Output is correct |
31 |
Correct |
1 ms |
8540 KB |
Output is correct |
32 |
Correct |
1 ms |
8540 KB |
Output is correct |
33 |
Correct |
1 ms |
8540 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
4444 KB |
Output is correct |
36 |
Correct |
0 ms |
4444 KB |
Output is correct |
37 |
Correct |
0 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
40 |
Correct |
1 ms |
10588 KB |
Output is correct |
41 |
Correct |
2 ms |
16732 KB |
Output is correct |
42 |
Correct |
2 ms |
16732 KB |
Output is correct |
43 |
Correct |
2 ms |
16732 KB |
Output is correct |
44 |
Correct |
2 ms |
16732 KB |
Output is correct |
45 |
Correct |
2 ms |
16732 KB |
Output is correct |
46 |
Correct |
2 ms |
16732 KB |
Output is correct |
47 |
Correct |
3 ms |
16732 KB |
Output is correct |
48 |
Correct |
2 ms |
16732 KB |
Output is correct |
49 |
Correct |
2 ms |
16732 KB |
Output is correct |
50 |
Correct |
2 ms |
16728 KB |
Output is correct |
51 |
Correct |
2 ms |
16732 KB |
Output is correct |
52 |
Correct |
2 ms |
16732 KB |
Output is correct |
53 |
Correct |
1 ms |
16916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4560 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4560 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4448 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
0 ms |
6492 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
1 ms |
8540 KB |
Output is correct |
25 |
Correct |
1 ms |
8540 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
1 ms |
8536 KB |
Output is correct |
28 |
Correct |
1 ms |
8540 KB |
Output is correct |
29 |
Correct |
1 ms |
8540 KB |
Output is correct |
30 |
Correct |
1 ms |
8540 KB |
Output is correct |
31 |
Correct |
1 ms |
8540 KB |
Output is correct |
32 |
Correct |
1 ms |
8540 KB |
Output is correct |
33 |
Correct |
1 ms |
8540 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
4444 KB |
Output is correct |
36 |
Correct |
0 ms |
4444 KB |
Output is correct |
37 |
Correct |
0 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
40 |
Correct |
1 ms |
10588 KB |
Output is correct |
41 |
Correct |
2 ms |
16732 KB |
Output is correct |
42 |
Correct |
2 ms |
16732 KB |
Output is correct |
43 |
Correct |
2 ms |
16732 KB |
Output is correct |
44 |
Correct |
2 ms |
16732 KB |
Output is correct |
45 |
Correct |
2 ms |
16732 KB |
Output is correct |
46 |
Correct |
2 ms |
16732 KB |
Output is correct |
47 |
Correct |
3 ms |
16732 KB |
Output is correct |
48 |
Correct |
2 ms |
16732 KB |
Output is correct |
49 |
Correct |
2 ms |
16732 KB |
Output is correct |
50 |
Correct |
2 ms |
16728 KB |
Output is correct |
51 |
Correct |
2 ms |
16732 KB |
Output is correct |
52 |
Correct |
2 ms |
16732 KB |
Output is correct |
53 |
Correct |
1 ms |
16916 KB |
Output is correct |
54 |
Correct |
4 ms |
33116 KB |
Output is correct |
55 |
Correct |
25 ms |
66196 KB |
Output is correct |
56 |
Correct |
11 ms |
66136 KB |
Output is correct |
57 |
Correct |
11 ms |
66136 KB |
Output is correct |
58 |
Correct |
10 ms |
66140 KB |
Output is correct |
59 |
Correct |
9 ms |
66052 KB |
Output is correct |
60 |
Correct |
9 ms |
66048 KB |
Output is correct |
61 |
Correct |
11 ms |
66136 KB |
Output is correct |
62 |
Correct |
19 ms |
66148 KB |
Output is correct |
63 |
Correct |
9 ms |
66140 KB |
Output is correct |
64 |
Correct |
9 ms |
66140 KB |
Output is correct |
65 |
Correct |
14 ms |
66136 KB |
Output is correct |
66 |
Correct |
13 ms |
66136 KB |
Output is correct |
67 |
Correct |
9 ms |
66140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4560 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4560 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4448 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
84 ms |
115476 KB |
Output is correct |
14 |
Correct |
109 ms |
121640 KB |
Output is correct |
15 |
Correct |
113 ms |
133968 KB |
Output is correct |
16 |
Correct |
130 ms |
140104 KB |
Output is correct |
17 |
Correct |
155 ms |
147280 KB |
Output is correct |
18 |
Correct |
159 ms |
147280 KB |
Output is correct |
19 |
Correct |
158 ms |
147280 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
0 ms |
4444 KB |
Output is correct |
24 |
Correct |
0 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
0 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
0 ms |
6492 KB |
Output is correct |
31 |
Correct |
0 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
4440 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
6492 KB |
Output is correct |
37 |
Correct |
0 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
8540 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
40 |
Correct |
1 ms |
8540 KB |
Output is correct |
41 |
Correct |
1 ms |
8540 KB |
Output is correct |
42 |
Correct |
1 ms |
8540 KB |
Output is correct |
43 |
Correct |
1 ms |
8540 KB |
Output is correct |
44 |
Correct |
1 ms |
8536 KB |
Output is correct |
45 |
Correct |
1 ms |
8540 KB |
Output is correct |
46 |
Correct |
1 ms |
8540 KB |
Output is correct |
47 |
Correct |
1 ms |
8540 KB |
Output is correct |
48 |
Correct |
1 ms |
8540 KB |
Output is correct |
49 |
Correct |
1 ms |
8540 KB |
Output is correct |
50 |
Correct |
1 ms |
8540 KB |
Output is correct |
51 |
Correct |
0 ms |
2396 KB |
Output is correct |
52 |
Correct |
0 ms |
4444 KB |
Output is correct |
53 |
Correct |
0 ms |
4444 KB |
Output is correct |
54 |
Correct |
0 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
6492 KB |
Output is correct |
56 |
Correct |
1 ms |
8540 KB |
Output is correct |
57 |
Correct |
1 ms |
10588 KB |
Output is correct |
58 |
Correct |
2 ms |
16732 KB |
Output is correct |
59 |
Correct |
2 ms |
16732 KB |
Output is correct |
60 |
Correct |
2 ms |
16732 KB |
Output is correct |
61 |
Correct |
2 ms |
16732 KB |
Output is correct |
62 |
Correct |
2 ms |
16732 KB |
Output is correct |
63 |
Correct |
2 ms |
16732 KB |
Output is correct |
64 |
Correct |
3 ms |
16732 KB |
Output is correct |
65 |
Correct |
2 ms |
16732 KB |
Output is correct |
66 |
Correct |
2 ms |
16732 KB |
Output is correct |
67 |
Correct |
2 ms |
16728 KB |
Output is correct |
68 |
Correct |
2 ms |
16732 KB |
Output is correct |
69 |
Correct |
2 ms |
16732 KB |
Output is correct |
70 |
Correct |
1 ms |
16916 KB |
Output is correct |
71 |
Correct |
4 ms |
33116 KB |
Output is correct |
72 |
Correct |
25 ms |
66196 KB |
Output is correct |
73 |
Correct |
11 ms |
66136 KB |
Output is correct |
74 |
Correct |
11 ms |
66136 KB |
Output is correct |
75 |
Correct |
10 ms |
66140 KB |
Output is correct |
76 |
Correct |
9 ms |
66052 KB |
Output is correct |
77 |
Correct |
9 ms |
66048 KB |
Output is correct |
78 |
Correct |
11 ms |
66136 KB |
Output is correct |
79 |
Correct |
19 ms |
66148 KB |
Output is correct |
80 |
Correct |
9 ms |
66140 KB |
Output is correct |
81 |
Correct |
9 ms |
66140 KB |
Output is correct |
82 |
Correct |
14 ms |
66136 KB |
Output is correct |
83 |
Correct |
13 ms |
66136 KB |
Output is correct |
84 |
Correct |
9 ms |
66140 KB |
Output is correct |
85 |
Correct |
24 ms |
98900 KB |
Output is correct |
86 |
Correct |
57 ms |
147284 KB |
Output is correct |
87 |
Correct |
51 ms |
147280 KB |
Output is correct |
88 |
Correct |
46 ms |
147308 KB |
Output is correct |
89 |
Correct |
43 ms |
147264 KB |
Output is correct |
90 |
Correct |
41 ms |
147280 KB |
Output is correct |
91 |
Correct |
79 ms |
147284 KB |
Output is correct |
92 |
Correct |
91 ms |
147280 KB |
Output is correct |
93 |
Correct |
47 ms |
147284 KB |
Output is correct |
94 |
Correct |
48 ms |
147284 KB |
Output is correct |
95 |
Correct |
82 ms |
147252 KB |
Output is correct |
96 |
Correct |
85 ms |
147444 KB |
Output is correct |
97 |
Correct |
41 ms |
147068 KB |
Output is correct |