#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ll N;cin>>N;vector<ll> V(N);cin>>V[0];ll a=0;
  for(int i=1;i<N;i++){
    cin>>V[i];
    a+=max(V[i]-V[i-1],0);
  }cout<<a<<endl;
}