#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
void solve() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < int(n); i++) {
    cin >> arr[i];
  }
  int cur = arr[0];
  int ans = 0;
  for (int i = int(1); i <= int(n - 1); i++) {
    if (arr[i] < cur)
      ans += cur - arr[i];
    else
      cur = arr[i];
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}
