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