#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  int c = 0;
  int min = 0;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (min > a)
      c = c + min - a;
    else
      min = a;
  }
  cout << c << endl;
  return 0;
}
