#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int a[n];
  for (auto &i : a) cin >> i;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i]) {
      ans += a[i - 1] - a[i];
      a[i] += (a[i - 1] - a[i]);
    }
  }
  cout << ans;
  return 0;
}
