#include <bits/stdc++.h>
using namespace std;
const int mxn = 200001;
int n;
long long a[mxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  int ret = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (!i) continue;
    ret += max(0ll, a[i - 1] - a[i]);
    a[i] = max(a[i], a[i - 1]);
  }
  cout << ret << '\n';
  return 0;
}
