#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],dp[N],g[N],st[4*N],del[4*N],now,vt;
stack<int>ss;
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]);
}
int get(int id,int l,int r,int u,int v)
{
if(l>v||r<u) return 1e9;
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]=1e9;
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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |