#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (a[i + 1] - a[i] < 0) {
      v.push_back(abs(a[i + 1] - a[i]));
      a[i + 1] = a[i + 1] + abs(a[i + 1] - a[i]);
    }
  }
  int x = 0;
  for (int i = 0; i < v.size(); i++) {
    x = x + v[i];
  }
  cout << x << endl;
  return 0;
}
