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