#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> v;
  if (n == 0) {
    cout << 0;
  }
  return 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  int m = v[0];
  int stool = 0;
  for (int i = 1; i < n; i++) {
    if (v[i] < m) {
      int sdif = m - v[i];
      stool = stool + sdif;
    } else if (v[i] > m) {
      m = v[i];
    }
  }
  cout << stool;
  return 0;
}
