#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, ans = 0;
  cin >> n;
  int a[n];
  for (int x = 0; x < (n); ++x) cin >> a[x];
  int temp = a[0];
  for (int x = 1; x < (n); ++x) {
    if (temp > a[x]) {
      ans += (temp - a[x]);
      a[x] += (temp - a[x]);
    }
    temp = a[x];
  }
  cout << ans << endl;
  return 0;
}
