题意:
思路:
每回枚举去哪个山包
枚举的姿势很重要
//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
int n,k,a[1005],l,r,base,cnt,ans,minn,recl,recr,recbase;
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
start:cnt=0,minn=0x3fffffff;
for(int i=1;i<=n;i++){
if(a[i]<=a[i+1])continue;
l=r=i;
while(l>=1&&a[l]>=a[l-1])l--;
while(r<=n&&a[r]>=a[r+1])r++;
base=max(a[l],a[r]);
if(l>=1||r<=n){
int temp=0;
for(int j=l;j<=r;j++)temp+=max(a[j]-base,0);
if(temp<minn)minn=temp,recl=l,recr=r,recbase=base;
cnt++,i=r;
}
}
if(cnt<=k){printf("%d
",ans);return 0;}
ans+=minn;
for(int i=recl;i<=recr;i++)if(a[i]>recbase)a[i]=recbase;
goto start;
}