#include <bits/stdc++.h>
using namespace std;
int a[200000 + 5];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n, temp;
  cin >> n;
  int mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > mx) mx = a[i];
  }
  int low = a[0];
  int sum = 0;
  for (int i = 1; i < n; i++) {
    if (low < a[i]) low = a[i];
    if (a[i - 1] <= a[i])
      continue;
    else {
      sum += low - a[i];
      low = a[i - 1];
    }
  }
  cout << sum << "\n";
  return 0;
}
