#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int m = 0, steps = 0;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] > m) {
      m = A[i];
    }
    steps += m - A[i];
  }
  cout << steps;
}
