提交时间:2024-04-19 13:21:21

运行 ID: 143850

#include<bits/stdc++.h> using namespace std; int n,a[11000],l=1,r,cnt,tmp,j=l,k=l+1; int main(){ cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; r=n; while(k<=r+1){ if(a[k]<a[j]){ tmp+=a[k]; k++; } else{ cnt+=(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{ cnt+=(j-k-1)*a[j]-tmp; tmp=0; j=k; k--; } } cout<<cnt; return 0; }