#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;
}