#include <bits/stdc++.h>
int main() {
  int people;
  scanf("%d", &people);
  long height[people];
  scanf("%ll", &height[0]);
  int sum = 0;
  for (int i = 1; i < people; i++) {
    scanf("%ll", &height[i]);
    if (height[i - 1] > height[i]) {
      sum += height[i - 1] - height[i];
      height[i] += (height[i - 1] - height[i]);
    }
  }
  printf("%d\n", sum);
  return 0;
}
