#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) cin >> A[i];
  int min = A[0];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (min <= A[i]) {
      min = A[i];
    } else {
      cnt += min - A[i];
    }
  }
  cout << cnt << endl;
  return 0;
}
