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