#include <iostream>

using namespace std;

int min_mas(int n, int a[]) {
    int k = 0;
    while (a[k] == 0 && k < n + 2) {
        k++;
    }
    int min_ = a[k];
    for (int i = 1; i <= n; i++) {
        if (a[i] < min_ && a[i] != 0) min_ = a[i];
    }
    return min_;
}

int imba(int n, int a[]) {
    int k = 0;
    for (int i = 0; i < n + 1; i++) {
        if (a[i] == 0 && a[i+1] > 0) {
            k++;
        }
    }
    return k;
}

int main()
{
    int n,a[300000],k = 0,res = 0,t = 0;
    cin >> n;
    a[0] = 0;
    a[n+1] = 0;
    a[n+2] = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    while (min_mas(n,a) != 0) {
        t = min_mas(n,a);
        res += t * imba(n,a);
        for (int i = 0; i <= n + 1; i++) {
            if (a[i] > 0) a[i] -= t;
        }
    }
    cout << res;
    return 0;
}
