#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0L);
const long long mod = 1e9 + 7;
long long dx[4] = {-1, 0, 1, 0};
long long dy[4] = {0, -1, 0, 1};
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>& p) {
  ost << "{ " << p.first << ", " << p.second << " }";
  return ost;
}
template <class T>
ostream& operator<<(ostream& ost, const vector<T>& v) {
  ost << "{ ";
  for (int i = 0; i < v.size(); i++) {
    if (i) ost << ", ";
    ost << v[i];
  }
  ost << " }";
  return ost;
}
template <class A, class B>
ostream& operator<<(ostream& ost, const map<A, B>& v) {
  ost << "{ ";
  for (auto p : v) {
    ost << "{ " << p.first << ", " << p.second << " }";
  }
  ost << " }";
  return ost;
}
void debugs() { std::cerr << "\n"; }
template <class T, class... Args>
void debugs(const T& x, const Args&... args) {
  std::cerr << x << " ";
  debugs(args...);
}
bool out_check(long long a, long long b) { return (0 <= a && a < b); }
double pitagoras(long long a, long long b, long long c, long long d) {
  double dx = a - b, dy = c - d;
  return pow(dx * dx + dy * dy, 0.5);
}
long long modpow(long long a, long long b, long long mod) {
  long long c = 1;
  while (b > 0) {
    if (b & 1) {
      c = a * c % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return c;
}
void Yes(bool x) { cout << ((x) ? "Yes\n" : "No\n"); }
void YES(bool x) { cout << ((x) ? "YES\n" : "NO\n"); }
void yes(bool x) { cout << ((x) ? "yes\n" : "no\n"); }
void Yay(bool x) { cout << ((x) ? "Yay!\n" : ":(\n"); }
void OK(bool x) { cout << ((x) ? "OK!\n" : "NG\n"); }
long long n, m, d, e, l, r, k, h, w, Q, ans, ret = LLONG_MAX;
bool flag = false, flag2 = false, flag3 = false;
string s, t, u;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; ++i) cin >> a[i];
  for (long long i = (1); i < (n); ++i) {
    ans += max(0LL, a[i] - a[i - 1]);
  }
  cout << ans << endl;
}
