#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K, X, T, X1, X2, ans = 0, keep1, keep2;
  cin >> N;
  vector<long> A(N);
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 1; i < N; i++) {
    if (A[i] - A[i - 1] > 0) {
      ans += A[i] - A[i - 1];
    }
  }
  cout << ans;
}
