#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long n;
vector<long long> A(N);
long long i, j, k, l;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  long long stool = 0;
  for (long long i = 0; i < n; i++) {
    cin >> A[i];
    if (i == 0) continue;
    if (A[i] < A[i - 1]) {
      stool += A[i - 1] - A[i];
      A[i] = A[i - 1];
    }
  }
  cout << stool << endl;
  return 0;
}
