#include <bits/stdc++.h>
int main() {
  long long i, n, number[1000000], a = 0, ans = 0;
  scanf("%lld", &n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &number[i]);
    if (i == 0) continue;
    a = number[i - 1] - number[i];
    if (a > 0) {
      ans += a;
      number[i] = number[i - 1];
    }
  }
  printf("%d\n", ans);
  return 0;
}
