function Main(input) {
    var lines = input.split("\n");
    var N = lines[0] - 0;
    var kadan = lines[1].split(" ").map(function(value) { return value - 0; });
    var count = mizuyari(kadan, 0, N - 1);
    console.log(count);
}
function nonZeroArray(array, l, r) {
    var result = true;
    for (var n = l; result && n <= r; n++) {
        result = result && (array[n] > 0);
    }
    return result;
}
function zeroArray(array, l, r) {
    var result = true;
    for (var n = l; result && n <= r; n++) {
        result = result && (array[n] == 0);
    }
    return result;
}
function mizuyari(kadan, l, r) {
    var count = 0;
    while (nonZeroArray(kadan, l, r)) {
        for (var n = l; n <= r; n++) {
            kadan[n] -= 1;
        }
        count++;
    }
    var zero = zeroArray(kadan, l, r);
    if (!zero) {
        var N = r + 1;
        while (l < N) {
            while (l < N && kadan[l] == 0) l++;
            r = l;
            while (r < N && kadan[r] != 0) r++;
            count += mizuyari(kadan, l, r - 1);
            l = r + 1;
        }
    }
    return count;
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
