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