#include <bits/stdc++.h>
using namespace std;
void c_step(void) {
  int n;
  int* a;
  int* b;
  cin >> n;
  a = (int*)calloc(n, sizeof(int));
  b = (int*)calloc(n, sizeof(int));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  b[0] = a[0];
  int sum = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < b[i - 1]) {
      b[i] = b[i - 1];
      sum += (b[i - 1] - a[i]);
    } else {
      b[i] = a[i];
    }
  }
  cout << sum << endl;
  free(a);
  free(b);
}
int main() {
  c_step();
  return 0;
}
