#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int a[5001];
int b[5001];
int h[5001];
int main(int argc, char* argv[]) {
int n, ans = 0;
int minh;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", h + i);
a[1] = h[1];
for (int i = 2; i <= n; i++)
a[i] = max(a[i - 1], h[i]);
b[n] = h[n];
for (int i = n - 1; i >= 1; i--)
b[i] = max(b[i + 1], h[i]);
for (int i = 2; i < n; i++) {
minh = min(a[i - 1], b[i + 1]);
if (minh > h[i])
ans += minh - h[i];
}
printf("%d\n", ans);
return 0;
}
|