#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a;
  int r2 = 0;
  int ans;
  int prev = 0;
  long long res = 0;
  int pr2 = 0;
  for (int i = 0; i < n; i++) {
    int tmp;
    scanf("%d", &tmp);
    if (i == 0) {
      prev = tmp;
      continue;
    }
    if (tmp < prev) {
      res += prev - tmp;
    }
    prev = tmp;
  }
  printf("%I64d", res);
  return 0;
}
