#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, u;
  int sum = 0;
  int A[100000000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    int s = i + 1;
    if (A[i] > A[s]) {
      u = A[i] - A[s];
      A[s] = A[i];
      sum += u;
    }
  }
  cout << sum << endl;
}
