#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int a;
  int flag = 0;
  int s = 0;
  int add = 0;
  cin >> a;
  vector<int> vec(a);
  for (int i = 0; i < (a); ++i) {
    cin >> vec[i];
  }
  flag = vec[0];
  for (int i = 0; i < (a); ++i) {
    if (vec[i] < flag) {
      s = flag - vec[i];
      add = add + s;
    } else {
      flag = vec[i];
    }
  }
  cout << add << endl;
  return 0;
}
