#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e8;
const int mod = 1e9 + 7;
int n, x, t, a[mxN];
map<int, int> hash1;
void solve() {
  cin >> n;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    if (a[i] - a[i - 1] < 0) {
      sum += abs(a[i] - a[i - 1]);
      a[i] = a[i - 1];
    }
  }
  cout << sum << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  t = 1;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
}
