#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  int sum = 0;
  int memory = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] > memory) {
      memory = a[i];
    } else {
      sum += memory - a[i];
    }
  }
  cout << sum << endl;
  return 0;
}
