#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int A[n];
  for (int i = 0; i < n; i++) scanf("%d", &A[i]);
  int mn = A[0];
  int res = 0;
  for (int i = 1; i < n; i++) {
    if (mn < A[i])
      mn = A[i];
    else
      res += A[i] - mn;
  }
  printf("%d\n", abs(res));
}
