#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;
ll base, mod;
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 nxt[mxn + 1];
ll gethsh(int l, int r){
return((hsh[r] - hsh[l - 1] * pw[r - l + 1] + mod * mod) % mod);
}
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; mod = rr(1e8, 1e9); base = rr(100, 10000);
pw[0] = 1;
for(int i = 1; i <= n; i++){
pw[i] = (pw[i - 1] * base) % mod;
}
for(int i = 1; i <= n; i++){
hsh[i] = (hsh[i - 1] * base + (s[i - 1] - 'a' + 1)) % mod;
}
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++){
vt<pii>comp;
for(int j = 1; j + len - 1 <= n; j++){
nxt[j] = -1;
comp.pb(make_pair(gethsh(j, j + len - 1), j));
}
sort(comp.begin(), comp.end());
for(int i = 0; i < sz(comp);){
int r = i;
while(r < sz(comp) && comp[r].first == comp[i].first)r++;
int rp = i;
for(int j = i; j < r; j++){
while(rp < r && comp[rp].second < comp[j].second + len){
rp++;
}
if(rp == r)nxt[comp[j].second] = -1;
else nxt[comp[j].second] = comp[rp].second;
}
i = r;
}
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] != -1){
idl = nxt[idl];
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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
112 ms |
37560 KB |
Output is correct |
4 |
Correct |
141 ms |
39528 KB |
Output is correct |
5 |
Correct |
172 ms |
43740 KB |
Output is correct |
6 |
Correct |
209 ms |
47848 KB |
Output is correct |
7 |
Correct |
255 ms |
48236 KB |
Output is correct |
8 |
Correct |
245 ms |
48244 KB |
Output is correct |
9 |
Correct |
264 ms |
48244 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2392 KB |
Output is correct |
15 |
Correct |
0 ms |
2392 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2392 KB |
Output is correct |
15 |
Correct |
0 ms |
2392 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
1 ms |
2392 KB |
Output is correct |
40 |
Correct |
1 ms |
2648 KB |
Output is correct |
41 |
Correct |
1 ms |
4440 KB |
Output is correct |
42 |
Correct |
2 ms |
4440 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
2 ms |
4440 KB |
Output is correct |
45 |
Correct |
2 ms |
4440 KB |
Output is correct |
46 |
Correct |
2 ms |
4440 KB |
Output is correct |
47 |
Correct |
2 ms |
4440 KB |
Output is correct |
48 |
Correct |
2 ms |
4440 KB |
Output is correct |
49 |
Correct |
2 ms |
4440 KB |
Output is correct |
50 |
Correct |
2 ms |
4444 KB |
Output is correct |
51 |
Correct |
2 ms |
4440 KB |
Output is correct |
52 |
Correct |
2 ms |
4444 KB |
Output is correct |
53 |
Correct |
2 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2392 KB |
Output is correct |
15 |
Correct |
0 ms |
2392 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
1 ms |
2392 KB |
Output is correct |
40 |
Correct |
1 ms |
2648 KB |
Output is correct |
41 |
Correct |
1 ms |
4440 KB |
Output is correct |
42 |
Correct |
2 ms |
4440 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
2 ms |
4440 KB |
Output is correct |
45 |
Correct |
2 ms |
4440 KB |
Output is correct |
46 |
Correct |
2 ms |
4440 KB |
Output is correct |
47 |
Correct |
2 ms |
4440 KB |
Output is correct |
48 |
Correct |
2 ms |
4440 KB |
Output is correct |
49 |
Correct |
2 ms |
4440 KB |
Output is correct |
50 |
Correct |
2 ms |
4444 KB |
Output is correct |
51 |
Correct |
2 ms |
4440 KB |
Output is correct |
52 |
Correct |
2 ms |
4444 KB |
Output is correct |
53 |
Correct |
2 ms |
4440 KB |
Output is correct |
54 |
Correct |
7 ms |
10588 KB |
Output is correct |
55 |
Correct |
24 ms |
21080 KB |
Output is correct |
56 |
Correct |
36 ms |
21084 KB |
Output is correct |
57 |
Correct |
32 ms |
21084 KB |
Output is correct |
58 |
Incorrect |
30 ms |
21080 KB |
Output isn't correct |
59 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
112 ms |
37560 KB |
Output is correct |
14 |
Correct |
141 ms |
39528 KB |
Output is correct |
15 |
Correct |
172 ms |
43740 KB |
Output is correct |
16 |
Correct |
209 ms |
47848 KB |
Output is correct |
17 |
Correct |
255 ms |
48236 KB |
Output is correct |
18 |
Correct |
245 ms |
48244 KB |
Output is correct |
19 |
Correct |
264 ms |
48244 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
2392 KB |
Output is correct |
29 |
Correct |
0 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2392 KB |
Output is correct |
31 |
Correct |
0 ms |
2392 KB |
Output is correct |
32 |
Correct |
0 ms |
2392 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
2392 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
0 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2392 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2392 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2392 KB |
Output is correct |
45 |
Correct |
1 ms |
2392 KB |
Output is correct |
46 |
Correct |
1 ms |
2392 KB |
Output is correct |
47 |
Correct |
1 ms |
2392 KB |
Output is correct |
48 |
Correct |
1 ms |
2392 KB |
Output is correct |
49 |
Correct |
1 ms |
2392 KB |
Output is correct |
50 |
Correct |
1 ms |
2392 KB |
Output is correct |
51 |
Correct |
0 ms |
344 KB |
Output is correct |
52 |
Correct |
1 ms |
344 KB |
Output is correct |
53 |
Correct |
0 ms |
344 KB |
Output is correct |
54 |
Correct |
0 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2392 KB |
Output is correct |
56 |
Correct |
1 ms |
2392 KB |
Output is correct |
57 |
Correct |
1 ms |
2648 KB |
Output is correct |
58 |
Correct |
1 ms |
4440 KB |
Output is correct |
59 |
Correct |
2 ms |
4440 KB |
Output is correct |
60 |
Correct |
2 ms |
4444 KB |
Output is correct |
61 |
Correct |
2 ms |
4440 KB |
Output is correct |
62 |
Correct |
2 ms |
4440 KB |
Output is correct |
63 |
Correct |
2 ms |
4440 KB |
Output is correct |
64 |
Correct |
2 ms |
4440 KB |
Output is correct |
65 |
Correct |
2 ms |
4440 KB |
Output is correct |
66 |
Correct |
2 ms |
4440 KB |
Output is correct |
67 |
Correct |
2 ms |
4444 KB |
Output is correct |
68 |
Correct |
2 ms |
4440 KB |
Output is correct |
69 |
Correct |
2 ms |
4444 KB |
Output is correct |
70 |
Correct |
2 ms |
4440 KB |
Output is correct |
71 |
Correct |
7 ms |
10588 KB |
Output is correct |
72 |
Correct |
24 ms |
21080 KB |
Output is correct |
73 |
Correct |
36 ms |
21084 KB |
Output is correct |
74 |
Correct |
32 ms |
21084 KB |
Output is correct |
75 |
Incorrect |
30 ms |
21080 KB |
Output isn't correct |
76 |
Halted |
0 ms |
0 KB |
- |