#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, fumi = 0;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    int front_max = *max_element(A, A + i + 1);
    fumi += front_max - A[i];
  }
  cout << fumi;
  return 0;
}
