#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int64_t a[100001], b[100001], c, d, e, f, m, n, p, x, y, z, t, i, j, k, s = 0;
  cin >> t;
  for (i = 0; i < t; i++) {
    cin >> a[i];
  }
  n = 2;
  for (i = 0; i < t - 1; i++) {
    c = *max_element(a, a + n);
    if (a[i] > a[i + 1]) {
      s += c - a[i + 1];
    }
    n++;
  }
  cout << s << "\n";
}
