#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  long long ans = 0;
  int lastindex = n;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int amax = *max_element(a.begin(), a.end());
  int amaxindex = max_element(a.begin(), a.end()) - a.begin();
  while (amaxindex != 0) {
    for (int i = amaxindex + 1; i < lastindex; i++) {
      ans += amax - a[i];
    }
    lastindex = amaxindex;
    amax = *max_element(a.begin(), a.end() - (amaxindex + 1));
    amaxindex = max_element(a.begin(), a.end() - (amaxindex + 1)) - a.begin();
    if (amaxindex == 0) {
      for (int i = amaxindex + 1; i < lastindex; i++) {
        ans += amax - a[i];
      }
    }
  }
  cout << ans;
}
