#include <bits/stdc++.h>
using namespace std;
int i, n, a[100005];
long long rs;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (i = 1; i <= n; ++i) cin >> a[i];
  for (i = 2; i <= n; ++i)
    if (a[i - 1] > a[i]) rs += a[i - 1] - a[i];
  cout << rs << '\n';
  return 0;
}
