#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int main() {
  int n, ans = 0;
  long long a[200000];
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    if (i != 0) {
      if (a[i - 1] > a[i]) {
        ans = ans + a[i - 1] - a[i];
        a[i] = a[i - 1];
      }
    }
  }
  cout << ans << endl;
  return 0;
}
