#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
using namespace std;
const int inf=1e9;
const int mxn=1e5+9;
int tree[4*mxn]={};
int pnt;
void query(int node,int b,int e,int v)
{
if(b==e)
{
pnt=min(pnt,b);
return;
}
int mid=b+e>>1;
int left=node<<1;
int right=left|1;
if(tree[right]<=v)
{
pnt=min(pnt,mid+1);
query(left,b,mid,v);
}
else
{
query(right,mid+1,e,v);
}
}
void update(int node, int b,int e,int p,int v)
{
if(b>p||e<p)return;
if(b==e)
{
tree[node]=v;
return;
}
int m=b+e>>1;
int l=node<<1;
int r=l|1;
update(l,b,m,p,v);
update(r,m+1,e,p,v);
tree[node]=max(tree[l],tree[r]);
}
int main()
{
int n,k;
cin>>n>>k;
int a[n+1];
for(int i=1;i<=n;i++)cin>>a[i];
int dp[n+1][k+1];
dp[1][1]=a[1];
int mx[n+1][k+1];
mx[1][1]=a[1];
for(int i=2;i<=k;i++)dp[1][i]=inf,mx[1][i]=0;
for(int i=2;i<=n;i++)
{
dp[i][1]=max(dp[i-1][1],a[i]);
mx[i][1]=dp[i][1];
update(1,1,mxn,i,a[i]);
pnt=i;
query(1,1,mxn,a[i]);
for(int j=2;j<=k;j++)
{
int l=max(pnt-1,j-1);
int v1=dp[i-1][l]+a[i];
int v2=dp[i-1][j]-mx[i-1][j]+max(mx[i-1][j],a[i]);
if(v1<v2)
{
dp[i][j]=v1;
mx[i][j]=a[i];
}
else
{
dp[i][j]=v2;
mx[i][j]=max(mx[i-1][j],a[i]);
}
}
}
cout<<dp[n][k]<<endl;
return 0;
}
Compilation message
blocks.cpp: In function 'void query(int, int, int, int)':
blocks.cpp:16:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=b+e>>1;
~^~
blocks.cpp: In function 'void update(int, int, int, int, int)':
blocks.cpp:38:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m=b+e>>1;
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Incorrect |
5 ms |
336 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Incorrect |
5 ms |
248 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
408 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
2552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |