#include <bits/stdc++.h>
int main(int argc, char *argv[]) {
  int n;
  std::cin >> n;
  long sum = 0;
  int h = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    std::cin >> a;
    if (i > 1) {
      if (h < a) sum += a - h;
    }
    h = a;
  }
  std::cout << sum << std::endl;
}
