#include <iostream>
#include <type_traits>

using ull = unsigned long long;

template<class T>
class ReadNumber{
    char c;
    T w,n;
public:
    ReadNumber& operator>>(T &t){
        c=(char)0,w=1,n=0;
        while(!isdigit(c)){
            if constexpr (std::is_signed_v<T>){
                if(c=='-')w=-1;
            }
            c=getchar();
        }
        while(isdigit(c)){
            n=n*10+(c-'0');
            c=getchar();
        }
        t = n*w;
        return *this;
    }
};
static ReadNumber<ull> readull;
// static auto &is = std::cin;
static auto &is = readull;
static auto &os = std::cout;

static ull a, b, ans, n;

int main(){
    is>>n;
    for(size_t i{0};i<n;i++){
        is>>b;
        if(a<b){
            ans+=b-a;
        }
        a=b;
    }
    os<<ans<<'\n';
}