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