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 ll long long
#define el '\n'
#define rep(i,a,b) for (int i=a;i<=b;++i)
using namespace std;
ll n,k,a[100001],dp[101][100001],ma[22][100001];
ll get(ll l, ll r)
{
ll k=log2(r-l+1);
return max(ma[k][l],ma[k][r-(1<<k)+1]);
}
void sol(ll g, ll l, ll r, ll otpl, ll otpr)
{
if (l>r) return;
ll mid=l+r>>1;
ll pos=0;
dp[g][mid]=1e18;
for (ll i=otpl;i<=min(mid,otpr);i++)
{
ll cost=dp[g-1][i-1]+get(i,mid);
if (cost<dp[g][mid])
{
pos=i;
dp[g][mid]=cost;
}
}
sol(g,l,mid-1,l,pos);
sol(g,mid+1,r,pos,r);
}
void solve()
{
cin>>n>>k;
rep(i,1,n)
{
cin>>a[i];
dp[1][i]=max(dp[1][i-1],a[i]);
ma[0][i]=a[i];
}
for (int i=1;i<=log2(n);i++)
{
for (int j=1;j<=(n-(1<<i)+1);j++) ma[i][j]=max(ma[i-1][j],ma[i-1][j+(1<<(i-1))]);
}
rep(i,2,k) sol(i,1,n,1,n);
cout<<dp[k][n];
}
int main()
{
solve();
return 0;
}
Compilation message (stderr)
blocks.cpp: In function 'void sol(long long int, long long int, long long int, long long int, long long int)':
blocks.cpp:15:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | ll mid=l+r>>1;
| ~^~
# | 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... |