#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a;
  cin >> a;
  long long b[a];
  for (long long i = 0; i < a; i++) {
    cin >> b[i];
  }
  long long meow = 0;
  for (long long i = a - 1; i > 0; i--) {
    if (b[i] < b[i - 1]) {
      meow = meow + b[i - 1] - b[i];
    }
  }
  cout << meow;
  return 0;
}
