#include <bits/stdc++.h>
using namespace std;
int trapWater(vector<int>& heights) {
int heightsSize = heights.size();
int totalWater = 0; // Variable to store the total trapped water
for(int i = 0; i < heightsSize; i++) {
// Initialize the maximum heights to left and right
int maximumLeft = heights[0], maximumRight = heights[heightsSize-1];
// Find the maximum heights on the left of the current position
for(int j = 0; j <= i; j++) {
maximumLeft = max(maximumLeft, heights[j]);
}
// Find the maximum heights on the right of the current position
for(int j = i; j < heightsSize; j++) {
maximumRight = max(maximumRight, heights[j]);
}
// Calculate the trapped water at the current position
totalWater += max(0, min(maximumLeft, maximumRight) - heights[i]);
}
return totalWater;
}
int main() {
int n; cin >> n;
vector<int> heights(n);
for(int& height : heights) cin >> height;
cout << trapWater(heights);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgdHJhcFdhdGVyKHZlY3RvcjxpbnQ+JiBoZWlnaHRzKSB7CiAgICBpbnQgaGVpZ2h0c1NpemUgPSBoZWlnaHRzLnNpemUoKTsKICAgIGludCB0b3RhbFdhdGVyID0gMDsgLy8gVmFyaWFibGUgdG8gc3RvcmUgdGhlIHRvdGFsIHRyYXBwZWQgd2F0ZXIKCiAgICBmb3IoaW50IGkgPSAwOyBpIDwgaGVpZ2h0c1NpemU7IGkrKykgewogICAgICAgIC8vIEluaXRpYWxpemUgdGhlIG1heGltdW0gaGVpZ2h0cyB0byBsZWZ0IGFuZCByaWdodAogICAgICAgIGludCBtYXhpbXVtTGVmdCA9IGhlaWdodHNbMF0sIG1heGltdW1SaWdodCA9IGhlaWdodHNbaGVpZ2h0c1NpemUtMV07CiAgICAgICAgCiAgICAgICAgLy8gRmluZCB0aGUgbWF4aW11bSBoZWlnaHRzIG9uIHRoZSBsZWZ0IG9mIHRoZSBjdXJyZW50IHBvc2l0aW9uCiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8PSBpOyBqKyspIHsKICAgICAgICAgICAgbWF4aW11bUxlZnQgPS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