#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[1000000], ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      int d = a[i] - a[i + 1];
      a[i + 1] = a[i];
      ans = ans + d;
    }
  }
  printf("%d", ans);
  return 0;
}
