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