#include <bits/stdc++.h>
using namespace std;
long long A[100005];
int main() {
  int n, i;
  cin >> n;
  for (i = 0; i < n; i++) cin >> A[i];
  long long cur = 0;
  for (i = 1; i < n; i++)
    if (A[i] < A[i - 1]) cur += (A[i - 1] - A[i]);
  cout << cur << endl;
  return 0;
}
