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