#include <bits/stdc++.h>
#pragma comment(linker, "/STACK: 16777216")
using namespace std;
void dout() { cerr << endl; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}
using ll = long long;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;
template <typename T>
void do_uniq(vector<T> vec) {
  sort((vec).begin(), (vec).end());
  vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
}
template <typename T>
void freadT(T &x) {
  x = 0;
  char ch = getchar_unlocked();
  ll sign = 1;
  while (!isdigit(ch)) {
    if (ch == '-') sign *= -1;
    ch = getchar_unlocked();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar_unlocked();
  }
  x *= sign;
}
int fparse(string s, int start_indx) {
  int x = 0;
  while (isdigit(s[start_indx])) {
    x = x * 10 + s[start_indx] - '0';
    ++start_indx;
  }
  return x;
}
ll mod = 998244353;
ll add(ll a, ll b) { return (a + b) % mod; }
ll mult(ll a, ll b) { return (a * b) % mod; }
const int MAXN = 3e5 + 120;
void solve() {
  int n;
  cin >> n;
  ll answr = 0;
  ll pref = 0;
  for (int i = 0; i < n; ++i) {
    ll curr;
    cin >> curr;
    if (i == 0)
      pref = curr;
    else {
      answr += max(0ll, curr - pref);
      pref = curr;
    }
  }
  cout << answr << '\n';
}
clock_t timestamp_start = clock();
void time_calc() {
  cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\n";
}
void multisolve() {
  int t;
  cin >> t;
  int i = 1;
  while (t--) {
    solve();
    i++;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(20);
  solve();
}
