#include <bits/stdc++.h>
using ll = long long;
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
ll n, ans;
int maxv, num;
vector<int> a(1000100);
int main() {
  cin >> n;
  for (int i = (0); i < (n); i++) cin >> a[i];
  for (int i = (0); i < (n - 1); i++) {
    if (a[i] < a[i + 1]) ans += a[i + 1] - a[i];
  }
  cout << ans << "\n";
  return 0;
}
