import 'dart:io';

void main() {
  int n = int.parse(stdin.readLineSync());
  List aList =
      stdin.readLineSync().split(" ").map((x) => int.parse(x)).toList();
  int sum = 0;
  int s = 0;
  int dai = 0;
  for (int i = 1; i < n; i++) {
    int diff = (aList[s] - (aList[i]));
    // print(diff);
    if (diff > 0) {
      sum = sum + diff;
    } else if (diff < 0) {
      s = i;
    }
  }
  print(sum);
}
