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