program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,ans=0
    integer(int64), allocatable:: a(:)

    read*, n
    allocate(a(n))
    read*, a(:)

    do i=2,n
        ans=ans+max(a(i)-a(i-1),0_8)
    end do

    print'(i0)', ans
end program main