#include <bits/stdc++.h>
using namespace std;
int solve(int v[], int n) {
  int res = 0;
  int maxi = v[0];
  for (int i = 1; i < n; i++) {
    if (v[i] < maxi) {
      res += (maxi - v[i]);
    }
    maxi = max(maxi, v[i]);
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  int v[n];
  for (int i = 0; i < n; i++) cin >> v[i];
  cout << solve(v, n) << "\n";
  return 0;
}
