#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long double pi = acos(-1.0);
const long long MAX = 1000010;
const long long INF = 1ll << 60;
const long long MOD = 1000000007;
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < (long long)(n); i++) cin >> a[i];
  long long ans = 0;
  for (long long i = (1); i < (long long)(n); i++) {
    if (a[i] - a[i - 1] > 0) ans += a[i] - a[i - 1];
  }
  cout << ans << endl;
  return 0;
}
