#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }
const double PI = 3.14159265358979323846;
int vector_finder(vector<int> vec, int number) {
  auto itr = find(vec.begin(), vec.end(), number);
  size_t index = distance(vec.begin(), itr);
  if (index != vec.size()) {
    return 1;
  } else {
    return 0;
  }
}
int digit_sum(int n) {
  if (n < 10) return n;
  return digit_sum(n / 10) + n % 10;
}
unsigned GetDigit(unsigned num) { return log10(num) + 1; }
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int sum = 0, d = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      d = (a[i] - a[i + 1]);
      sum = sum + d;
      a[i + 1] = a[i + 1] + d;
    }
  }
  cout << sum;
}
