#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  long long count = 0;
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (count < A[i])
      count = A[i];
    else
      ans += A[i] - count;
  }
  cout << ans << endl;
}
