#include <bits/stdc++.h>
using namespace std;
int pre[2504][2504];
long long dp[2504][2504];
int pw(int x, int y, int mod){
if(!y) return 1;
int gop = 1;
if(y % 2){
gop = x;
y -= 1;
}
int val = pw(x, y / 2, mod);
return (long long)val * val % mod * gop % mod;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
memset(pre, -1, sizeof(pre));
int n; cin >> n;
string s; cin >> s;
int a, b, c; cin >> a >> b >> c;
int p = 998244353, mod = (int)1e9 + 7;
int invp = pw(p, mod - 2, mod);
for(int i = 1; i <= n; ++i){
int pi = 1;
for(int j = 0; j < i; ++j){
pi = (long long)pi * p % mod;
}
int now = 0;
for(int j = i - 1; j >= 0; --j){
now = (long long)now * p % mod;
now = ((long long)now + s[j] - 'a') % mod;
}
unordered_map<int, deque<int>> pos;
for(int j = 0; j + i - 1 < n; ++j){
auto&nowp = pos[now];
while((int)nowp.size() >= 2 && nowp[1] + i <= j){
nowp.pop_front();
}
if((int)nowp.size() && nowp[0] + i <= j){
pre[j][i] = nowp[0];
}
nowp.push_back(j);
if(j + i < n){
now = ((long long)now + (long long)pi * (s[j + i] - 'a') % mod) % mod;
now = ((long long)now - s[j] + 'a' + mod) % mod;
now = (long long)now * invp % mod;
}
}
}
for(int j = 0; j < n; ++j){
long long mnval = (long long)1e18;
vector<long long> mnadd(n, (long long)1e18);
for(int i = j; i >= 0; --i){
if(i == j){
dp[i][j] = a;
}
else{
dp[i][j] = dp[i][j - 1] + a;
}
mnval = min(mnval, mnadd[i]);
dp[i][j] = min(dp[i][j], mnval - (long long)a * i);
long long cnt = 0;
for(int k = i; k != -1; k = pre[k][j - i + 1]){
++cnt;
mnadd[k] = min(mnadd[k], dp[i][j] + b + cnt * c + a * (j - k + 1 - cnt * (j - i + 1)) + (long long)k * a);
}
}
}
cout << dp[0][n - 1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
24788 KB |
Output is correct |
2 |
Correct |
10 ms |
24872 KB |
Output is correct |
3 |
Correct |
12 ms |
24768 KB |
Output is correct |
4 |
Correct |
10 ms |
24864 KB |
Output is correct |
5 |
Correct |
10 ms |
24844 KB |
Output is correct |
6 |
Correct |
12 ms |
24920 KB |
Output is correct |
7 |
Correct |
10 ms |
24788 KB |
Output is correct |
8 |
Correct |
10 ms |
24788 KB |
Output is correct |
9 |
Correct |
10 ms |
24872 KB |
Output is correct |
10 |
Correct |
10 ms |
24788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
24916 KB |
Output is correct |
2 |
Correct |
10 ms |
24940 KB |
Output is correct |
3 |
Correct |
304 ms |
45252 KB |
Output is correct |
4 |
Correct |
362 ms |
48148 KB |
Output is correct |
5 |
Correct |
451 ms |
51280 KB |
Output is correct |
6 |
Correct |
526 ms |
54560 KB |
Output is correct |
7 |
Correct |
625 ms |
58448 KB |
Output is correct |
8 |
Correct |
652 ms |
58268 KB |
Output is correct |
9 |
Correct |
635 ms |
58464 KB |
Output is correct |
10 |
Correct |
10 ms |
24788 KB |
Output is correct |
11 |
Correct |
9 ms |
24872 KB |
Output is correct |
12 |
Correct |
10 ms |
24864 KB |
Output is correct |
13 |
Correct |
10 ms |
24864 KB |
Output is correct |
14 |
Correct |
10 ms |
24880 KB |
Output is correct |
15 |
Correct |
10 ms |
24916 KB |
Output is correct |
16 |
Correct |
11 ms |
24916 KB |
Output is correct |
17 |
Correct |
11 ms |
24792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
24788 KB |
Output is correct |
2 |
Correct |
10 ms |
24872 KB |
Output is correct |
3 |
Correct |
12 ms |
24768 KB |
Output is correct |
4 |
Correct |
10 ms |
24864 KB |
Output is correct |
5 |
Correct |
10 ms |
24844 KB |
Output is correct |
6 |
Correct |
12 ms |
24920 KB |
Output is correct |
7 |
Correct |
10 ms |
24788 KB |
Output is correct |
8 |
Correct |
10 ms |
24788 KB |
Output is correct |
9 |
Correct |
10 ms |
24872 KB |
Output is correct |
10 |
Correct |
10 ms |
24788 KB |
Output is correct |
11 |
Correct |
12 ms |
24864 KB |
Output is correct |
12 |
Correct |
11 ms |
24832 KB |
Output is correct |
13 |
Correct |
9 ms |
24864 KB |
Output is correct |
14 |
Correct |
10 ms |
24856 KB |
Output is correct |
15 |
Correct |
10 ms |
24916 KB |
Output is correct |
16 |
Correct |
11 ms |
24916 KB |
Output is correct |
17 |
Correct |
10 ms |
24800 KB |
Output is correct |
18 |
Correct |
10 ms |
24868 KB |
Output is correct |
19 |
Correct |
10 ms |
24864 KB |
Output is correct |
20 |
Correct |
10 ms |
24848 KB |
Output is correct |
21 |
Correct |
10 ms |
24964 KB |
Output is correct |
22 |
Correct |
10 ms |
24916 KB |
Output is correct |
23 |
Correct |
10 ms |
24932 KB |
Output is correct |
24 |
Correct |
10 ms |
24992 KB |
Output is correct |
25 |
Correct |
10 ms |
24916 KB |
Output is correct |
26 |
Correct |
9 ms |
24964 KB |
Output is correct |
27 |
Correct |
10 ms |
24952 KB |
Output is correct |
28 |
Correct |
10 ms |
24996 KB |
Output is correct |
29 |
Correct |
10 ms |
24900 KB |
Output is correct |
30 |
Correct |
11 ms |
24916 KB |
Output is correct |
31 |
Correct |
10 ms |
24916 KB |
Output is correct |
32 |
Correct |
10 ms |
24896 KB |
Output is correct |
33 |
Correct |
9 ms |
24916 KB |
Output is correct |
34 |
Correct |
10 ms |
24852 KB |
Output is correct |
35 |
Correct |
9 ms |
24800 KB |
Output is correct |
36 |
Correct |
10 ms |
24788 KB |
Output is correct |
37 |
Correct |
11 ms |
25044 KB |
Output is correct |
38 |
Correct |
9 ms |
24916 KB |
Output is correct |
39 |
Correct |
9 ms |
24932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
24788 KB |
Output is correct |
2 |
Correct |
10 ms |
24872 KB |
Output is correct |
3 |
Correct |
12 ms |
24768 KB |
Output is correct |
4 |
Correct |
10 ms |
24864 KB |
Output is correct |
5 |
Correct |
10 ms |
24844 KB |
Output is correct |
6 |
Correct |
12 ms |
24920 KB |
Output is correct |
7 |
Correct |
10 ms |
24788 KB |
Output is correct |
8 |
Correct |
10 ms |
24788 KB |
Output is correct |
9 |
Correct |
10 ms |
24872 KB |
Output is correct |
10 |
Correct |
10 ms |
24788 KB |
Output is correct |
11 |
Correct |
12 ms |
24864 KB |
Output is correct |
12 |
Correct |
11 ms |
24832 KB |
Output is correct |
13 |
Correct |
9 ms |
24864 KB |
Output is correct |
14 |
Correct |
10 ms |
24856 KB |
Output is correct |
15 |
Correct |
10 ms |
24916 KB |
Output is correct |
16 |
Correct |
11 ms |
24916 KB |
Output is correct |
17 |
Correct |
10 ms |
24800 KB |
Output is correct |
18 |
Correct |
10 ms |
24868 KB |
Output is correct |
19 |
Correct |
10 ms |
24864 KB |
Output is correct |
20 |
Correct |
10 ms |
24848 KB |
Output is correct |
21 |
Correct |
10 ms |
24964 KB |
Output is correct |
22 |
Correct |
10 ms |
24916 KB |
Output is correct |
23 |
Correct |
10 ms |
24932 KB |
Output is correct |
24 |
Correct |
10 ms |
24992 KB |
Output is correct |
25 |
Correct |
10 ms |
24916 KB |
Output is correct |
26 |
Correct |
9 ms |
24964 KB |
Output is correct |
27 |
Correct |
10 ms |
24952 KB |
Output is correct |
28 |
Correct |
10 ms |
24996 KB |
Output is correct |
29 |
Correct |
10 ms |
24900 KB |
Output is correct |
30 |
Correct |
11 ms |
24916 KB |
Output is correct |
31 |
Correct |
10 ms |
24916 KB |
Output is correct |
32 |
Correct |
10 ms |
24896 KB |
Output is correct |
33 |
Correct |
9 ms |
24916 KB |
Output is correct |
34 |
Correct |
10 ms |
24852 KB |
Output is correct |
35 |
Correct |
9 ms |
24800 KB |
Output is correct |
36 |
Correct |
10 ms |
24788 KB |
Output is correct |
37 |
Correct |
11 ms |
25044 KB |
Output is correct |
38 |
Correct |
9 ms |
24916 KB |
Output is correct |
39 |
Correct |
9 ms |
24932 KB |
Output is correct |
40 |
Correct |
10 ms |
25216 KB |
Output is correct |
41 |
Correct |
11 ms |
25812 KB |
Output is correct |
42 |
Correct |
14 ms |
25916 KB |
Output is correct |
43 |
Correct |
14 ms |
25940 KB |
Output is correct |
44 |
Correct |
15 ms |
25940 KB |
Output is correct |
45 |
Correct |
16 ms |
25940 KB |
Output is correct |
46 |
Correct |
14 ms |
25940 KB |
Output is correct |
47 |
Correct |
15 ms |
25840 KB |
Output is correct |
48 |
Correct |
13 ms |
25760 KB |
Output is correct |
49 |
Correct |
13 ms |
25812 KB |
Output is correct |
50 |
Correct |
13 ms |
25812 KB |
Output is correct |
51 |
Correct |
13 ms |
25764 KB |
Output is correct |
52 |
Correct |
13 ms |
25864 KB |
Output is correct |
53 |
Correct |
15 ms |
25940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
24788 KB |
Output is correct |
2 |
Correct |
10 ms |
24872 KB |
Output is correct |
3 |
Correct |
12 ms |
24768 KB |
Output is correct |
4 |
Correct |
10 ms |
24864 KB |
Output is correct |
5 |
Correct |
10 ms |
24844 KB |
Output is correct |
6 |
Correct |
12 ms |
24920 KB |
Output is correct |
7 |
Correct |
10 ms |
24788 KB |
Output is correct |
8 |
Correct |
10 ms |
24788 KB |
Output is correct |
9 |
Correct |
10 ms |
24872 KB |
Output is correct |
10 |
Correct |
10 ms |
24788 KB |
Output is correct |
11 |
Correct |
12 ms |
24864 KB |
Output is correct |
12 |
Correct |
11 ms |
24832 KB |
Output is correct |
13 |
Correct |
9 ms |
24864 KB |
Output is correct |
14 |
Correct |
10 ms |
24856 KB |
Output is correct |
15 |
Correct |
10 ms |
24916 KB |
Output is correct |
16 |
Correct |
11 ms |
24916 KB |
Output is correct |
17 |
Correct |
10 ms |
24800 KB |
Output is correct |
18 |
Correct |
10 ms |
24868 KB |
Output is correct |
19 |
Correct |
10 ms |
24864 KB |
Output is correct |
20 |
Correct |
10 ms |
24848 KB |
Output is correct |
21 |
Correct |
10 ms |
24964 KB |
Output is correct |
22 |
Correct |
10 ms |
24916 KB |
Output is correct |
23 |
Correct |
10 ms |
24932 KB |
Output is correct |
24 |
Correct |
10 ms |
24992 KB |
Output is correct |
25 |
Correct |
10 ms |
24916 KB |
Output is correct |
26 |
Correct |
9 ms |
24964 KB |
Output is correct |
27 |
Correct |
10 ms |
24952 KB |
Output is correct |
28 |
Correct |
10 ms |
24996 KB |
Output is correct |
29 |
Correct |
10 ms |
24900 KB |
Output is correct |
30 |
Correct |
11 ms |
24916 KB |
Output is correct |
31 |
Correct |
10 ms |
24916 KB |
Output is correct |
32 |
Correct |
10 ms |
24896 KB |
Output is correct |
33 |
Correct |
9 ms |
24916 KB |
Output is correct |
34 |
Correct |
10 ms |
24852 KB |
Output is correct |
35 |
Correct |
9 ms |
24800 KB |
Output is correct |
36 |
Correct |
10 ms |
24788 KB |
Output is correct |
37 |
Correct |
11 ms |
25044 KB |
Output is correct |
38 |
Correct |
9 ms |
24916 KB |
Output is correct |
39 |
Correct |
9 ms |
24932 KB |
Output is correct |
40 |
Correct |
10 ms |
25216 KB |
Output is correct |
41 |
Correct |
11 ms |
25812 KB |
Output is correct |
42 |
Correct |
14 ms |
25916 KB |
Output is correct |
43 |
Correct |
14 ms |
25940 KB |
Output is correct |
44 |
Correct |
15 ms |
25940 KB |
Output is correct |
45 |
Correct |
16 ms |
25940 KB |
Output is correct |
46 |
Correct |
14 ms |
25940 KB |
Output is correct |
47 |
Correct |
15 ms |
25840 KB |
Output is correct |
48 |
Correct |
13 ms |
25760 KB |
Output is correct |
49 |
Correct |
13 ms |
25812 KB |
Output is correct |
50 |
Correct |
13 ms |
25812 KB |
Output is correct |
51 |
Correct |
13 ms |
25764 KB |
Output is correct |
52 |
Correct |
13 ms |
25864 KB |
Output is correct |
53 |
Correct |
15 ms |
25940 KB |
Output is correct |
54 |
Correct |
35 ms |
27708 KB |
Output is correct |
55 |
Correct |
84 ms |
32800 KB |
Output is correct |
56 |
Correct |
143 ms |
33484 KB |
Output is correct |
57 |
Correct |
137 ms |
33448 KB |
Output is correct |
58 |
Correct |
139 ms |
33440 KB |
Output is correct |
59 |
Correct |
149 ms |
33380 KB |
Output is correct |
60 |
Correct |
136 ms |
33392 KB |
Output is correct |
61 |
Correct |
89 ms |
32844 KB |
Output is correct |
62 |
Correct |
77 ms |
32716 KB |
Output is correct |
63 |
Correct |
124 ms |
33012 KB |
Output is correct |
64 |
Correct |
127 ms |
33056 KB |
Output is correct |
65 |
Correct |
115 ms |
32960 KB |
Output is correct |
66 |
Correct |
109 ms |
32932 KB |
Output is correct |
67 |
Correct |
139 ms |
33484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
24788 KB |
Output is correct |
2 |
Correct |
10 ms |
24872 KB |
Output is correct |
3 |
Correct |
12 ms |
24768 KB |
Output is correct |
4 |
Correct |
10 ms |
24864 KB |
Output is correct |
5 |
Correct |
10 ms |
24844 KB |
Output is correct |
6 |
Correct |
12 ms |
24920 KB |
Output is correct |
7 |
Correct |
10 ms |
24788 KB |
Output is correct |
8 |
Correct |
10 ms |
24788 KB |
Output is correct |
9 |
Correct |
10 ms |
24872 KB |
Output is correct |
10 |
Correct |
10 ms |
24788 KB |
Output is correct |
11 |
Correct |
11 ms |
24916 KB |
Output is correct |
12 |
Correct |
10 ms |
24940 KB |
Output is correct |
13 |
Correct |
304 ms |
45252 KB |
Output is correct |
14 |
Correct |
362 ms |
48148 KB |
Output is correct |
15 |
Correct |
451 ms |
51280 KB |
Output is correct |
16 |
Correct |
526 ms |
54560 KB |
Output is correct |
17 |
Correct |
625 ms |
58448 KB |
Output is correct |
18 |
Correct |
652 ms |
58268 KB |
Output is correct |
19 |
Correct |
635 ms |
58464 KB |
Output is correct |
20 |
Correct |
10 ms |
24788 KB |
Output is correct |
21 |
Correct |
9 ms |
24872 KB |
Output is correct |
22 |
Correct |
10 ms |
24864 KB |
Output is correct |
23 |
Correct |
10 ms |
24864 KB |
Output is correct |
24 |
Correct |
10 ms |
24880 KB |
Output is correct |
25 |
Correct |
10 ms |
24916 KB |
Output is correct |
26 |
Correct |
11 ms |
24916 KB |
Output is correct |
27 |
Correct |
11 ms |
24792 KB |
Output is correct |
28 |
Correct |
12 ms |
24864 KB |
Output is correct |
29 |
Correct |
11 ms |
24832 KB |
Output is correct |
30 |
Correct |
9 ms |
24864 KB |
Output is correct |
31 |
Correct |
10 ms |
24856 KB |
Output is correct |
32 |
Correct |
10 ms |
24916 KB |
Output is correct |
33 |
Correct |
11 ms |
24916 KB |
Output is correct |
34 |
Correct |
10 ms |
24800 KB |
Output is correct |
35 |
Correct |
10 ms |
24868 KB |
Output is correct |
36 |
Correct |
10 ms |
24864 KB |
Output is correct |
37 |
Correct |
10 ms |
24848 KB |
Output is correct |
38 |
Correct |
10 ms |
24964 KB |
Output is correct |
39 |
Correct |
10 ms |
24916 KB |
Output is correct |
40 |
Correct |
10 ms |
24932 KB |
Output is correct |
41 |
Correct |
10 ms |
24992 KB |
Output is correct |
42 |
Correct |
10 ms |
24916 KB |
Output is correct |
43 |
Correct |
9 ms |
24964 KB |
Output is correct |
44 |
Correct |
10 ms |
24952 KB |
Output is correct |
45 |
Correct |
10 ms |
24996 KB |
Output is correct |
46 |
Correct |
10 ms |
24900 KB |
Output is correct |
47 |
Correct |
11 ms |
24916 KB |
Output is correct |
48 |
Correct |
10 ms |
24916 KB |
Output is correct |
49 |
Correct |
10 ms |
24896 KB |
Output is correct |
50 |
Correct |
9 ms |
24916 KB |
Output is correct |
51 |
Correct |
10 ms |
24852 KB |
Output is correct |
52 |
Correct |
9 ms |
24800 KB |
Output is correct |
53 |
Correct |
10 ms |
24788 KB |
Output is correct |
54 |
Correct |
11 ms |
25044 KB |
Output is correct |
55 |
Correct |
9 ms |
24916 KB |
Output is correct |
56 |
Correct |
9 ms |
24932 KB |
Output is correct |
57 |
Correct |
10 ms |
25216 KB |
Output is correct |
58 |
Correct |
11 ms |
25812 KB |
Output is correct |
59 |
Correct |
14 ms |
25916 KB |
Output is correct |
60 |
Correct |
14 ms |
25940 KB |
Output is correct |
61 |
Correct |
15 ms |
25940 KB |
Output is correct |
62 |
Correct |
16 ms |
25940 KB |
Output is correct |
63 |
Correct |
14 ms |
25940 KB |
Output is correct |
64 |
Correct |
15 ms |
25840 KB |
Output is correct |
65 |
Correct |
13 ms |
25760 KB |
Output is correct |
66 |
Correct |
13 ms |
25812 KB |
Output is correct |
67 |
Correct |
13 ms |
25812 KB |
Output is correct |
68 |
Correct |
13 ms |
25764 KB |
Output is correct |
69 |
Correct |
13 ms |
25864 KB |
Output is correct |
70 |
Correct |
15 ms |
25940 KB |
Output is correct |
71 |
Correct |
35 ms |
27708 KB |
Output is correct |
72 |
Correct |
84 ms |
32800 KB |
Output is correct |
73 |
Correct |
143 ms |
33484 KB |
Output is correct |
74 |
Correct |
137 ms |
33448 KB |
Output is correct |
75 |
Correct |
139 ms |
33440 KB |
Output is correct |
76 |
Correct |
149 ms |
33380 KB |
Output is correct |
77 |
Correct |
136 ms |
33392 KB |
Output is correct |
78 |
Correct |
89 ms |
32844 KB |
Output is correct |
79 |
Correct |
77 ms |
32716 KB |
Output is correct |
80 |
Correct |
124 ms |
33012 KB |
Output is correct |
81 |
Correct |
127 ms |
33056 KB |
Output is correct |
82 |
Correct |
115 ms |
32960 KB |
Output is correct |
83 |
Correct |
109 ms |
32932 KB |
Output is correct |
84 |
Correct |
139 ms |
33484 KB |
Output is correct |
85 |
Correct |
360 ms |
42056 KB |
Output is correct |
86 |
Correct |
907 ms |
60076 KB |
Output is correct |
87 |
Correct |
881 ms |
60080 KB |
Output is correct |
88 |
Correct |
849 ms |
60048 KB |
Output is correct |
89 |
Correct |
870 ms |
59792 KB |
Output is correct |
90 |
Correct |
882 ms |
60072 KB |
Output is correct |
91 |
Correct |
473 ms |
58484 KB |
Output is correct |
92 |
Correct |
556 ms |
58296 KB |
Output is correct |
93 |
Correct |
726 ms |
59012 KB |
Output is correct |
94 |
Correct |
773 ms |
58844 KB |
Output is correct |
95 |
Correct |
778 ms |
58560 KB |
Output is correct |
96 |
Correct |
777 ms |
58604 KB |
Output is correct |
97 |
Incorrect |
861 ms |
60172 KB |
Output isn't correct |
98 |
Halted |
0 ms |
0 KB |
- |