#include <bits/stdc++.h>
int main() {
  int N, A[10000000000 + 1], b, c, d[10000000000 + 1], e, f;
  scanf("%d", &N);
  for (b = 0; b <= N; b++) {
    scanf("%d", &A[b]);
  }
  for (c = 1; c <= N; c++) {
    if (A[c - 1] > A[c]) {
      d[c] = A[c - 1] - A[c];
    } else {
      d[c] = 0;
    }
  }
  for (e = 0; e <= N; e++) {
    f = f + d[e];
  }
  printf("%d", f);
  return 0;
}
