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