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