#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 5;
int a[M];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int m = a[1];
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] < m) {
      cnt += m - a[i];
    }
    m = max(a[i], m);
  }
  printf("%d\n", cnt);
  return 0;
}
