#include <bits/stdc++.h>
using namespace std;
int main() {
  long int N, x, sum = 0;
  cin >> N;
  vector<long int> a(N);
  for (int i = 0; i < (int)(N); i++) cin >> a.at(i);
  x = max(a.at(0), a.at(1));
  for (int i = 0; i < (int)(N - 1); i++) {
    if (x <= a.at(i + 1))
      x = max(x, a.at(i + 1));
    else if (x > a.at(i + 1))
      sum += (x - a.at(i + 1));
  }
  cout << sum << endl;
}
