#include <bits/stdc++.h>
int main(void) {
  int high[200001], i = 0, a, count = 0, x = 0;
  scanf("%d", &a);
  for (i = 1; i <= a; i++) {
    scanf("%d", &high[i]);
  }
  for (i = 0; i <= a; i++) {
    x = high[i] - high[i + 1];
    if (x >= 1) high[i + 1] += x;
    count += x;
  }
  printf("%d", count);
  return 0;
}
