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