#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}
void solve() {
  int n;
  cin >> n;
  std::vector<long long int> v(n);
  for (auto &x : v) {
    cin >> x;
  }
  int c = 0, curr = v[0];
  for (int i = 1; i < v.size(); i++) {
    if (v[i] < curr) {
      c += abs(v[i] - curr);
    }
    if (v[i] > curr) {
      curr = v[i];
    }
  }
  cout << c;
}
