#include<bits/stdc++.h>
#include<fstream>
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
//ifstream fin("FEEDING.INP");
//ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int mxn = 2505;
const ll inf = 1e18, mod = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rr(ll l, ll r){
return(uniform_int_distribution<ll>(l, r)(rng));
}
ll n, a, b, c;
string s;
ll dp[mxn + 1][mxn + 1], hsh[mxn + 1], pw[mxn + 1];
int z[2505], nxt[2505][2505];
void ckmin(ll &a, ll b){
a = min(a, b);
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s >> a >> b >> c;
for(int i = 0; i < n; i++){
int l = i, r = i;
for(int j = i + 1; j < n; j++){
if(j > r){
l = r = j;
while(r < n && s[r] == s[r - l + i])r++;
z[j] = r - l; r--;
}else{
int pos = j - l + i;
if(z[pos] < r - j + 1)z[j] = z[pos];
else{
l = j;
while(r < n && s[r] == s[r - l + i])r++;
z[j] = r - l; r--;
}
}
}
int rp = i + 1;
for(int j = 1; j <= n; j++){
rp = max(rp, i + j);
while(rp < n && z[rp] < j){
rp++;
}
if(rp < n)nxt[i + 1][j] = rp + 1;
else nxt[i + 1][j] = -1;
}
}
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
dp[i][j] = inf;
}
}
for(int len = 1; len <= n; len++){
for(int i = 1; i + len - 1 <= n; i++){
int l = i, r = i + len - 1;
ckmin(dp[l][r], dp[l + 1][r] + a); ckmin(dp[l][r], dp[l][r - 1] + a);
int idl = l, cnt = 1;
while(nxt[idl][len] != -1){
idl = nxt[idl][len];
cnt++;
int nwr = idl + len - 1;
ckmin(dp[l][nwr], dp[l][r] + b + c * cnt + a * (nwr - l + 1 - len * cnt));
}
}
}
cout << dp[1][n];
return(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
119 ms |
58008 KB |
Output is correct |
4 |
Correct |
152 ms |
62172 KB |
Output is correct |
5 |
Correct |
214 ms |
68336 KB |
Output is correct |
6 |
Correct |
212 ms |
72280 KB |
Output is correct |
7 |
Correct |
261 ms |
72812 KB |
Output is correct |
8 |
Correct |
268 ms |
72812 KB |
Output is correct |
9 |
Correct |
256 ms |
72788 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 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 |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2468 KB |
Output is correct |
36 |
Correct |
0 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 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 |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2468 KB |
Output is correct |
36 |
Correct |
0 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6568 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 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 |
8540 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 |
2 ms |
8536 KB |
Output is correct |
49 |
Correct |
1 ms |
8540 KB |
Output is correct |
50 |
Correct |
2 ms |
8748 KB |
Output is correct |
51 |
Correct |
1 ms |
8540 KB |
Output is correct |
52 |
Correct |
1 ms |
8540 KB |
Output is correct |
53 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 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 |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2468 KB |
Output is correct |
36 |
Correct |
0 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
6568 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 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 |
8540 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 |
2 ms |
8536 KB |
Output is correct |
49 |
Correct |
1 ms |
8540 KB |
Output is correct |
50 |
Correct |
2 ms |
8748 KB |
Output is correct |
51 |
Correct |
1 ms |
8540 KB |
Output is correct |
52 |
Correct |
1 ms |
8540 KB |
Output is correct |
53 |
Correct |
1 ms |
8540 KB |
Output is correct |
54 |
Correct |
4 ms |
18780 KB |
Output is correct |
55 |
Correct |
25 ms |
33432 KB |
Output is correct |
56 |
Correct |
14 ms |
33372 KB |
Output is correct |
57 |
Correct |
12 ms |
33372 KB |
Output is correct |
58 |
Correct |
10 ms |
33432 KB |
Output is correct |
59 |
Correct |
9 ms |
33380 KB |
Output is correct |
60 |
Correct |
9 ms |
33372 KB |
Output is correct |
61 |
Correct |
10 ms |
33372 KB |
Output is correct |
62 |
Correct |
21 ms |
33432 KB |
Output is correct |
63 |
Correct |
10 ms |
33372 KB |
Output is correct |
64 |
Correct |
11 ms |
33372 KB |
Output is correct |
65 |
Correct |
16 ms |
33396 KB |
Output is correct |
66 |
Correct |
15 ms |
33624 KB |
Output is correct |
67 |
Correct |
9 ms |
33372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
119 ms |
58008 KB |
Output is correct |
14 |
Correct |
152 ms |
62172 KB |
Output is correct |
15 |
Correct |
214 ms |
68336 KB |
Output is correct |
16 |
Correct |
212 ms |
72280 KB |
Output is correct |
17 |
Correct |
261 ms |
72812 KB |
Output is correct |
18 |
Correct |
268 ms |
72812 KB |
Output is correct |
19 |
Correct |
256 ms |
72788 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
0 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6492 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
1 ms |
6492 KB |
Output is correct |
48 |
Correct |
1 ms |
6488 KB |
Output is correct |
49 |
Correct |
1 ms |
6492 KB |
Output is correct |
50 |
Correct |
1 ms |
6492 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
0 ms |
2468 KB |
Output is correct |
53 |
Correct |
0 ms |
2392 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
4444 KB |
Output is correct |
56 |
Correct |
1 ms |
6568 KB |
Output is correct |
57 |
Correct |
1 ms |
6492 KB |
Output is correct |
58 |
Correct |
2 ms |
8540 KB |
Output is correct |
59 |
Correct |
1 ms |
8540 KB |
Output is correct |
60 |
Correct |
1 ms |
8540 KB |
Output is correct |
61 |
Correct |
1 ms |
8540 KB |
Output is correct |
62 |
Correct |
1 ms |
8540 KB |
Output is correct |
63 |
Correct |
1 ms |
8540 KB |
Output is correct |
64 |
Correct |
1 ms |
8540 KB |
Output is correct |
65 |
Correct |
2 ms |
8536 KB |
Output is correct |
66 |
Correct |
1 ms |
8540 KB |
Output is correct |
67 |
Correct |
2 ms |
8748 KB |
Output is correct |
68 |
Correct |
1 ms |
8540 KB |
Output is correct |
69 |
Correct |
1 ms |
8540 KB |
Output is correct |
70 |
Correct |
1 ms |
8540 KB |
Output is correct |
71 |
Correct |
4 ms |
18780 KB |
Output is correct |
72 |
Correct |
25 ms |
33432 KB |
Output is correct |
73 |
Correct |
14 ms |
33372 KB |
Output is correct |
74 |
Correct |
12 ms |
33372 KB |
Output is correct |
75 |
Correct |
10 ms |
33432 KB |
Output is correct |
76 |
Correct |
9 ms |
33380 KB |
Output is correct |
77 |
Correct |
9 ms |
33372 KB |
Output is correct |
78 |
Correct |
10 ms |
33372 KB |
Output is correct |
79 |
Correct |
21 ms |
33432 KB |
Output is correct |
80 |
Correct |
10 ms |
33372 KB |
Output is correct |
81 |
Correct |
11 ms |
33372 KB |
Output is correct |
82 |
Correct |
16 ms |
33396 KB |
Output is correct |
83 |
Correct |
15 ms |
33624 KB |
Output is correct |
84 |
Correct |
9 ms |
33372 KB |
Output is correct |
85 |
Correct |
31 ms |
51804 KB |
Output is correct |
86 |
Correct |
79 ms |
72820 KB |
Output is correct |
87 |
Correct |
65 ms |
72788 KB |
Output is correct |
88 |
Correct |
50 ms |
72796 KB |
Output is correct |
89 |
Correct |
44 ms |
72784 KB |
Output is correct |
90 |
Correct |
41 ms |
72792 KB |
Output is correct |
91 |
Correct |
122 ms |
72816 KB |
Output is correct |
92 |
Correct |
171 ms |
72812 KB |
Output is correct |
93 |
Correct |
52 ms |
72784 KB |
Output is correct |
94 |
Correct |
63 ms |
72796 KB |
Output is correct |
95 |
Correct |
154 ms |
72820 KB |
Output is correct |
96 |
Correct |
152 ms |
72808 KB |
Output is correct |
97 |
Correct |
41 ms |
72792 KB |
Output is correct |