#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];
  }
  vector<int> m(n);
  m[0] = v[0];
  for (int i = 1; i < n; i++) {
    if (v[i] < m[i - 1]) {
      m[i] = m[i - 1];
    } else {
      m[i] = v[i];
    }
  }
  int s = 0;
  for (int i = 0; i < n; i++) {
    s += (m[i] - v[i]);
  }
  cout << s << endl;
  return 0;
}
