#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 = 1e6 + 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().se], 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 |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
5 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8792 KB |
Output is correct |
10 |
Correct |
4 ms |
8796 KB |
Output is correct |
11 |
Correct |
6 ms |
8648 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Runtime error |
12 ms |
17496 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
4 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
8796 KB |
Output is correct |
9 |
Correct |
5 ms |
8792 KB |
Output is correct |
10 |
Correct |
6 ms |
8792 KB |
Output is correct |
11 |
Correct |
7 ms |
8792 KB |
Output is correct |
12 |
Correct |
7 ms |
8796 KB |
Output is correct |
13 |
Correct |
7 ms |
8844 KB |
Output is correct |
14 |
Runtime error |
16 ms |
17560 KB |
Execution killed with signal 11 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
5 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8792 KB |
Output is correct |
10 |
Correct |
4 ms |
8796 KB |
Output is correct |
11 |
Correct |
6 ms |
8648 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Runtime error |
12 ms |
17496 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
5 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
8796 KB |
Output is correct |
9 |
Correct |
3 ms |
8792 KB |
Output is correct |
10 |
Correct |
4 ms |
8796 KB |
Output is correct |
11 |
Correct |
6 ms |
8648 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Runtime error |
12 ms |
17496 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |