#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define eb emplace_back
#define ll long long
#define fi first
#define se second
int t,n,i,k,j;
const int maxn = 1e6 + 10,maxk = 110;
int a[maxn],dp[maxn][maxk];
stack<pair<int,int>> st[maxk];
multiset<int> s[maxk];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(i = 1;i<=k;i++)
{
s[i].insert(1e9+1);
dp[0][i] = 1e9;
st[i].push({1e9,0});
}
s[0].insert(0);
for(i = 1;i<=n;i++)
{
dp[i][0] = 1e9;
cin>>a[i];
for(j = 1;j<=k;j++)
{
if(j <= i)
{
while(st[j].top().fi <= a[i] && st[j].top().se >= j)
{
auto it = s[j].find(dp[st[j].top().se][j]);
if(it != s[j].end())s[j].erase(it);
st[j].pop();
}
dp[i][j] = min( dp[ st[j].top().se ][ j-1 ] + a[i] , *s[j].begin() );
s[j].insert(dp[i][j]);
}
else dp[i][j] = 1e9;
st[j].push({a[i],i});
}
}
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... |