提交时间:2024-08-21 10:01:42
运行 ID: 175670
#include<cstdio> int tmp,j,k; int n,a[10001],l,r,ans; int main() { scanf("%d",&n); for (int i=1;i<=n;i++)scanf("%d",&a[i]); l=1; while(a[l]==0)l++; r=n; while(a[r]==0)r--; j=l; k=l+1; while(k<=(r+1)) if(a[k]<a[j])tmp+=a[k++]; else { ans+=(k-j-1)*a[j]-tmp; tmp=0; j=k; k++; } tmp=0; j=r; k=r-1; while(k>=l-1) if(a[k]<=a[j])tmp+=a[k--]; else { ans+=(j-k-1)*a[j]-tmp; tmp=0; j=k; k--; } printf("%d",ans); return 0; }