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