#include <bits/stdc++.h>
using namespace std;
int read() {
  int sum = 0, f = 1;
  char s = getchar();
  while (s < '0' or s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' and s <= '9') {
    sum = sum * 10 + s - '0';
    s = getchar();
  }
  return sum * f;
}
const int N = 2 * 1e5 + 10;
int a[N], b[N];
int n;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  b[1] = a[1];
  for (int i = 2; i <= n; i++) b[i] = max(b[i - 1], a[i - 1]);
  int sum = 0;
  for (int i = 2; i <= n; i++) {
    if (b[i] > a[i]) sum += b[i] - a[i];
  }
  cout << sum << endl;
  return 0;
}
