#include <bits/stdc++.h>
using namespace std;
int a[200005];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int sum = 0;
  int max = a[1];
  for (int i = 2; i <= n; i++)
    if (a[i] <= max)
      sum += max - a[i];
    else
      max = a[i];
  cout << sum;
  return 0;
}
