#include <bits/stdc++.h>
using namespace std;
int main() {
  int mx = 0, n, an = 0;
  cin >> n;
  vector<int> ls(n);
  for (int &x : ls) cin >> x;
  for (int &x : ls) {
    mx = max(mx, x);
    an += mx - x;
  }
  cout << an;
}
