import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

int calc(int[] a) {

    int ans = 0;
    while (a.any!(e => e > 0)) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] == 0) continue;

            int x = a[i];
            int last = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] == 0) break;
                x = min(x, a[j]);
                last = j;
            }

            for (int j = i; j <= last; j++) {
                a[j] -= x;
            }
            ans += x;
            break;
        }
    }
    return ans;

}

void main() {
    readint;
    auto a = readints;
    writeln(calc(a));
}
