#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a; --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define lb long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define cd complex<double>
const double PI = acos(-1);
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<pii, null_type,less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// - insert(x),erase(x)
// - find_by_order(k): return iterator to the k-th smallest element
// - order_of_key(x): the number of elements that are strictly smaller
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} else return false;
}
template<class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
const int mod = 1e9 + 7;
const int inf = 1e9;
const int N = 1e5 + 5;
const int K = 105;
int n, k, a[N];
int dp[N], dp_before[N];
void process() {
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
dp[0] = 0;
FOR(i, 1, n) dp[i] = max(dp[i - 1], a[i]);
FOR(j, 2, k) {
swap(dp_before, dp);
memset(dp, 0x3f, sizeof(dp));
stack<pii> st;
st.emplace(0, 0);
a[0] = inf;
FOR(i, j, n) {
int res = dp_before[i - 1];
while(a[st.top().fi] <= a[i]) {
minimize(res, st.top().se);
st.pop();
}
dp[i] = min(dp[st.top().fi], res + a[i]);
st.emplace(i, res);
}
}
cout << dp[n] << '\n';
}
signed main() {
if(fopen("test.inp","r")) {
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// clock_t start = clock()
int ntests = 1;
// cin >> ntests;
while(ntests--) process();
// cerr << "Time elapsed: " << clock() - start << " ms!\n";
return 0;
}
// https://oj.uz/problem/view/IZhO14_blocks
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | freopen("test.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
8 |
Correct |
1 ms |
1112 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1368 KB |
Output is correct |
11 |
Correct |
2 ms |
1112 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
2 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
2 ms |
1236 KB |
Output is correct |
20 |
Correct |
2 ms |
1248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
8 |
Correct |
1 ms |
1112 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1368 KB |
Output is correct |
11 |
Correct |
2 ms |
1112 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
1 ms |
1116 KB |
Output is correct |
34 |
Correct |
2 ms |
1116 KB |
Output is correct |
35 |
Correct |
2 ms |
1236 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
2 ms |
1236 KB |
Output is correct |
38 |
Correct |
2 ms |
1248 KB |
Output is correct |
39 |
Correct |
4 ms |
1116 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
1116 KB |
Output is correct |
44 |
Correct |
1 ms |
1232 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1116 KB |
Output is correct |
48 |
Correct |
1 ms |
1116 KB |
Output is correct |
49 |
Correct |
1 ms |
1184 KB |
Output is correct |
50 |
Correct |
1 ms |
1240 KB |
Output is correct |
51 |
Correct |
2 ms |
1112 KB |
Output is correct |
52 |
Correct |
2 ms |
1244 KB |
Output is correct |
53 |
Correct |
9 ms |
1248 KB |
Output is correct |
54 |
Correct |
10 ms |
1116 KB |
Output is correct |
55 |
Correct |
5 ms |
1372 KB |
Output is correct |
56 |
Correct |
8 ms |
1112 KB |
Output is correct |
57 |
Correct |
8 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
8 |
Correct |
1 ms |
1112 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1368 KB |
Output is correct |
11 |
Correct |
2 ms |
1112 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
1112 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
1116 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
2 ms |
1116 KB |
Output is correct |
32 |
Correct |
2 ms |
1116 KB |
Output is correct |
33 |
Correct |
1 ms |
1116 KB |
Output is correct |
34 |
Correct |
2 ms |
1116 KB |
Output is correct |
35 |
Correct |
2 ms |
1236 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
2 ms |
1236 KB |
Output is correct |
38 |
Correct |
2 ms |
1248 KB |
Output is correct |
39 |
Correct |
4 ms |
1116 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
1116 KB |
Output is correct |
44 |
Correct |
1 ms |
1232 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1116 KB |
Output is correct |
48 |
Correct |
1 ms |
1116 KB |
Output is correct |
49 |
Correct |
1 ms |
1184 KB |
Output is correct |
50 |
Correct |
1 ms |
1240 KB |
Output is correct |
51 |
Correct |
2 ms |
1112 KB |
Output is correct |
52 |
Correct |
2 ms |
1244 KB |
Output is correct |
53 |
Correct |
9 ms |
1248 KB |
Output is correct |
54 |
Correct |
10 ms |
1116 KB |
Output is correct |
55 |
Correct |
5 ms |
1372 KB |
Output is correct |
56 |
Correct |
8 ms |
1112 KB |
Output is correct |
57 |
Correct |
8 ms |
1116 KB |
Output is correct |
58 |
Correct |
5 ms |
1112 KB |
Output is correct |
59 |
Correct |
3 ms |
1092 KB |
Output is correct |
60 |
Correct |
6 ms |
1628 KB |
Output is correct |
61 |
Correct |
43 ms |
1708 KB |
Output is correct |
62 |
Correct |
94 ms |
2304 KB |
Output is correct |
63 |
Correct |
9 ms |
2140 KB |
Output is correct |
64 |
Correct |
62 ms |
2280 KB |
Output is correct |
65 |
Correct |
3 ms |
1116 KB |
Output is correct |
66 |
Correct |
5 ms |
1116 KB |
Output is correct |
67 |
Correct |
6 ms |
1116 KB |
Output is correct |
68 |
Correct |
4 ms |
1248 KB |
Output is correct |
69 |
Correct |
0 ms |
604 KB |
Output is correct |
70 |
Correct |
2 ms |
1116 KB |
Output is correct |
71 |
Correct |
13 ms |
1356 KB |
Output is correct |
72 |
Correct |
2 ms |
1116 KB |
Output is correct |
73 |
Correct |
5 ms |
1116 KB |
Output is correct |
74 |
Correct |
3 ms |
1112 KB |
Output is correct |
75 |
Correct |
6 ms |
1628 KB |
Output is correct |
76 |
Correct |
42 ms |
1628 KB |
Output is correct |
77 |
Correct |
113 ms |
2136 KB |
Output is correct |
78 |
Correct |
8 ms |
2136 KB |
Output is correct |
79 |
Correct |
56 ms |
2136 KB |
Output is correct |
80 |
Correct |
6 ms |
1880 KB |
Output is correct |
81 |
Correct |
23 ms |
2280 KB |
Output is correct |
82 |
Correct |
117 ms |
2140 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
1116 KB |
Output is correct |
85 |
Correct |
15 ms |
1388 KB |
Output is correct |
86 |
Correct |
1 ms |
1368 KB |
Output is correct |
87 |
Correct |
5 ms |
1372 KB |
Output is correct |
88 |
Correct |
3 ms |
1116 KB |
Output is correct |
89 |
Correct |
6 ms |
1620 KB |
Output is correct |
90 |
Correct |
37 ms |
1612 KB |
Output is correct |
91 |
Correct |
104 ms |
2868 KB |
Output is correct |
92 |
Correct |
8 ms |
2496 KB |
Output is correct |
93 |
Correct |
50 ms |
2608 KB |
Output is correct |
94 |
Correct |
2 ms |
1116 KB |
Output is correct |
95 |
Correct |
13 ms |
1116 KB |
Output is correct |
96 |
Correct |
2 ms |
1116 KB |
Output is correct |
97 |
Correct |
0 ms |
472 KB |
Output is correct |
98 |
Correct |
1 ms |
1116 KB |
Output is correct |
99 |
Correct |
1 ms |
1116 KB |
Output is correct |