#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  ;
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  int currentMax = A[0];
  int height = 0;
  for (int i = 0; i < n; i++) {
    if (A[i] > currentMax) {
      currentMax = A[i];
    } else if (A[i] < currentMax) {
      height += currentMax - A[i];
    }
  }
  cout << height << "\n";
  return 0;
}
