#include <bits/stdc++.h>
using namespace std;
#define lesgooo ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\n'
#define ll long long
#pragma GCC optimize("Ofast")
ll dp[2][100'005];
int a[100'005], sparse[17][100'005], lg[100'005];
void build(int n)
{
for (int i = 1; i <= n; i++) sparse[0][i] = a[i];
for (int j = 1; j < 17; j++) for (int i = 1; i + (1 << (j - 1)) <= n; i++) sparse[j][i] = max(sparse[j-1][i], sparse[j-1][i + (1 << (j - 1))]);
}
int query(int l, int r)
{
int sz = lg[r-l+1];
return max(sparse[sz][l], sparse[sz][r-(1 << sz) + 1]);
}
int n, m;
int mstanyeh(int lvl, int j, int k)
{
int l = max(j + 1, k + 1), r = n;
while (l <= r)
{
int md = ((l + r) >> 1);
if (dp[lvl & 1][j] - dp[lvl & 1][k] <= query(k + 1, md) - query(j + 1, md)) r = md - 1;
else l = md + 1;
}
return l;
}
signed main()
{
lesgooo;
lg[1] = 0;
for (int i = 2; i < 100'005; i++) lg[i] = lg[i >> 1] + 1;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
build(n);
for (int i = 1; i <= n; i++) dp[0][i] = query(1, i);
dp[0][0] = dp[1][0] = 1e18;
for (int i = 1; i < m; i++)
{
vector<int> v;
for (int j = 1; j <= n; j++)
{
while (v.size() > 1 && mstanyeh(i - 1, v[v.size() - 2], v.back()) <= mstanyeh(i - 1, v.back(), j - 1)) v.pop_back();
v.push_back(j - 1);
while (v.size() > 1 && mstanyeh(i - 1, v[v.size() - 2], v.back()) <= j) v.pop_back();
dp[i&1][j] = dp[(i - 1)&1][v.back()] + query(v.back() + 1, j);
}
}
cout << dp[(m - 1) & 1][n] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4696 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4696 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4696 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6744 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4696 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4696 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4696 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
1 ms |
6744 KB |
Output is correct |
34 |
Correct |
1 ms |
6748 KB |
Output is correct |
35 |
Correct |
1 ms |
6748 KB |
Output is correct |
36 |
Correct |
1 ms |
6748 KB |
Output is correct |
37 |
Correct |
1 ms |
6748 KB |
Output is correct |
38 |
Correct |
1 ms |
6748 KB |
Output is correct |
39 |
Correct |
1 ms |
6748 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
1 ms |
4696 KB |
Output is correct |
42 |
Correct |
1 ms |
4700 KB |
Output is correct |
43 |
Correct |
1 ms |
4700 KB |
Output is correct |
44 |
Correct |
1 ms |
4696 KB |
Output is correct |
45 |
Correct |
1 ms |
4696 KB |
Output is correct |
46 |
Correct |
1 ms |
4700 KB |
Output is correct |
47 |
Correct |
1 ms |
4700 KB |
Output is correct |
48 |
Correct |
1 ms |
4696 KB |
Output is correct |
49 |
Correct |
1 ms |
4696 KB |
Output is correct |
50 |
Correct |
1 ms |
4700 KB |
Output is correct |
51 |
Correct |
1 ms |
6748 KB |
Output is correct |
52 |
Correct |
1 ms |
6748 KB |
Output is correct |
53 |
Correct |
2 ms |
6748 KB |
Output is correct |
54 |
Correct |
2 ms |
6748 KB |
Output is correct |
55 |
Correct |
1 ms |
6748 KB |
Output is correct |
56 |
Correct |
2 ms |
6788 KB |
Output is correct |
57 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4696 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4696 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4696 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
1 ms |
6744 KB |
Output is correct |
34 |
Correct |
1 ms |
6748 KB |
Output is correct |
35 |
Correct |
1 ms |
6748 KB |
Output is correct |
36 |
Correct |
1 ms |
6748 KB |
Output is correct |
37 |
Correct |
1 ms |
6748 KB |
Output is correct |
38 |
Correct |
1 ms |
6748 KB |
Output is correct |
39 |
Correct |
1 ms |
6748 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
1 ms |
4696 KB |
Output is correct |
42 |
Correct |
1 ms |
4700 KB |
Output is correct |
43 |
Correct |
1 ms |
4700 KB |
Output is correct |
44 |
Correct |
1 ms |
4696 KB |
Output is correct |
45 |
Correct |
1 ms |
4696 KB |
Output is correct |
46 |
Correct |
1 ms |
4700 KB |
Output is correct |
47 |
Correct |
1 ms |
4700 KB |
Output is correct |
48 |
Correct |
1 ms |
4696 KB |
Output is correct |
49 |
Correct |
1 ms |
4696 KB |
Output is correct |
50 |
Correct |
1 ms |
4700 KB |
Output is correct |
51 |
Correct |
1 ms |
6748 KB |
Output is correct |
52 |
Correct |
1 ms |
6748 KB |
Output is correct |
53 |
Correct |
2 ms |
6748 KB |
Output is correct |
54 |
Correct |
2 ms |
6748 KB |
Output is correct |
55 |
Correct |
1 ms |
6748 KB |
Output is correct |
56 |
Correct |
2 ms |
6788 KB |
Output is correct |
57 |
Correct |
1 ms |
6748 KB |
Output is correct |
58 |
Correct |
56 ms |
8792 KB |
Output is correct |
59 |
Correct |
4 ms |
8796 KB |
Output is correct |
60 |
Correct |
84 ms |
9008 KB |
Output is correct |
61 |
Correct |
879 ms |
9008 KB |
Output is correct |
62 |
Execution timed out |
1047 ms |
9428 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |