#include <bits/stdc++.h>
using namespace std;
const long long M = 1000000007;
int main() {
  long long N;
  cin >> N;
  long long A[N];
  for (long long i = 0; i < (long long)(N); i++) cin >> A[i];
  long long cnt = A[0];
  long long sum = 0;
  for (long long i = 0; i < (long long)(N - 1); i++) {
    if (A[i] < A[i + 1]) sum += A[i + 1] - A[i];
    cnt = max(cnt, A[i]);
  }
  cout << sum << endl;
}
