/* 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 inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;
const ll mod = 16227933338363;
const ll base = 1e3 + 9;
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][N], P[N], dp[N][N]; /// dp -> how much we can save
string s;
inline ll get(int l, int r)
{
return Hsh[l][r];
}
int32_t main()
{
fast_io;
cin >> n >> s >> A >> B >> C;
s = "." + s;
for(int i = 1; i <= n; i ++)
{
for(int j = i; j <= n; j ++)
{
Hsh[i][j] = (Hsh[i][j - 1] * base + s[j]) % 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 |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
250 ms |
54212 KB |
Output is correct |
4 |
Correct |
280 ms |
62024 KB |
Output is correct |
5 |
Correct |
323 ms |
68780 KB |
Output is correct |
6 |
Correct |
467 ms |
75560 KB |
Output is correct |
7 |
Correct |
497 ms |
83108 KB |
Output is correct |
8 |
Correct |
460 ms |
83032 KB |
Output is correct |
9 |
Correct |
466 ms |
83064 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 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 |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
472 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
472 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
1108 KB |
Output is correct |
41 |
Correct |
3 ms |
2388 KB |
Output is correct |
42 |
Correct |
4 ms |
2420 KB |
Output is correct |
43 |
Correct |
3 ms |
2388 KB |
Output is correct |
44 |
Correct |
4 ms |
2356 KB |
Output is correct |
45 |
Correct |
5 ms |
2388 KB |
Output is correct |
46 |
Correct |
5 ms |
2392 KB |
Output is correct |
47 |
Correct |
5 ms |
2388 KB |
Output is correct |
48 |
Correct |
7 ms |
2388 KB |
Output is correct |
49 |
Correct |
5 ms |
2388 KB |
Output is correct |
50 |
Correct |
4 ms |
2384 KB |
Output is correct |
51 |
Correct |
5 ms |
2388 KB |
Output is correct |
52 |
Correct |
5 ms |
2388 KB |
Output is correct |
53 |
Correct |
4 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
472 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
1108 KB |
Output is correct |
41 |
Correct |
3 ms |
2388 KB |
Output is correct |
42 |
Correct |
4 ms |
2420 KB |
Output is correct |
43 |
Correct |
3 ms |
2388 KB |
Output is correct |
44 |
Correct |
4 ms |
2356 KB |
Output is correct |
45 |
Correct |
5 ms |
2388 KB |
Output is correct |
46 |
Correct |
5 ms |
2392 KB |
Output is correct |
47 |
Correct |
5 ms |
2388 KB |
Output is correct |
48 |
Correct |
7 ms |
2388 KB |
Output is correct |
49 |
Correct |
5 ms |
2388 KB |
Output is correct |
50 |
Correct |
4 ms |
2384 KB |
Output is correct |
51 |
Correct |
5 ms |
2388 KB |
Output is correct |
52 |
Correct |
5 ms |
2388 KB |
Output is correct |
53 |
Correct |
4 ms |
2388 KB |
Output is correct |
54 |
Correct |
17 ms |
6204 KB |
Output is correct |
55 |
Correct |
58 ms |
20172 KB |
Output is correct |
56 |
Correct |
86 ms |
20064 KB |
Output is correct |
57 |
Correct |
86 ms |
20100 KB |
Output is correct |
58 |
Correct |
89 ms |
20044 KB |
Output is correct |
59 |
Correct |
91 ms |
20140 KB |
Output is correct |
60 |
Correct |
85 ms |
20144 KB |
Output is correct |
61 |
Correct |
95 ms |
20068 KB |
Output is correct |
62 |
Correct |
76 ms |
20120 KB |
Output is correct |
63 |
Correct |
101 ms |
20080 KB |
Output is correct |
64 |
Correct |
97 ms |
20048 KB |
Output is correct |
65 |
Correct |
98 ms |
20100 KB |
Output is correct |
66 |
Correct |
98 ms |
20080 KB |
Output is correct |
67 |
Correct |
87 ms |
20088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
250 ms |
54212 KB |
Output is correct |
14 |
Correct |
280 ms |
62024 KB |
Output is correct |
15 |
Correct |
323 ms |
68780 KB |
Output is correct |
16 |
Correct |
467 ms |
75560 KB |
Output is correct |
17 |
Correct |
497 ms |
83108 KB |
Output is correct |
18 |
Correct |
460 ms |
83032 KB |
Output is correct |
19 |
Correct |
466 ms |
83064 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
468 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
592 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
592 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
472 KB |
Output is correct |
56 |
Correct |
1 ms |
596 KB |
Output is correct |
57 |
Correct |
1 ms |
1108 KB |
Output is correct |
58 |
Correct |
3 ms |
2388 KB |
Output is correct |
59 |
Correct |
4 ms |
2420 KB |
Output is correct |
60 |
Correct |
3 ms |
2388 KB |
Output is correct |
61 |
Correct |
4 ms |
2356 KB |
Output is correct |
62 |
Correct |
5 ms |
2388 KB |
Output is correct |
63 |
Correct |
5 ms |
2392 KB |
Output is correct |
64 |
Correct |
5 ms |
2388 KB |
Output is correct |
65 |
Correct |
7 ms |
2388 KB |
Output is correct |
66 |
Correct |
5 ms |
2388 KB |
Output is correct |
67 |
Correct |
4 ms |
2384 KB |
Output is correct |
68 |
Correct |
5 ms |
2388 KB |
Output is correct |
69 |
Correct |
5 ms |
2388 KB |
Output is correct |
70 |
Correct |
4 ms |
2388 KB |
Output is correct |
71 |
Correct |
17 ms |
6204 KB |
Output is correct |
72 |
Correct |
58 ms |
20172 KB |
Output is correct |
73 |
Correct |
86 ms |
20064 KB |
Output is correct |
74 |
Correct |
86 ms |
20100 KB |
Output is correct |
75 |
Correct |
89 ms |
20044 KB |
Output is correct |
76 |
Correct |
91 ms |
20140 KB |
Output is correct |
77 |
Correct |
85 ms |
20144 KB |
Output is correct |
78 |
Correct |
95 ms |
20068 KB |
Output is correct |
79 |
Correct |
76 ms |
20120 KB |
Output is correct |
80 |
Correct |
101 ms |
20080 KB |
Output is correct |
81 |
Correct |
97 ms |
20048 KB |
Output is correct |
82 |
Correct |
98 ms |
20100 KB |
Output is correct |
83 |
Correct |
98 ms |
20080 KB |
Output is correct |
84 |
Correct |
87 ms |
20088 KB |
Output is correct |
85 |
Correct |
238 ms |
42396 KB |
Output is correct |
86 |
Correct |
604 ms |
83020 KB |
Output is correct |
87 |
Correct |
614 ms |
83012 KB |
Output is correct |
88 |
Correct |
609 ms |
83056 KB |
Output is correct |
89 |
Correct |
598 ms |
83152 KB |
Output is correct |
90 |
Correct |
600 ms |
83020 KB |
Output is correct |
91 |
Correct |
676 ms |
83188 KB |
Output is correct |
92 |
Correct |
654 ms |
83112 KB |
Output is correct |
93 |
Correct |
652 ms |
83184 KB |
Output is correct |
94 |
Correct |
628 ms |
83132 KB |
Output is correct |
95 |
Correct |
714 ms |
83084 KB |
Output is correct |
96 |
Correct |
723 ms |
83084 KB |
Output is correct |
97 |
Correct |
591 ms |
83096 KB |
Output is correct |