#include <bits/stdc++.h>
int main() {
  int n, a;
  scanf("%d", &n);
  int sum = 0, beforeA = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    if (i > 0) {
      int diff = beforeA - a;
      if (diff > 0) {
        sum += diff;
        a += diff;
      }
    }
    beforeA = a;
  }
  printf("%d\n", sum);
  return 0;
}
