#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, s = 0, m;
  cin >> n;
  int c = -1;
  vector<int> v;
  for (i = 0; i < n; i++) {
    cin >> m;
    v.push_back(m);
  }
  for (i = 0; i < n; i++) {
    c = max(v[i], c);
    s = s + (c - v[i]);
  }
  cout << s << "\n";
}
