/* Im the best and i work like that */
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;
#define int ll
const int N = 2510;
const int LOG = 20;
const ll mod = 1e9 + 21;
const ll base = 124338541;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;
ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }
mt19937 rng(time(nullptr));
int n, par[N];
ll A, B, C, Hsh[N], P[N], dp[N][N]; /// dp -> how much we can save
string s;
inline ll get(ll l, ll r)
{
return (Hsh[r] - (Hsh[l - 1] * P[r - l + 1] % mod) + mod) % mod;
}
int32_t main()
{
fast_io;
for(int i = P[0] = 1; i < N; i ++) { P[i] = P[i - 1] * base % mod; }
cin >> n >> s >> A >> B >> C;
s = "." + s;
for(int i = 1; i <= n; i ++) { Hsh[i] = (Hsh[i - 1] * base + s[i]) % mod; }
for(int len = 1; len <= n; len ++)
{
memset(par, -1, sizeof par);
vector < pll > vec;
for(int i = len; i <= n; i ++)
{
vec.push_back(Mp(get(i - len + 1, i), i));
}
for(int i = 1; i <= n; i ++)
{
dp[i][i + len - 1] = max({dp[i][i + len - 1], dp[i + 1][i + len - 1], dp[i][i + len - 2]});
}
sort(all(vec));
for(int i = len; i <= n; i ++)
{
ll cu = get(i - len + 1, i);
int id = lower_bound(all(vec), Mp(cu, 1ll * i + len)) - vec.begin();
if(id < SZ(vec) && vec[id].F == cu)
{
par[i] = vec[id].S;
}
}
for(int i = len; i <= n; i ++)
{
int nxt = par[i], cnt = 1;
ll cu = dp[i - len + 1][i];
while(nxt != -1)
{
cnt ++;
dp[i - len + 1][nxt] = max(dp[i - len + 1][nxt], cu + 1ll * len * (cnt - 1) * A - B - 1ll * cnt * C);
nxt = par[nxt];
}
}
}
cout << 1ll * n * A - dp[1][n];
return 0;
}
/* check corner case(n = 1?), watch for negetive index or overflow */
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
200 ms |
33804 KB |
Output is correct |
4 |
Correct |
246 ms |
38936 KB |
Output is correct |
5 |
Correct |
287 ms |
42452 KB |
Output is correct |
6 |
Correct |
334 ms |
45876 KB |
Output is correct |
7 |
Correct |
404 ms |
49620 KB |
Output is correct |
8 |
Correct |
404 ms |
49544 KB |
Output is correct |
9 |
Correct |
400 ms |
49632 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
416 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
416 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
1492 KB |
Output is correct |
42 |
Correct |
3 ms |
1384 KB |
Output is correct |
43 |
Correct |
3 ms |
1492 KB |
Output is correct |
44 |
Correct |
3 ms |
1492 KB |
Output is correct |
45 |
Correct |
3 ms |
1492 KB |
Output is correct |
46 |
Correct |
3 ms |
1492 KB |
Output is correct |
47 |
Correct |
4 ms |
1492 KB |
Output is correct |
48 |
Correct |
4 ms |
1492 KB |
Output is correct |
49 |
Correct |
4 ms |
1492 KB |
Output is correct |
50 |
Correct |
4 ms |
1492 KB |
Output is correct |
51 |
Correct |
4 ms |
1492 KB |
Output is correct |
52 |
Correct |
4 ms |
1492 KB |
Output is correct |
53 |
Correct |
3 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
0 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
416 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
1492 KB |
Output is correct |
42 |
Correct |
3 ms |
1384 KB |
Output is correct |
43 |
Correct |
3 ms |
1492 KB |
Output is correct |
44 |
Correct |
3 ms |
1492 KB |
Output is correct |
45 |
Correct |
3 ms |
1492 KB |
Output is correct |
46 |
Correct |
3 ms |
1492 KB |
Output is correct |
47 |
Correct |
4 ms |
1492 KB |
Output is correct |
48 |
Correct |
4 ms |
1492 KB |
Output is correct |
49 |
Correct |
4 ms |
1492 KB |
Output is correct |
50 |
Correct |
4 ms |
1492 KB |
Output is correct |
51 |
Correct |
4 ms |
1492 KB |
Output is correct |
52 |
Correct |
4 ms |
1492 KB |
Output is correct |
53 |
Correct |
3 ms |
1492 KB |
Output is correct |
54 |
Correct |
14 ms |
3720 KB |
Output is correct |
55 |
Correct |
45 ms |
12236 KB |
Output is correct |
56 |
Correct |
75 ms |
12156 KB |
Output is correct |
57 |
Correct |
71 ms |
12200 KB |
Output is correct |
58 |
Correct |
71 ms |
12236 KB |
Output is correct |
59 |
Correct |
77 ms |
12236 KB |
Output is correct |
60 |
Correct |
71 ms |
12136 KB |
Output is correct |
61 |
Correct |
79 ms |
12188 KB |
Output is correct |
62 |
Correct |
58 ms |
12168 KB |
Output is correct |
63 |
Correct |
77 ms |
12236 KB |
Output is correct |
64 |
Correct |
75 ms |
12232 KB |
Output is correct |
65 |
Correct |
86 ms |
12244 KB |
Output is correct |
66 |
Correct |
80 ms |
12168 KB |
Output is correct |
67 |
Correct |
71 ms |
12240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
200 ms |
33804 KB |
Output is correct |
14 |
Correct |
246 ms |
38936 KB |
Output is correct |
15 |
Correct |
287 ms |
42452 KB |
Output is correct |
16 |
Correct |
334 ms |
45876 KB |
Output is correct |
17 |
Correct |
404 ms |
49620 KB |
Output is correct |
18 |
Correct |
404 ms |
49544 KB |
Output is correct |
19 |
Correct |
400 ms |
49632 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
0 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
416 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
724 KB |
Output is correct |
58 |
Correct |
2 ms |
1492 KB |
Output is correct |
59 |
Correct |
3 ms |
1384 KB |
Output is correct |
60 |
Correct |
3 ms |
1492 KB |
Output is correct |
61 |
Correct |
3 ms |
1492 KB |
Output is correct |
62 |
Correct |
3 ms |
1492 KB |
Output is correct |
63 |
Correct |
3 ms |
1492 KB |
Output is correct |
64 |
Correct |
4 ms |
1492 KB |
Output is correct |
65 |
Correct |
4 ms |
1492 KB |
Output is correct |
66 |
Correct |
4 ms |
1492 KB |
Output is correct |
67 |
Correct |
4 ms |
1492 KB |
Output is correct |
68 |
Correct |
4 ms |
1492 KB |
Output is correct |
69 |
Correct |
4 ms |
1492 KB |
Output is correct |
70 |
Correct |
3 ms |
1492 KB |
Output is correct |
71 |
Correct |
14 ms |
3720 KB |
Output is correct |
72 |
Correct |
45 ms |
12236 KB |
Output is correct |
73 |
Correct |
75 ms |
12156 KB |
Output is correct |
74 |
Correct |
71 ms |
12200 KB |
Output is correct |
75 |
Correct |
71 ms |
12236 KB |
Output is correct |
76 |
Correct |
77 ms |
12236 KB |
Output is correct |
77 |
Correct |
71 ms |
12136 KB |
Output is correct |
78 |
Correct |
79 ms |
12188 KB |
Output is correct |
79 |
Correct |
58 ms |
12168 KB |
Output is correct |
80 |
Correct |
77 ms |
12236 KB |
Output is correct |
81 |
Correct |
75 ms |
12232 KB |
Output is correct |
82 |
Correct |
86 ms |
12244 KB |
Output is correct |
83 |
Correct |
80 ms |
12168 KB |
Output is correct |
84 |
Correct |
71 ms |
12240 KB |
Output is correct |
85 |
Correct |
194 ms |
26336 KB |
Output is correct |
86 |
Correct |
531 ms |
49648 KB |
Output is correct |
87 |
Correct |
524 ms |
49516 KB |
Output is correct |
88 |
Correct |
524 ms |
49592 KB |
Output is correct |
89 |
Incorrect |
523 ms |
49876 KB |
Output isn't correct |
90 |
Halted |
0 ms |
0 KB |
- |