#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int a[n], s = 0;
  for (int i = 0; i < n; i++) cin >> a[i];
  int m = INT_MIN;
  for (int i = 0; i < n; i++) {
    m = max(m, a[i]);
    if (a[i] < m) s += m - a[i];
  }
  cout << s << endl;
  return 0;
}
