#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) cin >> A[i];
  int sum = 0;
  int min = A[0];
  for (int i = 1; i < n; i++) {
    if (A[i] < min)
      sum = sum + min - A[i];
    else
      min = A[i];
  }
  cout << sum;
  return 0;
}
