#include <bits/stdc++.h>
using namespace std;
struct debug {
  ~debug() { cerr << "\n"; }
  template <class C>
  debug& operator<<(C c) {
    cerr << c;
    return *this;
  }
};
const int nax = 2e5;
int n, a[nax];
void sol() {
  cin >> n >> a[0];
  int ans = 0;
  for (int i = 1; i < n; ++i) {
    cin >> a[i];
    if (a[i] > a[i - 1]) {
      ans += a[i] - a[i - 1];
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  sol();
  return 0;
}
