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