#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pll pair<int, int>
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define endl '\n'
#define ld long double
#define sz(x) static_cast<int>((x).size())
signed main(){
int n,k;cin>>n>>k;
vector<int> v(n+1, 0);
for(int i=1;i<=n;i++)cin>>v[i];
vector<vector<int>> mem(k+1, vector<int>(n+1, 1e15));
mem[0][0]=0;
for(int i=1;i<=k;i++){
stack<tuple<int,int,int>> st;
st.push({0, 1e15, mem[i-1][0]});
for(int j=1;j<=n;j++){
int mn=mem[i-1][j];
while(!st.empty() and get<0>(st.top()) <= v[j]){
mn=min(mn, get<2>(st.top()));
st.pop();
}
st.push({v[j],
min((st.empty()?(int)1e15:get<1>(st.top())),mn+v[j]),
mn});
mem[i][j]=get<1>(st.top());
}
}
//~ for(int i=0;i<=k;i++){
//~ for(int j=0;j<=n;j++){
//~ cout<<mem[i][j]<<" ";
//~ }
//~ cout<<endl;
//~ }
cout<<mem[k][n];
}
| # | 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... |