#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x = 0;
  cin >> n;
  int a[n];
  for (int i = 0; i <= n - 1; i += 1) {
    cin >> a[i];
  }
  for (int i = 0; i <= n - 2; i += 1) {
    if (a[i] > a[i + 1]) {
      x += (a[i] - a[i + 1]);
      a[i + 1] = a[i];
    }
  }
  cout << x << "\n";
}
