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