#include <bits/stdc++.h>
using namespace std;
long long int a[2000009];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int maxx = a[1];
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] < maxx) {
      res += (maxx - a[i]);
    } else
      maxx = a[i];
  }
  cout << res;
}
