如题,代码:
#include<bits/stdc++.h>
using namespace std;
int n,l,r,h[10005],ans,minh,x;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&h[i]);
l=1;r=n;
while(h[l]==0)l++;
while(h[r]==0)r--;
x=l;
for(int i=l;i<=r;i++)
{
if(h[x]>=h[x+1])
{
int st=x;
while(h[x]>=h[x+1]&&x<=r)x++;
while(h[x]<=h[x+1]&&x<=r)x++;
if(x>r)break;
int en=x;
minh=min(h[st],h[en]);
for(int i=st+1;i<en;i++)ans+=(minh-h[i]);
}
i=x=max(i,x);
}
printf("%d",ans);
return 0;
}