#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define md ((l + r) >> 1)
#pragma GCC optimize("Ofast,O3")
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)
{
if (dp[lvl & 1][j] - dp[lvl & 1][k] > query(k + 1, n) - query(j + 1, n)) return n + 1;
int l = max(j + 1, k + 1), r = n;
while (l <= r)
{
dp[lvl & 1][j] - dp[lvl & 1][k] <= query(k + 1, md) - query(j + 1, md)? r = md - 1 : l = md + 1;
}
return l;
}
int v[100'005], mstany[100'005], vsz, mstanysz, val;
signed main()
{
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++)
{
vsz = mstanysz = 0;
for (int j = 1; j <= n; j++)
{
while (vsz > 1 && mstany[mstanysz-1] <= (val = mstanyeh(i - 1, v[vsz-1], j - 1))) vsz--, mstanysz--;
if (vsz == 1) val = mstanyeh(i - 1, v[vsz-1], j - 1);
v[vsz++] = j - 1;
if (vsz > 1) mstany[mstanysz++] = val;
while (vsz > 1 && mstany[mstanysz-1] <= j) vsz--, mstanysz--;
dp[i&1][j] = dp[(i - 1)&1][v[vsz-1]] + query(v[vsz-1] + 1, j);
}
}
cout << dp[(m - 1) & 1][n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
3 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6692 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
3 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
6488 KB |
Output is correct |
27 |
Correct |
1 ms |
6488 KB |
Output is correct |
28 |
Correct |
1 ms |
6488 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
1 ms |
6488 KB |
Output is correct |
33 |
Correct |
1 ms |
6692 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6488 KB |
Output is correct |
36 |
Correct |
1 ms |
6488 KB |
Output is correct |
37 |
Correct |
1 ms |
6488 KB |
Output is correct |
38 |
Correct |
1 ms |
6488 KB |
Output is correct |
39 |
Correct |
1 ms |
6488 KB |
Output is correct |
40 |
Correct |
1 ms |
4440 KB |
Output is correct |
41 |
Correct |
1 ms |
4440 KB |
Output is correct |
42 |
Correct |
1 ms |
4696 KB |
Output is correct |
43 |
Correct |
1 ms |
4440 KB |
Output is correct |
44 |
Correct |
1 ms |
4440 KB |
Output is correct |
45 |
Correct |
1 ms |
4440 KB |
Output is correct |
46 |
Correct |
1 ms |
4440 KB |
Output is correct |
47 |
Correct |
1 ms |
6488 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 |
6488 KB |
Output is correct |
51 |
Correct |
1 ms |
6488 KB |
Output is correct |
52 |
Correct |
1 ms |
6488 KB |
Output is correct |
53 |
Correct |
1 ms |
6492 KB |
Output is correct |
54 |
Correct |
1 ms |
6488 KB |
Output is correct |
55 |
Correct |
1 ms |
6488 KB |
Output is correct |
56 |
Correct |
2 ms |
6488 KB |
Output is correct |
57 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
3 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
6488 KB |
Output is correct |
27 |
Correct |
1 ms |
6488 KB |
Output is correct |
28 |
Correct |
1 ms |
6488 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6488 KB |
Output is correct |
31 |
Correct |
1 ms |
6488 KB |
Output is correct |
32 |
Correct |
1 ms |
6488 KB |
Output is correct |
33 |
Correct |
1 ms |
6692 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6488 KB |
Output is correct |
36 |
Correct |
1 ms |
6488 KB |
Output is correct |
37 |
Correct |
1 ms |
6488 KB |
Output is correct |
38 |
Correct |
1 ms |
6488 KB |
Output is correct |
39 |
Correct |
1 ms |
6488 KB |
Output is correct |
40 |
Correct |
1 ms |
4440 KB |
Output is correct |
41 |
Correct |
1 ms |
4440 KB |
Output is correct |
42 |
Correct |
1 ms |
4696 KB |
Output is correct |
43 |
Correct |
1 ms |
4440 KB |
Output is correct |
44 |
Correct |
1 ms |
4440 KB |
Output is correct |
45 |
Correct |
1 ms |
4440 KB |
Output is correct |
46 |
Correct |
1 ms |
4440 KB |
Output is correct |
47 |
Correct |
1 ms |
6488 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 |
6488 KB |
Output is correct |
51 |
Correct |
1 ms |
6488 KB |
Output is correct |
52 |
Correct |
1 ms |
6488 KB |
Output is correct |
53 |
Correct |
1 ms |
6492 KB |
Output is correct |
54 |
Correct |
1 ms |
6488 KB |
Output is correct |
55 |
Correct |
1 ms |
6488 KB |
Output is correct |
56 |
Correct |
2 ms |
6488 KB |
Output is correct |
57 |
Correct |
1 ms |
6492 KB |
Output is correct |
58 |
Correct |
18 ms |
8536 KB |
Output is correct |
59 |
Correct |
11 ms |
8792 KB |
Output is correct |
60 |
Correct |
30 ms |
9048 KB |
Output is correct |
61 |
Correct |
247 ms |
9156 KB |
Output is correct |
62 |
Correct |
829 ms |
10184 KB |
Output is correct |
63 |
Correct |
38 ms |
10064 KB |
Output is correct |
64 |
Correct |
367 ms |
10016 KB |
Output is correct |
65 |
Correct |
1 ms |
6488 KB |
Output is correct |
66 |
Correct |
1 ms |
6488 KB |
Output is correct |
67 |
Correct |
1 ms |
6492 KB |
Output is correct |
68 |
Correct |
1 ms |
6488 KB |
Output is correct |
69 |
Correct |
2 ms |
8536 KB |
Output is correct |
70 |
Correct |
2 ms |
8536 KB |
Output is correct |
71 |
Correct |
72 ms |
8536 KB |
Output is correct |
72 |
Correct |
5 ms |
8536 KB |
Output is correct |
73 |
Correct |
22 ms |
8736 KB |
Output is correct |
74 |
Correct |
11 ms |
8796 KB |
Output is correct |
75 |
Correct |
34 ms |
9304 KB |
Output is correct |
76 |
Correct |
278 ms |
9048 KB |
Output is correct |
77 |
Correct |
856 ms |
10012 KB |
Output is correct |
78 |
Correct |
42 ms |
9808 KB |
Output is correct |
79 |
Correct |
394 ms |
9916 KB |
Output is correct |
80 |
Correct |
25 ms |
9040 KB |
Output is correct |
81 |
Correct |
105 ms |
10012 KB |
Output is correct |
82 |
Correct |
856 ms |
10064 KB |
Output is correct |
83 |
Correct |
2 ms |
8540 KB |
Output is correct |
84 |
Correct |
2 ms |
8540 KB |
Output is correct |
85 |
Correct |
21 ms |
8536 KB |
Output is correct |
86 |
Correct |
4 ms |
8536 KB |
Output is correct |
87 |
Correct |
9 ms |
8728 KB |
Output is correct |
88 |
Correct |
12 ms |
8792 KB |
Output is correct |
89 |
Correct |
21 ms |
9048 KB |
Output is correct |
90 |
Correct |
75 ms |
9156 KB |
Output is correct |
91 |
Correct |
219 ms |
9812 KB |
Output is correct |
92 |
Correct |
42 ms |
9808 KB |
Output is correct |
93 |
Correct |
113 ms |
9912 KB |
Output is correct |
94 |
Correct |
4 ms |
8536 KB |
Output is correct |
95 |
Correct |
63 ms |
8792 KB |
Output is correct |
96 |
Correct |
2 ms |
8536 KB |
Output is correct |
97 |
Correct |
1 ms |
8536 KB |
Output is correct |
98 |
Correct |
1 ms |
6488 KB |
Output is correct |
99 |
Correct |
1 ms |
6492 KB |
Output is correct |