#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int big{0};
  int height{0};
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      big = v[i];
    }
    if (big < v[i]) {
      big = v[i];
    }
    height += big - v[i];
  }
  cout << height;
  return 0;
}
