#include <bits/stdc++.h>
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("avx,avx2")
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  ll sum = 0;
  vector<ll> arr(n);
  for (int i{}; i < n; ++i) cin >> arr[i];
  for (int i{}; i < n - 1; ++i) {
    if (arr[i + 1] > arr[i]) {
      ll yes = (arr[i + 1] - arr[i]);
      sum += yes;
      arr[i] += yes;
    }
  }
  cout << sum << "\n";
}
