import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;

alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias SugarWater = Tuple!(long, "swater", long, "sugar");

T lread(T = long)()
{
    return readln.chomp.to!T();
}

T[] aryread(T = long)()
{
    return readln.split.to!(T[])();
}

void scan(TList...)(ref TList Args)
{
    auto line = readln.split();
    foreach (i, T; TList)
    {
        T val = line[i].to!(T);
        Args[i] = val;
    }
}

void main()
{
    auto n = lread();
    auto a = aryread();
    long right, left, ans;
    while (1)
    {
        while (left < a.length && a[left] == 0)
            left++;

        if(left == a.length)
            break;

        right = left;
        while (right < a.length && a[right] != 0)
            right++;
        auto tmp = a[left .. right];
        auto mini = tmp.reduce!(min);
        tmp[] -= mini;
        ans += mini;
        left = right = 0;
    }
    ans.writeln();
}
