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