#include <bits/stdc++.h>
int main(void) {
  int n, total = 0, height_front, height_back, diff, i;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &height_back);
    if (i != 0) {
      if (height_back < height_front) {
        diff = height_front - height_back;
        total += diff;
        height_back += diff;
      }
    }
    height_front = height_back;
  }
  printf("%d\n", total);
}
