#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  int min_height = 0;
  int sum_height = 0;
  for (int i = 0; i < N; ++i) {
    int A;
    cin >> A;
    if (A < min_height) {
      sum_height += min_height - A;
    } else {
      min_height = A;
    }
  }
  cout << sum_height << endl;
}
