#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n), maxnum, tp;
  int maxh = 0;
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
    if (v.at(i) > maxh) {
      maxh = v.at(i);
      maxnum.push_back(v.at(i));
      tp.push_back(i);
    }
  }
  int step = 0;
  int cursor = 0;
  for (int i = 0; i < tp.size(); i++) {
    int endp = (i + 1 >= tp.size()) ? n : tp.at(i + 1);
    for (int j = cursor; j < endp; j++) {
      step += (maxnum.at(i) - v.at(j));
    }
    cursor = endp;
  }
  cout << step << endl;
}
