#include <bits/stdc++.h>
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
struct SegTree {
int n;
vector<ll> tree, lazy;
SegTree(int _n) : n(_n), tree(4*_n+5), lazy(4*_n+5) {}
void push(int u, int tl, int tr) {
if(!lazy[u]) return ;
tree[u] += lazy[u];
if(tl != tr) {
lazy[2*u] += lazy[u];
lazy[2*u+1] += lazy[u];
}
lazy[u] = 0;
}
void update(int u, int tl, int tr, int l, int r, ll v) {
push(u, tl, tr);
if(tl > tr || l > tr || tl > r) return ;
if(l <= tl && tr <= r) {
lazy[u] += v;
push(u, tl, tr);
return ;
}
int tm = (tl + tr) / 2;
update(2*u, tl, tm, l, r, v);
update(2*u+1, tm+1, tr, l, r, v);
tree[u] = min(tree[2*u], tree[2*u+1]);
}
ll query(int u, int tl, int tr, int l, int r) {
if(tl > tr || l > tr || tl > r) return 1e12;
push(u, tl, tr);
if(l <= tl && tr <= r) return tree[u];
int tm = (tl + tr) / 2;
return min(query(2*u, tl, tm, l, r), query(2*u+1, tm+1, tr, l, r));
}
void update(int l, int r, ll v) { update(1, 0, n-1, l, r, v); }
ll query(int l, int r) { return query(1, 0, n-1, l, r); }
};
ll dp[maxn][105], v[maxn];
signed main() {
int n, k;
cin >> n >> k;
for(int i=0; i<=n; i++)
for(int j=0; j<=k; j++) dp[i][j] = 1e9;
for(int i=1; i<=n; i++) cin >> v[i];
dp[0][0] = 0;
ll mx = 0;
for(int i=1; i<=n; i++) {
mx = max(mx, v[i]);
dp[i][1] = mx;
}
for(int j=2; j<=k; j++) {
SegTree tree(n+1);
vector<pii> st; st.push_back({ 1e9, 0 });
int SZ = 1;
for(int i=1; i<=n; i++) {
int last = st.back().second;
while(SZ && st.back().first <= v[i]) {
if(SZ > 1) tree.update(st[SZ-2].second+1, st[SZ-1].second, v[i]-st.back().first);
st.pop_back();
SZ--;
}
st.push_back({ v[i], i }); SZ++;
tree.update(last+1, st.back().second, v[i]);
tree.update(i, i, dp[i-1][j-1]);
dp[i][j] = tree.query(1, i);
}
}
cout << dp[n][k] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2488 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2488 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2484 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2488 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2392 KB |
Output is correct |
30 |
Correct |
0 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2488 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2484 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
2 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
2396 KB |
Output is correct |
41 |
Correct |
0 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
0 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
0 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2392 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2392 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
0 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
3 ms |
2396 KB |
Output is correct |
54 |
Correct |
3 ms |
2504 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
3 ms |
2396 KB |
Output is correct |
57 |
Correct |
2 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2488 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2392 KB |
Output is correct |
30 |
Correct |
0 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2488 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2484 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
2 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
2396 KB |
Output is correct |
41 |
Correct |
0 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
0 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
0 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2392 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2392 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
0 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
3 ms |
2396 KB |
Output is correct |
54 |
Correct |
3 ms |
2504 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
3 ms |
2396 KB |
Output is correct |
57 |
Correct |
2 ms |
2392 KB |
Output is correct |
58 |
Correct |
143 ms |
11484 KB |
Output is correct |
59 |
Correct |
13 ms |
39772 KB |
Output is correct |
60 |
Correct |
186 ms |
42676 KB |
Output is correct |
61 |
Execution timed out |
1069 ms |
42916 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |