//Huyduocdithitp
#include <bits/stdc++.h>
typedef long long ll;
#define endl '\n'
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define fi first
#define se second
#define pii pair<ll, ll>
#define inf 1e18
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MP make_pair
#define TASK "ghuy4g"
#define start if(fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin);freopen(TASK".out","w",stdout);}
#define LOG 19
#define N 500005
using namespace std;
ll n, k;
ll a[N], f[N][101];
signed main(void) {
faster;
cin >> n >> k;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
}
ll ln = 0;
for (int i = 1; i <= n; i ++) {
ln = max(ln, a[i]);
f[i][1] = ln;
}
for (int j = 2; j <= k; j ++) {
stack<pii> st;
st.push({f[j - 1][j - 1], a[j]});
ll mini = st.top().fi + st.top().se;
for (int i = j; i <= n; i ++) {
f[i][j] = mini;
mini = inf;
pii moi = {f[i][j - 1], a[i + 1]};
while (st.size() && st.top().se <= a[i + 1]) {
moi.fi = min(moi.fi, st.top().fi);
st.pop();
}
if (st.size() && st.top().se > a[i + 1]) {
mini = min(mini, st.top().fi + st.top().se);
}
st.push(moi);
mini = min(mini, moi.fi + moi.se);
}
}
cout << f[n][k];
return 0;
}
# | 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... |