#include <bits/stdc++.h>
using namespace std;
inline long long read(void) {
  long long x = 0, tmp = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') tmp = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * tmp;
}
const long long N = 100010;
long long n, a[N];
signed main(void) {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; ++i) scanf("%lld", &a[i]);
  long long ans = 0;
  long long now = 1;
  while (now <= n) {
    while (a[now] <= a[now + 1] && now <= n) ++now;
    if (now >= n) break;
    long long i = 0;
    while (now + i + 1 <= n && a[now + i] > a[now + i + 1]) ++i;
    ans += a[now] - a[now + i];
    now += i;
  }
  printf("%lld", ans);
  return 0;
}
