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