#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7, MAX = 1e6 + 5;
const long long INF = 1e18 + 5;
int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int in = 0; in < 0 + n; in++) cin >> A[in];
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    if (A[i] < A[i - 1]) ans += A[i - 1] - A[i];
  }
  cout << ans << endl;
}
