#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int chkpower2(int x) { return (x && !(x & x - 1)); }
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i1 = 0; i1 < n; i1++) {
    cin >> a[i1];
  }
  int currheight = a[0];
  int ans = 0;
  for (int i1 = 0; i1 < n; i1++) {
    if (a[i1] <= currheight) {
      ans += currheight - a[i1];
    } else {
      currheight = a[i1];
    }
  }
  cout << ans;
  return;
}
int main() {
  int t;
  t = 1;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
