#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int maxHeight = 0;
  int sumFumidai = 0;
  maxHeight = A.at(0);
  for (int i = 1; i < N; i++) {
    if (A.at(i) < maxHeight) {
      int tarinai = maxHeight - A.at(i);
      sumFumidai += tarinai;
    } else if (A.at(i) > maxHeight) {
      maxHeight = A.at(i);
    }
  }
  cout << sumFumidai << endl;
}
