#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 < (N); i++) cin >> A[i];
  int M = 0, dai = 0;
  for (int i = 0; i < (N); i++) {
    if (A[i] < M) dai += M - A[i];
    M = max(M, A[i]);
  }
  cout << dai << endl;
}
