#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n, x, t;
  cin >> n;
  long long arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int s = 0;
  for (int i = 0, j = i + 1; j < n; j++, i++) {
    int c = 0;
    if (arr[j] < arr[i]) {
      c = c + arr[i] - arr[j];
      arr[j] += c;
      s += c;
    } else {
      c = 0;
      s += c;
    }
  }
  cout << s << endl;
}
