#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, count = 0, maxx = 0;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      maxx = a[i];
    }
    if (i && (a[i] < maxx)) {
      count += (maxx - a[i]);
    } else if (i && a[i] > maxx) {
      maxx = a[i];
    }
  }
  cout << count << "\n";
  return 0;
}
