#include <bits/stdc++.h>
using namespace std;
int main() {
  int size_of_arr;
  cin >> size_of_arr;
  vector<int> orig(size_of_arr);
  vector<int> dup(size_of_arr);
  int maxi = INT_MIN;
  int sum = 0;
  for (int i = 0; i < size_of_arr; i++) cin >> orig[i];
  for (int i = 0; i < size_of_arr; i++) {
    if (orig[i] > maxi) maxi = orig[i];
    dup[i] = maxi;
  }
  for (int i = 0; i < size_of_arr; i++) sum += dup[i] - orig[i];
  cout << sum << endl;
  return 0;
}
