#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
const int nmax = 1e5 + 5;
const ll oo = 1e18, base = 311;
const int lg = 19, M = 105;
const ll mod = 1e9 + 7;
#define pii pair<ll, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';
using namespace std;
int n, k;
ll f[nmax][M], a[nmax], mi[nmax][M];
void ckmax(ll &a, ll b){
a = max(a, b);
}
void ckmin(ll &a, ll b){
a = min(a, b);
}
deque<int> de;
multiset<pii> mt;
int L[nmax];
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n >> k;
memset(f, 0x3f, sizeof f);
memset(mi, 0x3f, sizeof f);
for(int i = 1; i <= n; ++i){
cin >> a[i];
if(i == 1) f[1][1] = a[i];
else f[i][1] = max(f[i - 1][1], a[i]);
}
for(int j = 1; j <= k; ++j){
de.clear();mt.clear();
for(int i = 1; i <= n; ++i){
if(j > 1){
while(de.size() && a[de.back()] <= a[i]){
int u = de.back();
mt.erase({mi[u][j - 1] + a[u], u});
de.pop_back();
}
de.push_back(i);
mt.insert({mi[i][j - 1] + a[i], i});
auto it = *mt.begin();
f[i][j] = it.fi;
}
L[i] = i - 1;
mi[i][j] = f[i - 1][j];
while(L[i] && a[L[i]] <= a[i]){
ckmin(mi[i][j],mi[L[i]][j]);
L[i] = L[L[i]];
}
}
}
cout << f[n][k];
}
/*
5 2
1 2 3 4 5
*/
Compilation message
blocks.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
28 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
165700 KB |
Output is correct |
2 |
Correct |
59 ms |
165716 KB |
Output is correct |
3 |
Correct |
59 ms |
165712 KB |
Output is correct |
4 |
Correct |
58 ms |
165632 KB |
Output is correct |
5 |
Correct |
59 ms |
165732 KB |
Output is correct |
6 |
Correct |
60 ms |
165652 KB |
Output is correct |
7 |
Correct |
58 ms |
165648 KB |
Output is correct |
8 |
Correct |
59 ms |
165708 KB |
Output is correct |
9 |
Correct |
68 ms |
165724 KB |
Output is correct |
10 |
Correct |
41 ms |
165620 KB |
Output is correct |
11 |
Correct |
41 ms |
165684 KB |
Output is correct |
12 |
Correct |
47 ms |
165712 KB |
Output is correct |
13 |
Correct |
44 ms |
165712 KB |
Output is correct |
14 |
Correct |
49 ms |
165656 KB |
Output is correct |
15 |
Correct |
39 ms |
165724 KB |
Output is correct |
16 |
Correct |
39 ms |
165712 KB |
Output is correct |
17 |
Correct |
38 ms |
165712 KB |
Output is correct |
18 |
Correct |
43 ms |
165712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
165744 KB |
Output is correct |
2 |
Correct |
45 ms |
165672 KB |
Output is correct |
3 |
Correct |
42 ms |
165716 KB |
Output is correct |
4 |
Correct |
46 ms |
165688 KB |
Output is correct |
5 |
Correct |
41 ms |
165716 KB |
Output is correct |
6 |
Correct |
44 ms |
165604 KB |
Output is correct |
7 |
Correct |
45 ms |
165668 KB |
Output is correct |
8 |
Correct |
44 ms |
165816 KB |
Output is correct |
9 |
Correct |
40 ms |
165716 KB |
Output is correct |
10 |
Correct |
40 ms |
165712 KB |
Output is correct |
11 |
Correct |
42 ms |
165712 KB |
Output is correct |
12 |
Correct |
39 ms |
165712 KB |
Output is correct |
13 |
Correct |
42 ms |
165716 KB |
Output is correct |
14 |
Correct |
39 ms |
165656 KB |
Output is correct |
15 |
Correct |
41 ms |
165712 KB |
Output is correct |
16 |
Correct |
38 ms |
165720 KB |
Output is correct |
17 |
Correct |
39 ms |
165716 KB |
Output is correct |
18 |
Correct |
37 ms |
165716 KB |
Output is correct |
19 |
Correct |
37 ms |
165724 KB |
Output is correct |
20 |
Correct |
43 ms |
165708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
165700 KB |
Output is correct |
2 |
Correct |
59 ms |
165716 KB |
Output is correct |
3 |
Correct |
59 ms |
165712 KB |
Output is correct |
4 |
Correct |
58 ms |
165632 KB |
Output is correct |
5 |
Correct |
59 ms |
165732 KB |
Output is correct |
6 |
Correct |
60 ms |
165652 KB |
Output is correct |
7 |
Correct |
58 ms |
165648 KB |
Output is correct |
8 |
Correct |
59 ms |
165708 KB |
Output is correct |
9 |
Correct |
68 ms |
165724 KB |
Output is correct |
10 |
Correct |
41 ms |
165620 KB |
Output is correct |
11 |
Correct |
41 ms |
165684 KB |
Output is correct |
12 |
Correct |
47 ms |
165712 KB |
Output is correct |
13 |
Correct |
44 ms |
165712 KB |
Output is correct |
14 |
Correct |
49 ms |
165656 KB |
Output is correct |
15 |
Correct |
39 ms |
165724 KB |
Output is correct |
16 |
Correct |
39 ms |
165712 KB |
Output is correct |
17 |
Correct |
38 ms |
165712 KB |
Output is correct |
18 |
Correct |
43 ms |
165712 KB |
Output is correct |
19 |
Correct |
44 ms |
165744 KB |
Output is correct |
20 |
Correct |
45 ms |
165672 KB |
Output is correct |
21 |
Correct |
42 ms |
165716 KB |
Output is correct |
22 |
Correct |
46 ms |
165688 KB |
Output is correct |
23 |
Correct |
41 ms |
165716 KB |
Output is correct |
24 |
Correct |
44 ms |
165604 KB |
Output is correct |
25 |
Correct |
45 ms |
165668 KB |
Output is correct |
26 |
Correct |
44 ms |
165816 KB |
Output is correct |
27 |
Correct |
40 ms |
165716 KB |
Output is correct |
28 |
Correct |
40 ms |
165712 KB |
Output is correct |
29 |
Correct |
42 ms |
165712 KB |
Output is correct |
30 |
Correct |
39 ms |
165712 KB |
Output is correct |
31 |
Correct |
42 ms |
165716 KB |
Output is correct |
32 |
Correct |
39 ms |
165656 KB |
Output is correct |
33 |
Correct |
41 ms |
165712 KB |
Output is correct |
34 |
Correct |
38 ms |
165720 KB |
Output is correct |
35 |
Correct |
39 ms |
165716 KB |
Output is correct |
36 |
Correct |
37 ms |
165716 KB |
Output is correct |
37 |
Correct |
37 ms |
165724 KB |
Output is correct |
38 |
Correct |
43 ms |
165708 KB |
Output is correct |
39 |
Correct |
37 ms |
165712 KB |
Output is correct |
40 |
Correct |
39 ms |
165716 KB |
Output is correct |
41 |
Correct |
38 ms |
165716 KB |
Output is correct |
42 |
Correct |
38 ms |
165716 KB |
Output is correct |
43 |
Correct |
38 ms |
165656 KB |
Output is correct |
44 |
Correct |
43 ms |
165720 KB |
Output is correct |
45 |
Correct |
38 ms |
165720 KB |
Output is correct |
46 |
Correct |
38 ms |
165700 KB |
Output is correct |
47 |
Correct |
37 ms |
165712 KB |
Output is correct |
48 |
Correct |
38 ms |
165620 KB |
Output is correct |
49 |
Correct |
38 ms |
165716 KB |
Output is correct |
50 |
Correct |
38 ms |
165712 KB |
Output is correct |
51 |
Correct |
41 ms |
165728 KB |
Output is correct |
52 |
Correct |
38 ms |
165732 KB |
Output is correct |
53 |
Correct |
40 ms |
165724 KB |
Output is correct |
54 |
Correct |
38 ms |
165716 KB |
Output is correct |
55 |
Correct |
37 ms |
165716 KB |
Output is correct |
56 |
Correct |
38 ms |
165724 KB |
Output is correct |
57 |
Correct |
38 ms |
165692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
165700 KB |
Output is correct |
2 |
Correct |
59 ms |
165716 KB |
Output is correct |
3 |
Correct |
59 ms |
165712 KB |
Output is correct |
4 |
Correct |
58 ms |
165632 KB |
Output is correct |
5 |
Correct |
59 ms |
165732 KB |
Output is correct |
6 |
Correct |
60 ms |
165652 KB |
Output is correct |
7 |
Correct |
58 ms |
165648 KB |
Output is correct |
8 |
Correct |
59 ms |
165708 KB |
Output is correct |
9 |
Correct |
68 ms |
165724 KB |
Output is correct |
10 |
Correct |
41 ms |
165620 KB |
Output is correct |
11 |
Correct |
41 ms |
165684 KB |
Output is correct |
12 |
Correct |
47 ms |
165712 KB |
Output is correct |
13 |
Correct |
44 ms |
165712 KB |
Output is correct |
14 |
Correct |
49 ms |
165656 KB |
Output is correct |
15 |
Correct |
39 ms |
165724 KB |
Output is correct |
16 |
Correct |
39 ms |
165712 KB |
Output is correct |
17 |
Correct |
38 ms |
165712 KB |
Output is correct |
18 |
Correct |
43 ms |
165712 KB |
Output is correct |
19 |
Correct |
44 ms |
165744 KB |
Output is correct |
20 |
Correct |
45 ms |
165672 KB |
Output is correct |
21 |
Correct |
42 ms |
165716 KB |
Output is correct |
22 |
Correct |
46 ms |
165688 KB |
Output is correct |
23 |
Correct |
41 ms |
165716 KB |
Output is correct |
24 |
Correct |
44 ms |
165604 KB |
Output is correct |
25 |
Correct |
45 ms |
165668 KB |
Output is correct |
26 |
Correct |
44 ms |
165816 KB |
Output is correct |
27 |
Correct |
40 ms |
165716 KB |
Output is correct |
28 |
Correct |
40 ms |
165712 KB |
Output is correct |
29 |
Correct |
42 ms |
165712 KB |
Output is correct |
30 |
Correct |
39 ms |
165712 KB |
Output is correct |
31 |
Correct |
42 ms |
165716 KB |
Output is correct |
32 |
Correct |
39 ms |
165656 KB |
Output is correct |
33 |
Correct |
41 ms |
165712 KB |
Output is correct |
34 |
Correct |
38 ms |
165720 KB |
Output is correct |
35 |
Correct |
39 ms |
165716 KB |
Output is correct |
36 |
Correct |
37 ms |
165716 KB |
Output is correct |
37 |
Correct |
37 ms |
165724 KB |
Output is correct |
38 |
Correct |
43 ms |
165708 KB |
Output is correct |
39 |
Correct |
37 ms |
165712 KB |
Output is correct |
40 |
Correct |
39 ms |
165716 KB |
Output is correct |
41 |
Correct |
38 ms |
165716 KB |
Output is correct |
42 |
Correct |
38 ms |
165716 KB |
Output is correct |
43 |
Correct |
38 ms |
165656 KB |
Output is correct |
44 |
Correct |
43 ms |
165720 KB |
Output is correct |
45 |
Correct |
38 ms |
165720 KB |
Output is correct |
46 |
Correct |
38 ms |
165700 KB |
Output is correct |
47 |
Correct |
37 ms |
165712 KB |
Output is correct |
48 |
Correct |
38 ms |
165620 KB |
Output is correct |
49 |
Correct |
38 ms |
165716 KB |
Output is correct |
50 |
Correct |
38 ms |
165712 KB |
Output is correct |
51 |
Correct |
41 ms |
165728 KB |
Output is correct |
52 |
Correct |
38 ms |
165732 KB |
Output is correct |
53 |
Correct |
40 ms |
165724 KB |
Output is correct |
54 |
Correct |
38 ms |
165716 KB |
Output is correct |
55 |
Correct |
37 ms |
165716 KB |
Output is correct |
56 |
Correct |
38 ms |
165724 KB |
Output is correct |
57 |
Correct |
38 ms |
165692 KB |
Output is correct |
58 |
Correct |
51 ms |
165912 KB |
Output is correct |
59 |
Correct |
45 ms |
166192 KB |
Output is correct |
60 |
Correct |
56 ms |
166212 KB |
Output is correct |
61 |
Correct |
232 ms |
166228 KB |
Output is correct |
62 |
Correct |
645 ms |
166684 KB |
Output is correct |
63 |
Correct |
56 ms |
166480 KB |
Output is correct |
64 |
Correct |
334 ms |
166724 KB |
Output is correct |
65 |
Correct |
37 ms |
165716 KB |
Output is correct |
66 |
Correct |
37 ms |
165688 KB |
Output is correct |
67 |
Correct |
40 ms |
165716 KB |
Output is correct |
68 |
Correct |
38 ms |
165712 KB |
Output is correct |
69 |
Correct |
36 ms |
165708 KB |
Output is correct |
70 |
Correct |
39 ms |
165712 KB |
Output is correct |
71 |
Correct |
131 ms |
165904 KB |
Output is correct |
72 |
Correct |
39 ms |
165892 KB |
Output is correct |
73 |
Correct |
62 ms |
165720 KB |
Output is correct |
74 |
Correct |
41 ms |
166224 KB |
Output is correct |
75 |
Correct |
65 ms |
166224 KB |
Output is correct |
76 |
Correct |
367 ms |
166308 KB |
Output is correct |
77 |
Correct |
999 ms |
166684 KB |
Output is correct |
78 |
Correct |
61 ms |
166480 KB |
Output is correct |
79 |
Correct |
475 ms |
166680 KB |
Output is correct |
80 |
Correct |
55 ms |
166664 KB |
Output is correct |
81 |
Correct |
131 ms |
166480 KB |
Output is correct |
82 |
Execution timed out |
1010 ms |
166692 KB |
Time limit exceeded |
83 |
Halted |
0 ms |
0 KB |
- |