#include <bits/stdc++.h>
using namespace std;
long long a[100005], ans = 0;
int main() {
  long long n;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  printf("%lld", ans);
}
