#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
using namespace std;
const long long maxn(1e5 + 5);
void rda(long long *a, long long n) {
  for (long long i = 1; i <= n; ++i) a[i] = read();
}
void print(long long *a, long long n) {
  for (long long i = 1; i <= n; ++i) printf("%lld ", a[i]);
  puts("");
}
long long n, a[maxn];
signed main() {
  n = read();
  rda(a, n);
  long long ans = 0;
  for (long long i = 1; i <= n; ++i) {
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans;
}
