#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ldb long double
#define in(x) freopen(x,"r",stdin)
#define out(x) freopen(x,"w",stdout)
#define fo(i,l,r) for(int i=(l);i<=(r);i++)
#define foi(i,l,r) for(int i=(l);i>=(r);i--)
#define el cout<<'\n';
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define gb(x,i) (((x)>>(i))&1)
#define mask(i) (1LL<<(i))
using namespace std;
const int N=2e5+5;
const int bl=450;
const ll base=1e9+7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll l,ll r)
{
return l+(1ll*rng()*rng()%(r-l+1)+(r-l+1))%(r-l+1);
}
template<class X,class Y>bool maximize(X &a,Y b)
{
if(a<b) return a=b,true;
return false;
}
template<class X,class Y>bool minimize(X &a, Y b)
{
if(a>b) return a=b,true;
return false;
}
void add(ll &a,ll b)
{
a+=b;
if(a>=base) a-=base;
if(a<0) a+=base;
}
int n,k,h[N],now,vt;
stack<int>ss;
ll dp[N],g[N],st[4*N],del[4*N];
void push(int id)
{
fo(i,id<<1,id<<1|1) st[i]+=del[id],del[i]+=del[id];
del[id]=0;
}
void update(int id,int l,int r,int u,int v,int val)
{
if(l>v||r<u) return;
if(u<=l&&r<=v)
{
st[id]+=val,del[id]+=val;
return;
}
push(id);
int mid=(l+r)>>1;
update(id<<1,l,mid,u,v,val),update(id<<1|1,mid+1,r,u,v,val);
st[id]=min(st[id<<1],st[id<<1|1]);
}
ll get(int id,int l,int r,int u,int v)
{
if(l>v||r<u) return 1e18;
if(u<=l&&r<=v) return st[id];
int mid=(l+r)>>1;
push(id);
return min(get(id<<1,l,mid,u,v),get(id<<1|1,mid+1,r,u,v));
}
void build(int id,int l,int r)
{
if(l>r) return;
if(l==r)
{
st[id]=del[id]=0;
return;
}
int mid=(l+r)>>1;
build(id<<1,l,mid),build(id<<1|1,mid+1,r);
}
int main()
{
//in("o.inp");
//out("o.out");
srand(time(NULL));
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
fo(i,1,n) cin>>h[i];
fo(i,1,n) dp[i]=max(dp[i-1],h[i]);
fo(x,2,k)
{
build(1,1,n);
while(ss.size()) ss.pop();
fo(i,1,n) g[i]=1e18;
fo(i,x,n)
{
update(1,1,n,i,i,dp[i-1]+h[i]);
now=i-1;
while(ss.size()&&h[ss.top()]<=h[i])
{
ss.pop();
if(ss.size()==0) vt=x;
else vt=ss.top()+1;
update(1,1,n,vt,now,h[now]-h[vt]);
now=vt-1;
}
ss.push(i),g[i]=get(1,1,n,x,i);
}
fo(i,1,n) dp[i]=g[i];
}
cout<<dp[n];
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:91:37: error: no matching function for call to 'max(long long int&, int&)'
91 | fo(i,1,n) dp[i]=max(dp[i-1],h[i]);
| ^
In file included from /usr/include/c++/10/bits/char_traits.h:39,
from /usr/include/c++/10/ios:40,
from /usr/include/c++/10/istream:38,
from /usr/include/c++/10/sstream:38,
from /usr/include/c++/10/complex:45,
from /usr/include/c++/10/ccomplex:39,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
from blocks.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
254 | max(const _Tp& __a, const _Tp& __b)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: template argument deduction/substitution failed:
blocks.cpp:91:37: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
91 | fo(i,1,n) dp[i]=max(dp[i-1],h[i]);
| ^
In file included from /usr/include/c++/10/bits/char_traits.h:39,
from /usr/include/c++/10/ios:40,
from /usr/include/c++/10/istream:38,
from /usr/include/c++/10/sstream:38,
from /usr/include/c++/10/complex:45,
from /usr/include/c++/10/ccomplex:39,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
from blocks.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
300 | max(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: template argument deduction/substitution failed:
blocks.cpp:91:37: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
91 | fo(i,1,n) dp[i]=max(dp[i-1],h[i]);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from blocks.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
3480 | max(initializer_list<_Tp> __l)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: template argument deduction/substitution failed:
blocks.cpp:91:37: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
91 | fo(i,1,n) dp[i]=max(dp[i-1],h[i]);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from blocks.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
3486 | max(initializer_list<_Tp> __l, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: template argument deduction/substitution failed:
blocks.cpp:91:37: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
91 | fo(i,1,n) dp[i]=max(dp[i-1],h[i]);
| ^