/*
Link: https://oj.uz/problem/submit/IZhO14_blocks
*/
#include <bits/stdc++.h>
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define __builtin_ctz __builtin_ctzll
#define For(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define Fod(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
//#define int long long
#define ll long long
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define whole(a) a.begin(), a.end()
#define vi vector<int>
#define ii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define gqt ""
template<class X, class Y>
bool minimize(X& x, const Y& y) {
X eps = 1e-9;
if (x > y + eps) {
x = y;
return true;
}
else return false;
}
template<class X, class Y>
bool maximize(X& x, const Y& y) {
X eps = 1e-9;
if (x + eps < y) {
x = y;
return true;
}
else return false;
}
template<class T>
T Abs(const T& x) {
return (x < 0 ? -x : x);
}
const int INF = 1e9 + 7;
const ll oo = 1e18 + 7;
const int MAX = 1000005;
const int MOD = 1e9 + 7;
using namespace std;
int n, k, a[MAX], dp[105][100005];
void process(){
cin >> n >> k;
For(i, 1, n) cin >> a[i];
memset(dp, 0x3f, sizeof(dp));
dp[1][0] = 0;
For(i, 1, n) dp[1][i] = max(dp[1][i - 1], a[i]);
For(i, 2, k){
stack<ii> st;
For(j, i, n){
int minF = dp[i - 1][j - 1];
while(!st.empty() && a[st.top().se] <= a[j]){
minimize(minF, st.top().fi);
st.pop();
}
dp[i][j] = min(dp[i][st.empty() ? 0 : st.top().se], minF + a[j]);
st.push(ii(minF, j));
}
}
cout << dp[k][n];
}
signed main()
{
fastio
if (fopen(gqt".inp", "r")) {
freopen(gqt".inp", "r", stdin);
freopen(gqt".out", "w", stdout);
}
int Test = 1;
while (Test--) {
process();
}
return 0;
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(gqt".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | freopen(gqt".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
41308 KB |
Output is correct |
2 |
Correct |
16 ms |
41472 KB |
Output is correct |
3 |
Correct |
15 ms |
41564 KB |
Output is correct |
4 |
Correct |
15 ms |
41320 KB |
Output is correct |
5 |
Correct |
15 ms |
41528 KB |
Output is correct |
6 |
Correct |
15 ms |
41432 KB |
Output is correct |
7 |
Correct |
15 ms |
41308 KB |
Output is correct |
8 |
Correct |
21 ms |
41308 KB |
Output is correct |
9 |
Correct |
15 ms |
41336 KB |
Output is correct |
10 |
Correct |
15 ms |
41512 KB |
Output is correct |
11 |
Correct |
15 ms |
41564 KB |
Output is correct |
12 |
Correct |
20 ms |
41476 KB |
Output is correct |
13 |
Correct |
15 ms |
41564 KB |
Output is correct |
14 |
Correct |
16 ms |
41552 KB |
Output is correct |
15 |
Correct |
15 ms |
41308 KB |
Output is correct |
16 |
Correct |
18 ms |
41308 KB |
Output is correct |
17 |
Correct |
17 ms |
41560 KB |
Output is correct |
18 |
Correct |
16 ms |
41308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
41564 KB |
Output is correct |
2 |
Correct |
15 ms |
41308 KB |
Output is correct |
3 |
Correct |
15 ms |
41476 KB |
Output is correct |
4 |
Correct |
16 ms |
41308 KB |
Output is correct |
5 |
Correct |
15 ms |
41564 KB |
Output is correct |
6 |
Correct |
15 ms |
41384 KB |
Output is correct |
7 |
Correct |
17 ms |
41304 KB |
Output is correct |
8 |
Correct |
15 ms |
41560 KB |
Output is correct |
9 |
Correct |
15 ms |
41308 KB |
Output is correct |
10 |
Correct |
18 ms |
41304 KB |
Output is correct |
11 |
Correct |
18 ms |
41456 KB |
Output is correct |
12 |
Correct |
17 ms |
41520 KB |
Output is correct |
13 |
Correct |
17 ms |
41304 KB |
Output is correct |
14 |
Correct |
17 ms |
41564 KB |
Output is correct |
15 |
Correct |
17 ms |
41304 KB |
Output is correct |
16 |
Correct |
18 ms |
41560 KB |
Output is correct |
17 |
Correct |
19 ms |
41308 KB |
Output is correct |
18 |
Correct |
17 ms |
41312 KB |
Output is correct |
19 |
Correct |
19 ms |
41308 KB |
Output is correct |
20 |
Correct |
17 ms |
41308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
41308 KB |
Output is correct |
2 |
Correct |
16 ms |
41472 KB |
Output is correct |
3 |
Correct |
15 ms |
41564 KB |
Output is correct |
4 |
Correct |
15 ms |
41320 KB |
Output is correct |
5 |
Correct |
15 ms |
41528 KB |
Output is correct |
6 |
Correct |
15 ms |
41432 KB |
Output is correct |
7 |
Correct |
15 ms |
41308 KB |
Output is correct |
8 |
Correct |
21 ms |
41308 KB |
Output is correct |
9 |
Correct |
15 ms |
41336 KB |
Output is correct |
10 |
Correct |
15 ms |
41512 KB |
Output is correct |
11 |
Correct |
15 ms |
41564 KB |
Output is correct |
12 |
Correct |
20 ms |
41476 KB |
Output is correct |
13 |
Correct |
15 ms |
41564 KB |
Output is correct |
14 |
Correct |
16 ms |
41552 KB |
Output is correct |
15 |
Correct |
15 ms |
41308 KB |
Output is correct |
16 |
Correct |
18 ms |
41308 KB |
Output is correct |
17 |
Correct |
17 ms |
41560 KB |
Output is correct |
18 |
Correct |
16 ms |
41308 KB |
Output is correct |
19 |
Correct |
15 ms |
41564 KB |
Output is correct |
20 |
Correct |
15 ms |
41308 KB |
Output is correct |
21 |
Correct |
15 ms |
41476 KB |
Output is correct |
22 |
Correct |
16 ms |
41308 KB |
Output is correct |
23 |
Correct |
15 ms |
41564 KB |
Output is correct |
24 |
Correct |
15 ms |
41384 KB |
Output is correct |
25 |
Correct |
17 ms |
41304 KB |
Output is correct |
26 |
Correct |
15 ms |
41560 KB |
Output is correct |
27 |
Correct |
15 ms |
41308 KB |
Output is correct |
28 |
Correct |
18 ms |
41304 KB |
Output is correct |
29 |
Correct |
18 ms |
41456 KB |
Output is correct |
30 |
Correct |
17 ms |
41520 KB |
Output is correct |
31 |
Correct |
17 ms |
41304 KB |
Output is correct |
32 |
Correct |
17 ms |
41564 KB |
Output is correct |
33 |
Correct |
17 ms |
41304 KB |
Output is correct |
34 |
Correct |
18 ms |
41560 KB |
Output is correct |
35 |
Correct |
19 ms |
41308 KB |
Output is correct |
36 |
Correct |
17 ms |
41312 KB |
Output is correct |
37 |
Correct |
19 ms |
41308 KB |
Output is correct |
38 |
Correct |
17 ms |
41308 KB |
Output is correct |
39 |
Correct |
16 ms |
41560 KB |
Output is correct |
40 |
Correct |
18 ms |
41384 KB |
Output is correct |
41 |
Correct |
16 ms |
41308 KB |
Output is correct |
42 |
Correct |
16 ms |
41312 KB |
Output is correct |
43 |
Correct |
18 ms |
41296 KB |
Output is correct |
44 |
Correct |
18 ms |
41296 KB |
Output is correct |
45 |
Correct |
17 ms |
41308 KB |
Output is correct |
46 |
Correct |
18 ms |
41420 KB |
Output is correct |
47 |
Correct |
17 ms |
41308 KB |
Output is correct |
48 |
Correct |
18 ms |
41364 KB |
Output is correct |
49 |
Correct |
17 ms |
41336 KB |
Output is correct |
50 |
Correct |
17 ms |
41560 KB |
Output is correct |
51 |
Correct |
18 ms |
41308 KB |
Output is correct |
52 |
Correct |
18 ms |
41556 KB |
Output is correct |
53 |
Correct |
17 ms |
41304 KB |
Output is correct |
54 |
Correct |
16 ms |
41308 KB |
Output is correct |
55 |
Correct |
19 ms |
41416 KB |
Output is correct |
56 |
Correct |
17 ms |
41564 KB |
Output is correct |
57 |
Correct |
18 ms |
41308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
41308 KB |
Output is correct |
2 |
Correct |
16 ms |
41472 KB |
Output is correct |
3 |
Correct |
15 ms |
41564 KB |
Output is correct |
4 |
Correct |
15 ms |
41320 KB |
Output is correct |
5 |
Correct |
15 ms |
41528 KB |
Output is correct |
6 |
Correct |
15 ms |
41432 KB |
Output is correct |
7 |
Correct |
15 ms |
41308 KB |
Output is correct |
8 |
Correct |
21 ms |
41308 KB |
Output is correct |
9 |
Correct |
15 ms |
41336 KB |
Output is correct |
10 |
Correct |
15 ms |
41512 KB |
Output is correct |
11 |
Correct |
15 ms |
41564 KB |
Output is correct |
12 |
Correct |
20 ms |
41476 KB |
Output is correct |
13 |
Correct |
15 ms |
41564 KB |
Output is correct |
14 |
Correct |
16 ms |
41552 KB |
Output is correct |
15 |
Correct |
15 ms |
41308 KB |
Output is correct |
16 |
Correct |
18 ms |
41308 KB |
Output is correct |
17 |
Correct |
17 ms |
41560 KB |
Output is correct |
18 |
Correct |
16 ms |
41308 KB |
Output is correct |
19 |
Correct |
15 ms |
41564 KB |
Output is correct |
20 |
Correct |
15 ms |
41308 KB |
Output is correct |
21 |
Correct |
15 ms |
41476 KB |
Output is correct |
22 |
Correct |
16 ms |
41308 KB |
Output is correct |
23 |
Correct |
15 ms |
41564 KB |
Output is correct |
24 |
Correct |
15 ms |
41384 KB |
Output is correct |
25 |
Correct |
17 ms |
41304 KB |
Output is correct |
26 |
Correct |
15 ms |
41560 KB |
Output is correct |
27 |
Correct |
15 ms |
41308 KB |
Output is correct |
28 |
Correct |
18 ms |
41304 KB |
Output is correct |
29 |
Correct |
18 ms |
41456 KB |
Output is correct |
30 |
Correct |
17 ms |
41520 KB |
Output is correct |
31 |
Correct |
17 ms |
41304 KB |
Output is correct |
32 |
Correct |
17 ms |
41564 KB |
Output is correct |
33 |
Correct |
17 ms |
41304 KB |
Output is correct |
34 |
Correct |
18 ms |
41560 KB |
Output is correct |
35 |
Correct |
19 ms |
41308 KB |
Output is correct |
36 |
Correct |
17 ms |
41312 KB |
Output is correct |
37 |
Correct |
19 ms |
41308 KB |
Output is correct |
38 |
Correct |
17 ms |
41308 KB |
Output is correct |
39 |
Correct |
16 ms |
41560 KB |
Output is correct |
40 |
Correct |
18 ms |
41384 KB |
Output is correct |
41 |
Correct |
16 ms |
41308 KB |
Output is correct |
42 |
Correct |
16 ms |
41312 KB |
Output is correct |
43 |
Correct |
18 ms |
41296 KB |
Output is correct |
44 |
Correct |
18 ms |
41296 KB |
Output is correct |
45 |
Correct |
17 ms |
41308 KB |
Output is correct |
46 |
Correct |
18 ms |
41420 KB |
Output is correct |
47 |
Correct |
17 ms |
41308 KB |
Output is correct |
48 |
Correct |
18 ms |
41364 KB |
Output is correct |
49 |
Correct |
17 ms |
41336 KB |
Output is correct |
50 |
Correct |
17 ms |
41560 KB |
Output is correct |
51 |
Correct |
18 ms |
41308 KB |
Output is correct |
52 |
Correct |
18 ms |
41556 KB |
Output is correct |
53 |
Correct |
17 ms |
41304 KB |
Output is correct |
54 |
Correct |
16 ms |
41308 KB |
Output is correct |
55 |
Correct |
19 ms |
41416 KB |
Output is correct |
56 |
Correct |
17 ms |
41564 KB |
Output is correct |
57 |
Correct |
18 ms |
41308 KB |
Output is correct |
58 |
Correct |
21 ms |
41564 KB |
Output is correct |
59 |
Correct |
20 ms |
42000 KB |
Output is correct |
60 |
Correct |
23 ms |
41820 KB |
Output is correct |
61 |
Correct |
50 ms |
41968 KB |
Output is correct |
62 |
Correct |
105 ms |
42588 KB |
Output is correct |
63 |
Correct |
25 ms |
42588 KB |
Output is correct |
64 |
Correct |
63 ms |
42576 KB |
Output is correct |
65 |
Correct |
18 ms |
41344 KB |
Output is correct |
66 |
Correct |
17 ms |
41564 KB |
Output is correct |
67 |
Correct |
16 ms |
41564 KB |
Output is correct |
68 |
Correct |
16 ms |
41308 KB |
Output is correct |
69 |
Correct |
17 ms |
41564 KB |
Output is correct |
70 |
Correct |
15 ms |
41532 KB |
Output is correct |
71 |
Correct |
28 ms |
41444 KB |
Output is correct |
72 |
Correct |
16 ms |
41516 KB |
Output is correct |
73 |
Correct |
19 ms |
41564 KB |
Output is correct |
74 |
Correct |
22 ms |
41820 KB |
Output is correct |
75 |
Correct |
23 ms |
41820 KB |
Output is correct |
76 |
Correct |
57 ms |
42032 KB |
Output is correct |
77 |
Correct |
146 ms |
42468 KB |
Output is correct |
78 |
Correct |
27 ms |
42580 KB |
Output is correct |
79 |
Correct |
72 ms |
42588 KB |
Output is correct |
80 |
Correct |
23 ms |
42588 KB |
Output is correct |
81 |
Correct |
32 ms |
42580 KB |
Output is correct |
82 |
Correct |
135 ms |
42616 KB |
Output is correct |
83 |
Correct |
17 ms |
41560 KB |
Output is correct |
84 |
Correct |
16 ms |
41564 KB |
Output is correct |
85 |
Correct |
30 ms |
41564 KB |
Output is correct |
86 |
Correct |
17 ms |
41564 KB |
Output is correct |
87 |
Correct |
23 ms |
41688 KB |
Output is correct |
88 |
Correct |
19 ms |
41916 KB |
Output is correct |
89 |
Correct |
22 ms |
41956 KB |
Output is correct |
90 |
Correct |
51 ms |
42148 KB |
Output is correct |
91 |
Correct |
122 ms |
42900 KB |
Output is correct |
92 |
Correct |
25 ms |
42844 KB |
Output is correct |
93 |
Correct |
69 ms |
42904 KB |
Output is correct |
94 |
Correct |
19 ms |
41560 KB |
Output is correct |
95 |
Correct |
23 ms |
41564 KB |
Output is correct |
96 |
Correct |
15 ms |
41568 KB |
Output is correct |
97 |
Correct |
15 ms |
41468 KB |
Output is correct |
98 |
Correct |
19 ms |
41560 KB |
Output is correct |
99 |
Correct |
16 ms |
41304 KB |
Output is correct |