#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> v;
  long long a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  reverse(v.begin(), v.end());
  long long z = 0;
  for (int i = 0; i < n; i++) {
    auto it = v.begin() + i;
    long long x = *max_element(it, v.end());
    z += x - v.at(i);
  }
  cout << z << endl;
  return 0;
}
