This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define mask(k) (1<<(k))
using namespace std;
int n,k,a[100001],dp[100001][101],l[100001],mx[100001][21];
stack<int> st;
int getmn(int l, int r)
{
int p=__lg(r-l+1);
return min(mx[l][p],mx[r-mask(p)+1][p]);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("kblocks.inp","r",stdin);
//freopen("kblocks.out","w",stdout);
cin>>n>>k;
for (int i=1; i<=n; i++) cin>>a[i];
for (int i=1; i<=n; i++)
{
while (!st.empty() && a[st.top()]<=a[i]) st.pop();
if (!st.empty()) l[i]=st.top(); else l[i]=0;
st.push(i);
}
memset(dp,0x3f,sizeof(dp));
dp[0][0]=0;
for (int j=1; j<=k; j++)
{
for (int i=0; i<=n; i++) mx[i][0]=dp[i][j-1];
for (int j=1; mask(j)<=n+1; j++)
for (int i=0; i+mask(j)-1<=n; i++)
mx[i][j]=min(mx[i][j-1],mx[i+mask(j-1)][j-1]);
for (int i=1; i<=n; i++)
{
dp[i][j]=min(dp[i][j],getmn(l[i],i)+a[i]);
dp[i][j]=min(dp[i][j],dp[l[i]][j]);
}
}
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... |