#include <bits/stdc++.h>
using namespace std;
const int max_n = 2e5 + 10;
int a[max_n];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  int max_h = a[1], sum = 0;
  for (int i = 2; i <= n; ++i) {
    if (a[i] < max_h)
      sum += max_h - a[i];
    else
      max_h = a[i];
  }
  cout << sum;
  return 0;
}
