#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max = a[0];
  int t = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < max) {
      t += (max - a[i]);
    } else if (a[i] > max) {
      max = a[i];
    }
  }
  cout << t;
  return 0;
}
