#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define md ((l + r) >> 1)
#pragma GCC optimize("Ofast,O3")
const int BUF_SZ = 1 << 20;
inline namespace Input {
char buf[BUF_SZ];
int pos;
int len;
char next_char() {
if (pos == len) {
pos = 0;
len = (int)fread(buf, 1, BUF_SZ, stdin);
if (!len) {
return EOF;
}
}
return buf[pos++];
}
int read_int() {
int x;
char ch;
int sgn = 1;
while (!isdigit(ch = next_char())) {
if (ch == '-') {
sgn *= -1;
}
}
x = ch - '0';
while (isdigit(ch = next_char())) {
x = x * 10 + (ch - '0');
}
return x * sgn;
}
long long read_ll() {
long long x;
char ch;
int sgn = 1;
while (!isdigit(ch = next_char())) {
if (ch == '-') {
sgn *= -1;
}
}
x = ch - '0';
while (isdigit(ch = next_char())) {
x = x * 10 + (ch - '0');
}
return x * sgn;
}
} // namespace Input
inline namespace Output {
char buf[BUF_SZ];
int pos;
void flush_out() {
fwrite(buf, 1, pos, stdout);
pos = 0;
}
void write_char(char c) {
if (pos == BUF_SZ) {
flush_out();
}
buf[pos++] = c;
}
void write_int(long long x) {
static char num_buf[100];
if (x < 0) {
write_char('-');
x *= -1;
}
int len = 0;
for (; x >= 10; x /= 10) {
num_buf[len++] = (char)('0' + (x % 10));
}
write_char((char)('0' + x));
while (len) {
write_char(num_buf[--len]);
}
write_char('\n');
}
// auto-flush output when program exits
void init_output() { assert(atexit(flush_out) == 0); }
} // namespace Output
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()
{
init_output();
lg[1] = 0;
for (int i = 2; i < 100'005; i++) lg[i] = lg[i >> 1] + 1;
n = read_int(), m = read_int();
for (int i = 1; i <= n; i++) a[i] = read_int();
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);
}
}
write_int(dp[(m - 1) & 1][n]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6600 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8652 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8792 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8948 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6600 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
1 ms |
8540 KB |
Output is correct |
28 |
Correct |
1 ms |
8540 KB |
Output is correct |
29 |
Correct |
1 ms |
8540 KB |
Output is correct |
30 |
Correct |
2 ms |
8652 KB |
Output is correct |
31 |
Correct |
1 ms |
8540 KB |
Output is correct |
32 |
Correct |
1 ms |
8540 KB |
Output is correct |
33 |
Correct |
1 ms |
8792 KB |
Output is correct |
34 |
Correct |
1 ms |
8540 KB |
Output is correct |
35 |
Correct |
1 ms |
8540 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
1 ms |
8948 KB |
Output is correct |
38 |
Correct |
1 ms |
8540 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6492 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 KB |
Output is correct |
44 |
Correct |
1 ms |
6504 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
2 ms |
8540 KB |
Output is correct |
48 |
Correct |
1 ms |
8540 KB |
Output is correct |
49 |
Correct |
1 ms |
8540 KB |
Output is correct |
50 |
Correct |
1 ms |
8540 KB |
Output is correct |
51 |
Correct |
2 ms |
8540 KB |
Output is correct |
52 |
Correct |
1 ms |
8540 KB |
Output is correct |
53 |
Correct |
2 ms |
8540 KB |
Output is correct |
54 |
Correct |
2 ms |
8540 KB |
Output is correct |
55 |
Correct |
1 ms |
8540 KB |
Output is correct |
56 |
Correct |
2 ms |
8540 KB |
Output is correct |
57 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6600 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
1 ms |
8540 KB |
Output is correct |
28 |
Correct |
1 ms |
8540 KB |
Output is correct |
29 |
Correct |
1 ms |
8540 KB |
Output is correct |
30 |
Correct |
2 ms |
8652 KB |
Output is correct |
31 |
Correct |
1 ms |
8540 KB |
Output is correct |
32 |
Correct |
1 ms |
8540 KB |
Output is correct |
33 |
Correct |
1 ms |
8792 KB |
Output is correct |
34 |
Correct |
1 ms |
8540 KB |
Output is correct |
35 |
Correct |
1 ms |
8540 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
1 ms |
8948 KB |
Output is correct |
38 |
Correct |
1 ms |
8540 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6492 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 KB |
Output is correct |
44 |
Correct |
1 ms |
6504 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
2 ms |
8540 KB |
Output is correct |
48 |
Correct |
1 ms |
8540 KB |
Output is correct |
49 |
Correct |
1 ms |
8540 KB |
Output is correct |
50 |
Correct |
1 ms |
8540 KB |
Output is correct |
51 |
Correct |
2 ms |
8540 KB |
Output is correct |
52 |
Correct |
1 ms |
8540 KB |
Output is correct |
53 |
Correct |
2 ms |
8540 KB |
Output is correct |
54 |
Correct |
2 ms |
8540 KB |
Output is correct |
55 |
Correct |
1 ms |
8540 KB |
Output is correct |
56 |
Correct |
2 ms |
8540 KB |
Output is correct |
57 |
Correct |
2 ms |
8540 KB |
Output is correct |
58 |
Correct |
17 ms |
10844 KB |
Output is correct |
59 |
Correct |
2 ms |
11100 KB |
Output is correct |
60 |
Correct |
21 ms |
11100 KB |
Output is correct |
61 |
Correct |
241 ms |
11292 KB |
Output is correct |
62 |
Correct |
788 ms |
12116 KB |
Output is correct |
63 |
Correct |
19 ms |
11868 KB |
Output is correct |
64 |
Correct |
365 ms |
12056 KB |
Output is correct |
65 |
Correct |
1 ms |
8552 KB |
Output is correct |
66 |
Correct |
2 ms |
8540 KB |
Output is correct |
67 |
Correct |
1 ms |
8540 KB |
Output is correct |
68 |
Correct |
1 ms |
8540 KB |
Output is correct |
69 |
Correct |
1 ms |
10588 KB |
Output is correct |
70 |
Correct |
2 ms |
10788 KB |
Output is correct |
71 |
Correct |
72 ms |
10588 KB |
Output is correct |
72 |
Correct |
3 ms |
10844 KB |
Output is correct |
73 |
Correct |
18 ms |
10844 KB |
Output is correct |
74 |
Correct |
3 ms |
11100 KB |
Output is correct |
75 |
Correct |
24 ms |
11352 KB |
Output is correct |
76 |
Correct |
274 ms |
11100 KB |
Output is correct |
77 |
Correct |
848 ms |
12040 KB |
Output is correct |
78 |
Correct |
23 ms |
11868 KB |
Output is correct |
79 |
Correct |
402 ms |
12036 KB |
Output is correct |
80 |
Correct |
3 ms |
11868 KB |
Output is correct |
81 |
Correct |
81 ms |
12052 KB |
Output is correct |
82 |
Correct |
850 ms |
12056 KB |
Output is correct |
83 |
Correct |
1 ms |
10588 KB |
Output is correct |
84 |
Correct |
2 ms |
10588 KB |
Output is correct |
85 |
Correct |
20 ms |
10844 KB |
Output is correct |
86 |
Correct |
2 ms |
10844 KB |
Output is correct |
87 |
Correct |
7 ms |
11096 KB |
Output is correct |
88 |
Correct |
2 ms |
11100 KB |
Output is correct |
89 |
Correct |
14 ms |
11248 KB |
Output is correct |
90 |
Correct |
67 ms |
11292 KB |
Output is correct |
91 |
Correct |
185 ms |
11868 KB |
Output is correct |
92 |
Correct |
16 ms |
11868 KB |
Output is correct |
93 |
Correct |
100 ms |
12060 KB |
Output is correct |
94 |
Correct |
2 ms |
10844 KB |
Output is correct |
95 |
Correct |
62 ms |
10844 KB |
Output is correct |
96 |
Correct |
2 ms |
10588 KB |
Output is correct |
97 |
Correct |
2 ms |
10588 KB |
Output is correct |
98 |
Correct |
1 ms |
8540 KB |
Output is correct |
99 |
Correct |
1 ms |
8540 KB |
Output is correct |