This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*huypheu
6 5
5 4 3 2 1 4
*/
#include <bits/stdc++.h>
#define int long long
#define oo (LLONG_MAX/2)
using namespace std;
bool pok[100007];
int a[100007],f[100007][107],mask[100007][107];
signed main()
{
int n,k;
cin >> n >> k;
for(int i=1;i<=n;i++)
{
cin >> a[i];
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=k;j++)
{
f[i][j]=oo;
mask[i][j]=oo;
}
}
f[0][0]=0;
stack <int> st;
for(int i=n;i>=1;i--)
{
while(!st.empty() && a[st.top()]<=a[i]) st.pop();
if(st.empty()) pok[i]=1;
st.push(i);
}
for(int j=1;j<=k;j++)
{
while(!st.empty()) st.pop();
for(int i=1+j-1;i<=n;i++)
{
while(!st.empty() && a[st.top()]<=a[i])
{
mask[i][j]=min(mask[i][j],mask[st.top()][j]);
st.pop();
}
if(st.empty()) mask[i][j]=min(mask[i][j],f[1+j-2][j-1]); else mask[i][j]=min(mask[i][j],f[st.top()][j-1]);
f[i][j]=min(f[i][j],mask[i][j]+a[i]);
if(!st.empty()) f[i][j]=min(f[i][j],f[st.top()][j]);
mask[i][j]=min(mask[i][j],f[i][j-1]);
st.push(i);
}
}
int mi=oo;
// for(int j=0;j<=k;j++)
// {
// for(int i=1;i<=n;i++)
// {
// cout << mask[i][j] /*<< "/" << f[i][j]*/ << " ";
// }
// cout << endl;
// }
// for(int j=0;j<=k;j++)
// {
// for(int i=1;i<=n;i++)
// {
// cout << f[i][j] /*<< "/" << f[i][j]*/ << " ";
// }
// cout << endl;
// }
for(int i=1;i<=n;i++)
{
if(pok[i]) mi=min(mi,f[i][k]);
}
cout << mi << endl;
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... |