#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long N = 2e5 + 5;
const long long INF = 1e9 + 5;
const long long mxN = 1000000007;
const double E = 1e-9;
int n;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int ans = 0, pre = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] < pre)
      ans += pre - a[i];
    else
      pre = a[i];
  }
  cout << ans;
  return 0;
}
