#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
inline T mini(T& x, T y) {
  return x = min(x, y);
}
template <typename T>
inline T maxi(T& x, T y) {
  return x = max(x, y);
}
template <typename T>
T rand(T x, T y) {
  return rand() % (y - x + 1) + x;
}
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
class CStep {
 public:
  void solve(std::istream& cin, std::ostream& cout) {
    int n;
    cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    ll sum = 0;
    for (int i = 0; i < n - 1; ++i) {
      if (a[i + 1] > a[i]) sum += a[i + 1] - a[i];
    }
    cout << sum << endl;
  }
};
int32_t main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  CStep solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
