提交时间:2024-04-06 11:30:50
运行 ID: 141827
#include<bits/stdc++.h> using namespace std; int main() { int n,a[11000],l,r,ans=0; cin>>n; for (int i=1;i<=n;i++) cin>>a[i]; l=1;while (a[l]==0) l++; r=n;while (a[r]==0) r--; int tmp=0,j=l,k=l+1; while (k<=r+1) if (a[k]<a[j]) { tmp+=a[k]; 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]; k--; } else { ans+=(j-k-1)*a[j]-tmp; tmp=0; j=k; k--; } cout<<ans; return 0; }