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