#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 1e6 + 8;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  long long int i, a[i];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int s = 0;
  for (i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      s = s + (a[i] - a[i + 1]);
      a[i + 1] = a[i];
    }
  }
  cout << s << "\n";
  cerr << "\nTime elapsed:" << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
