#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[i];
  }
  int ma = A[0];
  int sum = 0;
  for (int i = 0; i < N; i++) {
    ma = max(ma, A[i]);
    int sub = ma - A[i];
    sum += sub;
  }
  cout << sum << endl;
}
