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