#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
template <typename T>
inline void read(T& t) {
  char c = getchar();
  t = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) t = t * 10 + c - 48, c = getchar();
  t = f * t;
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
  read(t);
  read(args...);
}
long long a[maxn << 1];
int main() {
  int t = 1;
  while (t--) {
    int n;
    read(n);
    for (int i = 0; i < n; i++) {
      read(a[i]);
    }
    long long ans = a[0], tmp = 0;
    for (int i = 1; i < n; i++) {
      if (a[i] <= ans) {
        tmp += ans - a[i];
      } else {
        ans = a[i];
      }
    }
    printf("%d\n", tmp);
  }
  return 0;
}
