#include <bits/stdc++.h>
using namespace std;
int main() {
  int people;
  cin >> people;
  long int* row;
  row = (long int*)malloc(people * sizeof(long int));
  for (int i = 0; i < people; i++) {
    cin >> row[i];
  }
  int stool = 0;
  for (int i = 0; i < people - 1; i++) {
    if (row[i] > row[i + 1]) {
      stool += row[i] - row[i + 1];
      row[i + 1] += row[i] - row[i + 1];
    }
  }
  cout << stool << endl;
  return 0;
}
