#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int* a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int count = 0;
  int max = a[0];
  for (int i = 1; i < n; i++) {
    max = max > a[i] ? max : a[i];
    if (a[i] != max) {
      count += max - a[i];
    }
  }
  cout << count << endl;
  return 0;
}
