constint maxn=20005; int h[maxn]; int ans; intmain(){ // freopen("a.txt","r",stdin); int n; scanf("%d",&n); if ( n<3 ) { printf("%d",0); return0; } for (int i=0; i<n; i++) scanf("%d",&h[i]); int area = 0; int head = 0; int max_index = 0; for (int i = 0; i<n; i++) { if ( h[i] > h[max_index] ) max_index=i; if ( h[head] > h[i] ) { area += (h[head] - h[i]); } else { head = i; ans += area; area = 0; } }
// cout << max_index<<endl; head = n-1; area = 0; for (int i=n-1; i>=max_index; i--) { if ( h[i] < h[head] ) { area += ( h[head] - h[i] ); } else { ans += area; area = 0; head = i; } } printf("%d",ans); return0; }