#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int arr[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", (arr + i));
  }
  int prev = 0, s = 0, temp;
  for (int i = 0; i < n; i++) {
    temp = arr[i];
    if (arr[i] < prev) {
      arr[i] = prev;
    }
    temp = arr[i] - temp;
    s = s + temp;
    prev = arr[i];
  }
  printf("%d", s);
  return 0;
}
