#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
// Nhập độ cao
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// Tính tổng lượng nước đọng
long long ans = 0;
for (int i = 0; i < n; i++) {
// Tìm max_left và max_right
int max_left = 0, max_right = 0;
for (int j = 0; j <= i; j++) {
max_left = max(max_left, a[j]);
}
for (int j = i; j < n; j++) {
max_right = max(max_right, a[j]);
}
// Tính nước đọng tại vị trí i
ans += max(0, min(max_left, max_right) - a[i]);
}
cout << ans << '\n';
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwoKICAgIGludCBuOwogICAgY2luID4+IG47CiAgICB2ZWN0b3I8aW50PiBhKG4pOwoKICAgIC8vIE5o4bqtcCDEkeG7mSBjYW8KICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgY2luID4+IGFbaV07CiAgICB9CgogICAgLy8gVMOtbmggdOG7lW5nIGzGsOG7o25nIG7GsOG7m2MgxJHhu41uZwogICAgbG9uZyBsb25nIGFucyA9IDA7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIC8vIFTDrG0gbWF4X2xlZnQgdsOgIG1heF9yaWdodAogICAgICAgIGludCBtYXhfbGVmdCA9IDAsIG1heF9yaWdodCA9IDA7CgogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDw9IGk7IGorKykgewogICAgICAgICAgICBtYXhfbGVmdCA9IG1heChtYXhfbGVmdCwgYVtqXSk7CiAgICAgICAgfQoKICAgICAgICBmb3IgKGludCBqID0gaTsgaiA8IG47IGorKykgewogICAgICAgICAgICBtYXhfcmlnaHQgPSBtYXgobWF4X3JpZ2h0LCBhW2pdKTsKICAgICAgICB9CgogICAgICAgIC8vIFTDrW5oIG7GsOG7m2MgxJHhu41uZyB04bqhaSB24buLIHRyw60gaQogICAgICAgIGFucyArPSBtYXgoMCwgbWluKG1heF9sZWZ0LCBtYXhfcmlnaHQpIC0gYVtpXSk7CiAgICB9CgogICAgY291dCA8PCBhbnMgPDwgJ1xuJzsKICAgIHJldHVybiAwOwp9