#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 100 , K = 1e2 + 1 , LOG = 18;
int n , k , a[N] , l[N] , dp[N] , ndp[N];
struct node{
int dp_min , mn , lz;
};
vector<node> t(4 * N);
node merge(node x , node y){
x.dp_min = min(x.dp_min , y.dp_min);
x.mn = min(x.mn , y.mn);
return x;
}
void push(int u){
if(t[u].lz == -1){
return;
}
t[u * 2].mn = t[u].lz + t[u * 2].dp_min;
t[u * 2 + 1].mn = t[u].lz + t[u * 2 + 1].mn;
t[u * 2].lz = t[u].lz;
t[u * 2 + 1].lz = t[u].lz;
t[u].lz = -1;
}
void build(int u , int l , int r){
if(l == r){
t[u].dp_min = dp[l - 1];
t[u].mn = 1e9;
t[u].lz = -1;
return;
}
int m = (l + r) / 2;
build(u * 2 , l , m);
build(u * 2 + 1 , m + 1 , r);
t[u] = merge(t[u * 2] , t[u * 2 + 1]);
}
void upd(int u , int l , int r , int L , int R , int x){
if(l > r || r < L || l > R){
return;
}
if(L <= l && r <= R){
t[u].mn = t[u].dp_min + x;
t[u].lz = x;
return;
}
int m = (l + r) / 2;
upd(u * 2 , l , m , L , R , x);
upd(u * 2 + 1 , m + 1 , r , L , R , x);
t[u] = merge(t[u * 2] , t[u * 2 + 1]);
}
main(){
cin >> n >> k;
int _mx = 0;
dp[0] = 1e9;
for(int i = 1;i <= n;i ++){
cin >> a[i];
l[i] = i - 1;
while(l[i] > 0 && a[i] > a[l[i]]){
l[i] = l[l[i]];
}
_mx = max(_mx , a[i]);
dp[i] =_mx;
}
for(int x = 1;x < k;x ++){
for(int i = 0;i <= n;i ++){
ndp[i] = dp[i];
}
build(1 , 1 , n);
for(int i = 1;i <= n;i ++){
upd(1 , 1 , n , l[i] + 1 , i , a[i]);
dp[i] = t[1].mn;
}
}
cout << dp[n] << endl;
}
Compilation message
blocks.cpp:51:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
51 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9820 KB |
Output is correct |
7 |
Correct |
3 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9816 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
5 ms |
9808 KB |
Output is correct |
11 |
Correct |
5 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
3 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9800 KB |
Output is correct |
2 |
Correct |
4 ms |
9816 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9672 KB |
Output is correct |
5 |
Correct |
3 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9816 KB |
Output is correct |
7 |
Correct |
5 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9816 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9756 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9836 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
3 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9816 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
4 ms |
9816 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9820 KB |
Output is correct |
7 |
Correct |
3 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9816 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
5 ms |
9808 KB |
Output is correct |
11 |
Correct |
5 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
3 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
3 ms |
9800 KB |
Output is correct |
20 |
Correct |
4 ms |
9816 KB |
Output is correct |
21 |
Correct |
5 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9672 KB |
Output is correct |
23 |
Correct |
3 ms |
9820 KB |
Output is correct |
24 |
Correct |
5 ms |
9816 KB |
Output is correct |
25 |
Correct |
5 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
9816 KB |
Output is correct |
27 |
Correct |
4 ms |
9820 KB |
Output is correct |
28 |
Correct |
4 ms |
9756 KB |
Output is correct |
29 |
Correct |
4 ms |
9820 KB |
Output is correct |
30 |
Correct |
5 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9836 KB |
Output is correct |
32 |
Correct |
4 ms |
9820 KB |
Output is correct |
33 |
Correct |
3 ms |
9820 KB |
Output is correct |
34 |
Correct |
4 ms |
9816 KB |
Output is correct |
35 |
Correct |
4 ms |
9820 KB |
Output is correct |
36 |
Correct |
4 ms |
9820 KB |
Output is correct |
37 |
Correct |
4 ms |
9816 KB |
Output is correct |
38 |
Correct |
4 ms |
9820 KB |
Output is correct |
39 |
Correct |
4 ms |
9820 KB |
Output is correct |
40 |
Correct |
4 ms |
9820 KB |
Output is correct |
41 |
Correct |
4 ms |
9820 KB |
Output is correct |
42 |
Correct |
4 ms |
9816 KB |
Output is correct |
43 |
Correct |
3 ms |
9820 KB |
Output is correct |
44 |
Correct |
5 ms |
9960 KB |
Output is correct |
45 |
Correct |
4 ms |
9816 KB |
Output is correct |
46 |
Correct |
3 ms |
9820 KB |
Output is correct |
47 |
Correct |
4 ms |
9820 KB |
Output is correct |
48 |
Correct |
3 ms |
9820 KB |
Output is correct |
49 |
Correct |
3 ms |
9820 KB |
Output is correct |
50 |
Correct |
4 ms |
9820 KB |
Output is correct |
51 |
Correct |
3 ms |
9820 KB |
Output is correct |
52 |
Correct |
4 ms |
9704 KB |
Output is correct |
53 |
Correct |
4 ms |
9820 KB |
Output is correct |
54 |
Correct |
4 ms |
9860 KB |
Output is correct |
55 |
Correct |
4 ms |
9740 KB |
Output is correct |
56 |
Correct |
4 ms |
9816 KB |
Output is correct |
57 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
3 ms |
9820 KB |
Output is correct |
7 |
Correct |
3 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9816 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
5 ms |
9808 KB |
Output is correct |
11 |
Correct |
5 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
3 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
3 ms |
9800 KB |
Output is correct |
20 |
Correct |
4 ms |
9816 KB |
Output is correct |
21 |
Correct |
5 ms |
9820 KB |
Output is correct |
22 |
Correct |
5 ms |
9672 KB |
Output is correct |
23 |
Correct |
3 ms |
9820 KB |
Output is correct |
24 |
Correct |
5 ms |
9816 KB |
Output is correct |
25 |
Correct |
5 ms |
9820 KB |
Output is correct |
26 |
Correct |
4 ms |
9816 KB |
Output is correct |
27 |
Correct |
4 ms |
9820 KB |
Output is correct |
28 |
Correct |
4 ms |
9756 KB |
Output is correct |
29 |
Correct |
4 ms |
9820 KB |
Output is correct |
30 |
Correct |
5 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9836 KB |
Output is correct |
32 |
Correct |
4 ms |
9820 KB |
Output is correct |
33 |
Correct |
3 ms |
9820 KB |
Output is correct |
34 |
Correct |
4 ms |
9816 KB |
Output is correct |
35 |
Correct |
4 ms |
9820 KB |
Output is correct |
36 |
Correct |
4 ms |
9820 KB |
Output is correct |
37 |
Correct |
4 ms |
9816 KB |
Output is correct |
38 |
Correct |
4 ms |
9820 KB |
Output is correct |
39 |
Correct |
4 ms |
9820 KB |
Output is correct |
40 |
Correct |
4 ms |
9820 KB |
Output is correct |
41 |
Correct |
4 ms |
9820 KB |
Output is correct |
42 |
Correct |
4 ms |
9816 KB |
Output is correct |
43 |
Correct |
3 ms |
9820 KB |
Output is correct |
44 |
Correct |
5 ms |
9960 KB |
Output is correct |
45 |
Correct |
4 ms |
9816 KB |
Output is correct |
46 |
Correct |
3 ms |
9820 KB |
Output is correct |
47 |
Correct |
4 ms |
9820 KB |
Output is correct |
48 |
Correct |
3 ms |
9820 KB |
Output is correct |
49 |
Correct |
3 ms |
9820 KB |
Output is correct |
50 |
Correct |
4 ms |
9820 KB |
Output is correct |
51 |
Correct |
3 ms |
9820 KB |
Output is correct |
52 |
Correct |
4 ms |
9704 KB |
Output is correct |
53 |
Correct |
4 ms |
9820 KB |
Output is correct |
54 |
Correct |
4 ms |
9860 KB |
Output is correct |
55 |
Correct |
4 ms |
9740 KB |
Output is correct |
56 |
Correct |
4 ms |
9816 KB |
Output is correct |
57 |
Correct |
4 ms |
9820 KB |
Output is correct |
58 |
Correct |
33 ms |
10076 KB |
Output is correct |
59 |
Correct |
12 ms |
11096 KB |
Output is correct |
60 |
Correct |
43 ms |
11356 KB |
Output is correct |
61 |
Correct |
371 ms |
11368 KB |
Output is correct |
62 |
Execution timed out |
1064 ms |
13660 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |