#include <bits/stdc++.h>
using namespace std;
void solve();
void YN(bool b) {
  if (b) {
    cout << "YES"
         << "\n";
    ;
  } else {
    cout << "NO"
         << "\n";
    ;
  }
};
void yn(bool b) {
  if (b) {
    cout << "Yes"
         << "\n";
    ;
  } else {
    cout << "No"
         << "\n";
    ;
  }
};
void queries() {
  int n;
  cin >> n;
  while (n--) solve();
}
const int maxn = 200200;
void solve() { return; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long rs = 0;
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 1; i < n; i++) {
    if (arr[i] > arr[i - 1]) {
      rs += arr[i] - arr[i - 1];
    }
  }
  cout << rs << "\n";
  ;
  return 0;
}
