#include <iostream>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<long long int,long long int>,null_type,less<pair<long long int,long long int> >,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
vector<int> adj[100001],dist(100001,0);
bool v[100001];
stack<int> top_sort;
map<int,int> h;
void dfs(int node)
{
    //cout<<node<<" ";
    v[node]=1;
    for(int i=0;i<adj[node].size();i++)
    {
        if(h.find(adj[node][i])==h.end())
            top_sort.push(adj[node][i]),h[adj[node][i]]++;
    }
    if(h.find(node)==h.end())
        top_sort.push(node),h[node]++;
    for(int i=0;i<adj[node].size();i++)
    {
        if(!v[adj[node][i]])
            dfs(adj[node][i]);
    }
}
void go()
{
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++)
        cin>>a[i];
    int ans=0;
    for(int i=0;i<n-1;i++)
    {
        if(a[i] < a[i+1])
            ans+=a[i+1]-a[i];
    }
    cout<<ans;
}
int main()
{
    FIO;
//    int t;
//    cin>>t;
//    while(t--)
        go();
    return 0;
}

