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