#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; i++) {
    cin >> vec.at(i);
  }
  int count = vec.at(0);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (count < vec.at(i)) {
      count = vec.at(i);
    }
    sum = sum + (count - vec.at(i));
  }
  cout << sum << endl;
}
