def allz(arr)
    arr = arr.uniq
    arr.length == 1 and arr[0] == 0
end

# find_allのindex版
# https://qiita.com/pocari/items/0b4e632ef2a2b6a97ba9
def zidx(arr)
    arr.map.with_index { |e,i| e == 0 ? i : nil }.compact
end

def sub1(arr, s, g)
    #print "#{s} #{g}\n"
    arr[s..g] = arr[s..g].map {|x| x > 0 ? x-1 : 0}
    arr
end

def cc
    n = gets.chomp.to_i
    hh = [0] + gets.chomp.split(/\s/).map{|x| x.to_i} + [0]

    cnt = 0
    until allz(hh)
        idz = zidx(hh)
        if idz.length == 2
            hh = sub1(hh, 0, hh.length-1)
            #p hh
            cnt += 1
        else
            # 0 2 4 6
            # 1..1, 3..3, 5..5
            for i in 0..(idz.length-2)
                next if idz[i]+1 > idz[i+1]-1
                hh = sub1(hh, idz[i]+1, idz[i+1]-1)
                #p hh
                cnt += 1
                if allz(hh)
                    break
                end
            end
        end
    end

    puts cnt
end

cc