#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n = 0;
  cin >> n;
  long long h = 0;
  cin >> h;
  long long ans = 0;
  for (long long i = 1; i < n; i++) {
    long long temp = 0;
    cin >> temp;
    if (temp < h)
      ans += abs(h - temp);
    else
      h = temp;
  }
  cout << (int)ans;
  return 0;
}
