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