#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int sum = 0, i, n, mx;
  cin >> n;
  vector<int> v(n);
  for (i = 0; i < n; i++) cin >> v[i];
  mx = v[0];
  for (i = 1; i < n; i++) {
    if (mx > v[i]) {
      sum += mx - v[i];
    } else
      mx = v[i];
  }
  cout << sum;
}
