#include<bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using aa = array<int,3>;
const int N = 1e6+5;
const int INF = 1e9;
int dp[100005][105];
int a[100005];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin >> n >> k;
for(int i=1;i<=n;i++) {
cin >> a[i];
dp[i][0]=1e18;
}
for(int j=1;j<=k;j++) {
stack<int> st;
dp[j-1][j]=1e18;
for(int i=j;i<=n;i++) {
while(!st.empty() && a[i]>a[st.top()]) {
st.pop();
}
int pos;
if(st.empty()) {
pos=j-1;
dp[i][j]=dp[pos][j-1]+a[i];
}
else {
pos=st.top();
dp[i][j]=min(dp[pos][j-1]+a[i],dp[pos][j]);
}
st.push(i);
//cout << pos << ' ';
}
//cout << endl;
}
cout << dp[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... |