#include<bits/stdc++.h>
using namespace std;
#define ll long long
#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 pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
#define t_test int t;cin >> t;while(t--)
const ll mod = 1e9 + 7;
const ll INF = 1e3;
inline void adm(ll &x){if(x>=mod)x%=mod;else if(x<0)x+=mod;}
//--------------------------------------------------------------------
const ll N = 1e5 + 10;
const ll K = 102;
ll a[N];
ll f[N], g[N], L[N];
void hbmt() {
ll n, k;
cin >> n >> k;
FOR(i ,1, n) {
cin >> a[i];
}
stack<ll> st;
FOR(i, 1, n) {
while(!st.empty() && a[st.top()] <= a[i]) st.pop();
if(st.empty()) L[i] = 1;
else L[i] = st.top() + 1;
st.push(i);
}
FOR(i, 0, n) f[i] = g[i] = INF;
g[1] = a[1];
FOR(i, 2, n) g[i] = max(g[i - 1], a[i]);
FOR(j, 2, k) {
stack<pa> st;
FOR(i, 1, n) {
f[i] = f[L[i] - 1];
ll mn = g[i - 1];
while(!st.empty() && st.top().se <= a[i]) {
mn = min(mn, st.top().fi);
st.pop();
}
f[i] = min(f[i], mn + a[i]);
st.push({mn, a[i]});
}
FOR(i, 0, n) {
g[i] = f[i];
f[i] = INF;
}
}
cout << g[n];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen("hbmt.inp", "r")) {
freopen("hbmt.inp", "r", stdin);
freopen("hbmt.out", "w", stdout);
}
// t_test
hbmt();
return 0;
}
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("hbmt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen("hbmt.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |