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