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