#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int& x : v) cin >> x;
  int a = 0;
  int cur = v[0];
  for (int i = 1; i < n; i++) {
    if (v[i] < cur) {
      a += cur - v[i];
    }
    cur = max(cur, v[i]);
  }
  printf("%d", a);
  return 0;
}
