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