#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> A;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    A.push_back(tmp);
  }
  int max = A[0];
  int sum = 0;
  for (int i = 1; i < A.size(); i++) {
    if (A[i] < max) {
      sum += max - A[i];
    } else if (A[i] > max) {
      max = A[i];
    }
  }
  cout << sum << endl;
}
