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